Create a Note from Template and switch to editor view

Environment

  • Platform: Arch Linux
  • Platform version: Kernel 5.14.18
  • App Version: 5.4.3

Question

I found the post on adding a template and then got a bit carried away on wanting to change a couple of things for my end, just hit a bit of a wall on how to achieve exactly what I want.

  1. I want to create a new note from a template and spawn the note on a new window regardless of where I am in the app, I got this to work exactly how I would like. However, I can’t get it to switch to editor mode, so if I’m in preview mode on the main window, the new window opens in preview mode. Not sure how to change that behavior so the new window opens in editor mode.

  2. I would also like to have the main window switch to preview mode whenever I open an existing note, regardless of the mode I was in previously. In the majority of cases, when I edit a note I open it on a new window so that I can use the main window to reference other notes I have. However, not exactly sure how to achieve this or if it’s even possible.

I have the code below for the template action mentioned in point 1. I tried adding the view:toggle-preview but that toggles the main window and not the new window.

inkdrop.commands.add(document.body, "custom:new-writeup", async () => {
  const db = inkdrop.main.dataStore.getLocalDB()
  const template = await db.notes.get("note:fSolNc8jV")
  const note = {
    ...template,
    _id: db.notes.createId(),
    _rev: undefined,
    title: "Untitled Writeup",
    createdAt: +new Date(),
    updatedAt: +new Date(),
    pinned: false,
  }
  try {
    await db.notes.put(note)
    inkdrop.commands.dispatch(document.body, "core:open-note-in-separate-window", {
      noteId: note._id,
    })
    inkdrop.commands.dispatch(document.body, "editor:focus-mde")
  } catch (e) {
    console.error(e)
  }
})

Hi Nicole,

Thanks for the question.

You can hook the core:open-note command to extend the behavior for opening a note.

1. Switch to the edit mode on the new window

Basically, you can’t control a new window from the existing window because it’s a separate process.
But the new window also loads init.js, so the hook should work as well.
The new window has a (private) parameter to open a note, which is stored in “inkdrop.windowParams.noteId”.
If the window has the parameter, you can change the mode to 'edit' in the hook.

2. Switch to the preview mode when opening a note

If the window doesn’t have windowParams.noteId, it means it’s a main window.
In this case, you can change the mode to 'preview'.


Solution

So, this code should work to accomplish what you want:

const { actions } = require('inkdrop')

inkdrop.commands.add(document.body, {
  'core:open-note': ()=> {
    if (inkdrop.windowParams.noteId) {
      // note window
      inkdrop.store.dispatch(actions.editor.changeViewMode('edit'))
    } else {
      // main window
      inkdrop.store.dispatch(actions.editor.changeViewMode('preview'))
    }
  }
})

Hope that helps!

1 Like

Hi Takuya,

Yes, that works exactly how I wanted it. Thanks so much for your hard work and for taking the time to explain and look into this question I had.

Cool! Have a nice day.