如何使用Mobiledoc-kit重现medium.com编辑器的“嵌入媒体”按钮?

问题描述 投票:4回答:1

我们正在构建一个编辑器,并决定使用Mobiledoc-kit来克服contentEditable限制。

正如我们真的非常喜欢medium.com编辑器的简单性,我们正在尝试重现其“插入媒体”功能,即“仅允许媒体插入空行”大致转换为默认情况下的空白部分mobiledoc-kit场景。这种行为包括两个事件:

  • 当前行中没有其他内容时显示按钮/允许插入
  • 在相反的情况下隐藏/禁止插入。

为了达到这个目的,我试图:

  • 观察“进入”键按下显示按钮
  • 观察截面长度以隐藏/显示按钮

我仍然没有弄清楚如何检测“输入”按键和我用来检测段长度的方法postEditor.editor.range.tail.section.length返回didUpdatePostwillRender回调中的前一段长度。

这是我使用mobiledoc-kit的第一天,以及关于我是否选择了正确路径的任何反馈以及如何继续进行的建议都非常非常感谢。

javascript wysiwyg mobiledoc-kit
1个回答
4
投票

cursorDidChange钩(docs here)很可能是你想要使用的。

您可以观察光标移动并在光标位于空标记部分时作出反应,例如:

editor.cursorDidChange(() => {
  // if the range isn't collapsed (i.e., the user has selected some text),
  // just return
  if (!editor.range.isCollapsed) { return; }

  // head is a mobiledoc-kit position object.
  // range consists of two positions: head and tail.
  // For a collapsed range, head === tail
  let head = editor.range.head;

  // section can be a markup section (contains user-editable text
  // or a card section. All sections have an `isBlank` method
  let section = head.section;
  if (section.isBlank) {
    // the cursor is in a blank section; show the media insertion UI
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.