我已经创建了一个自定义插件,如图所示.我可以添加类为"PlaceHolder"的对象.<span class="placeholder">Loop-ANS(Q2)</span>美元.

enter image description here

创建自的内联构件 Created from

内联演示小工具 Demo Demo Demo

现在,当有人从编辑器中删除此项时,我需要生成一个事件(调用一个函数),以便我可以标记此项已在我的Redux状态中删除.我用下面的代码得到了这个问题的解决方案.

editor.model.document.on( 'change:data', () => {  // Listen to all changes in model.
    // Get changes buffered in Differ. Include items moved to the graveyard (removed from document).
    const changes = editor.model.document.differ.getChanges( { includeChangesInGraveyard: true } );

    for ( const change of changes ) {
        // Check items moved to the graveyard (so they are removed from the document root).
        if ( change.type == 'insert' && change.name != '$text' && change.position.root.rootName == '$graveyard' ) {

            // The element that we are looking for could be nested in some block-quote or a table cell
            // so we need to walk through the whole element content and check if our element was removed or not.
            const range = editor.model.createRangeOn( change.position.nodeAfter );

            for ( const item of range.getItems() ) {
                if ( item.is( 'element', 'imageInline' ) ) { // Here you change the value to your element's name.
                    console.log( 'inline image removed', item ); // (**I will add a callback here**)
                }
            }
        }
    }
} );

问题是,当我试图拖放内联小部件时,它也被视为已删除.也就是说,元素正在转移到墓地,这是不应该的. 有什么方法可以防止这种情况发生吗?该占位符在拖放到其他位置时不能在墓地上移动.或者判断事件是由于拖放而生成的,而不是调用回调.

如果不可能,有任何方法可以停止内联小工具的拖放事件

推荐答案

如果任何人以后需要这个功能,你可以参考这个代码

function _getPlaceholderChanges(editor, inGraveyard) {
  const changes = editor.model.document.differ.getChanges({
    includeChangesInGraveyard: inGraveyard,
  });
  let changedItems = [];
  for (const change of changes) {
    if (
      inGraveyard
        ? change.type == "insert" &&
          change.name != "$text" &&
          change.position.root.rootName == "$graveyard"
        : change.type == "insert" && change.name != "$text"
    ) {
      const range = editor.model.createRangeOn(change.position.nodeAfter);
      for (const item of range.getItems()) {
        if (item.is("element", "placeholder")) {
          changedItems.push(item);
        }
      }
    }
  }
  const changedPlaceholders = changedItems.map((item) =>
    item._attrs.get("name")
  );
  return changedPlaceholders;
}

function pipingDeletionChecker(editor, questionId, callback) {
  const model = editor.model;
  model.document.on("change:data", () => {
    // Get changes buffered in Differ. Include items moved to the graveyard (removed from document).
    const removedPlaceHolders = _getPlaceholderChanges(editor, true);
    const addedPlaceHolders = _getPlaceholderChanges(editor, false);

    //if a placeholder deleted and added on same time means, this is happend due to reposition of item (drag and drop).
    //In this case we wont call the callback
    if (removedPlaceHolders.length && !addedPlaceHolders.length) {
      callback(questionId, removedPlaceHolders);
    }
  });
}

export default pipingDeletionChecker;

Javascript相关问答推荐

为什么子组件没有在reaction中渲染?

获取最接近的父项(即自定义元素)

如何最好地从TypScript中的enum获取值

每次子路由重定向都会调用父加载器函数

深嵌套的ng-container元素仍然可以在Angular 布局组件中正确渲染内容吗?

IMDB使用 puppeteer 加载更多按钮(nodejs)

react/redux中的formData在expressjs中返回未定义的req.params.id

Google图表时间轴—更改hAxis文本 colored颜色

Redux查询多个数据库Api reducerPath&

Regex结果包含额外的match/group,只带一个返回

在Three JS中看不到补间不透明度更改效果

如何使onPaste事件与可拖动的HTML元素一起工作?

如何将数据块添加到d3力有向图中?

如何将数组用作复合函数参数?

使用getBorbingClientRect()更改绝对元素位置

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

如何用javascript更改元素的宽度和高度?

输入的值的类型脚本array.排序()

我怎样才能得到一个数组的名字在另一个数组?

AG-GRIDreact 显示布尔值而不是复选框