简体   繁体   English

更改新窗口小部件在CKEDITOR中的位置

[英]Change position of new widget in CKEDITOR

I click to the table on 'Smith' text 我点击表格上的“史密斯”文字

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
  </tr>
</table>

and I call method: 我调用方法:

var selection = editor.getSelection();
var element = new CKEDITOR.dom.element('span');
editor.insertElement(element);
widget = editor.widget.initOn(element, 'myWidget');

And it will create new widget on exact position, where I click (next to text 'Smith'). 它将在我单击的确切位置上创建新的小部件(在文本“ Smith”旁边)。

But I want to create this widget first cell in corespondent row. 但是我想在corespondent行中创建此小部件的第一个单元格。

Output should be something like: 输出应该是这样的:

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
  </tr>
  <tr>
    <td><span>myWidget</span>Jill</td>
    <td>Smith</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
  </tr>
</table>

How can I achieve that ? 我该如何实现?

You can either move selection before insertElement to position where actually you wish to insert the element. 您可以将选择insertElement之前,将其放置在实际希望插入元素的位置。 Insert element method use current selection as place to insert. 插入元素方法使用当前选择作为插入位置。 To do so you need to create new range, add proper element to range (as startNode), use collapse selection, and then make selection from range. 为此,您需要创建新范围,将适当的元素添加到范围(作为startNode),使用合拢选择,然后从范围中进行选择。 Right now you should have selection in place where element should be inserted. 现在,您应该在应该插入元素的位置进行选择。

Another option (for me a little bit more convenient) might be replace insertElement with method which place widget in proper place. 另一个选择(对我来说更方便一些)可能是将insertElement替换为将小部件放置在适当位置的方法。 It supposed to works sth like: 它应该像这样工作:

var selection = editor.getSelection();
var element = new CKEDITOR.dom.element( 'span' );
var firstCell = selection.getStartElement().getAscendant( 'tr' ).getFirst();
firstCell.append( element, true );

Of course code is dedicated for tables only, as tr is searched in ascendant nodes. 当然,由于tr是在上升节点中搜索的,因此代码仅用于表。 I've done this in such way to prevent of situation that selection is inside some bold, linked, emphasized text. 我这样做的目的是为了避免出现在粗体,链接,强调的文本中进行选择的情况。 So parent doesn't have to be a td . 因此,父母不必一定是td More element methods you can find in documentation: https://docs.ckeditor.com/ckeditor4/latest/api 您可以在文档中找到更多元素方法: https : //docs.ckeditor.com/ckeditor4/latest/api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM