gpt4 book ai didi

javascript - 如何使用 Mobiledoc-kit 重现 medium.com 编辑器的 "embed media"按钮?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:43:04 25 4
gpt4 key购买 nike

我们正在构建一个编辑器并决定使用 Mobiledoc-kit克服内容可编辑的限制。

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

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

为了实现这一目标,我正在尝试:

  • 观察“输入”键按下以显示按钮
  • 观察部分长度以隐藏/显示按钮

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

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

最佳答案

cursorDidChange Hook ( docs here ) 可能是您想要使用的。

您可以观察光标移动并在光标位于空标记部分时使用react,例如:

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
}
});

关于javascript - 如何使用 Mobiledoc-kit 重现 medium.com 编辑器的 "embed media"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com