Can't receive metadata sent from a command function

I have a command function that dispatches a new state with metadata saying a tooltip has to be displayed. In a PluginView update method, I watch for that metadata and display the tootip if present. However calling getMeta in the update method returns undefined.

const displayIframeLinkInput = (state, dispatch, view) => {
  if (dispatch) {
    const tr = state.tr.setMeta('iframe-link-input', 'open')
    dispatch(tr)
  }
  return true
}
class iframeLinkInputView {
  /**
   *
   * @param {EditorView} view
   */
  constructor(view) {
    this.view = view
    this.CSS = 'iframe-link-input-dialog'

    this.dialog = document.createElement('div')

    this.form = document.createElement('form')

    this.input = document.createElement('input')

    this.button = document.createElement('button')

    this.form.appendChild(this.input)
    this.form.appendChild(this.button)
    this.dialog.appendChild(this.form)
    view.dom.parentNode.appendChild(this.dialog)
  }

  _onSubmit(e) {
    e.preventDefault()
    console.log('submit')
  }

  /**
   * @param {EditorView} view
   * @param {EditorState} lastState
   */
  update(view, lastState) {
    console.log('update: ', view, lastState) <---- meta property is empty in both
  }

  destroy() {
    this.dialog.remove()
  }
}

export const iframeLinkInputPlugin = new Plugin({
  view(editorView) {
    return new iframeLinkInputView(editorView)
  },
})

Metadata is a thing that gets added to transactions, but plugin views do not have access to transactions, so they cannot read this. You may be able to use a plugin state field to track whether a link should be displayed, and respond to changes in that state field in your plugin view.

1 Like

Thank you for the guidance. I was able to check metadata using the plugin state field. Just one more question though. From what I have seen, the view, lastState params in the update method “can” access the transaction. However the meta properties in their transactions were empty when i logged to console. Is it because both params don’t contain the latest transactions?

No, it can’t. I think you may be creating a new transaction and confusing it with the transaction you are looking for.