gpt4 book ai didi

javascript - Ckeditor - 在 "select"字段中加载动态字段值

转载 作者:数据小太阳 更新时间:2023-10-29 04:16:34 28 4
gpt4 key购买 nike

我正在尝试为 ckeditor 中的下拉字段加载动态输入值:

正如您在按下按钮时看到的,下拉列表中没有加载任何值:

enter image description here

我想在我的下拉列表中加载以下值(应该与 onLoad 函数中的 reged 匹配):

{{ $slot }}
{{$example }}
{{ $Product2}}
{{$category1 }}

由于该代码段不在堆栈代码段上运行,所以我在 codepen 上复制了该问题的完整工作副本。请参阅以下链接:https://codepen.io/anon/pen/NBXObP

我的插件代码如下所示:

var selectedList = []

CKEDITOR.replace("editor", {
extraPlugins: "insertData"
});

CKEDITOR.plugins.add( 'insertData', {

icons: '',
init: function( editor ) {

editor.addCommand( 'insertData', new CKEDITOR.dialogCommand( 'insertDataDialog' ) );
editor.ui.addButton( 'InsertData', {
label: 'Insert InsertData',
command: 'insertData',
toolbar: 'insert'
});

if ( editor.contextMenu ) {
editor.addMenuGroup( 'insertDataGroup' );
editor.addMenuItem( 'insertDataItem', {
label: 'Edit InsertData',
icon: this.path + 'icons/insertData.png',
command: 'insertData',
group: 'insertDataGroup'
});

editor.contextMenu.addListener( function( element ) {
if ( element.getAscendant( 'insertData', true ) ) {
return { insertDataItem: CKEDITOR.TRISTATE_OFF };
}
});
}

CKEDITOR.dialog.add( 'insertDataDialog', function (editor) {
return {

// Basic properties of the dialog window: title, minimum size.
title: 'InsertData Properties',
minWidth: 400,
minHeight: 200,

// Dialog window content definition.
contents: [{
// Definition of the Basic Settings dialog tab (page).
id: 'tab-basic',
label: 'Basic Settings',

// The tab content.
elements: [{
// Text input field for the insertData text.
type: 'select',
id: 'insertData',
label: 'Element',
items: selectedList,
'default': '',

onLoad: function (widget) {
var text = CKEDITOR.instances.editor.getData();
var selectedList = text.match(/{{\s*\$\w+\s*}}/g)
console.log("text: " + text)
console.log("selectedList: " + selectedList)
},

onChange: function (api) {
alert('Current value: ' + this.getValue());
}
},
{
type: 'text',
id: 'title',
label: 'InsertDatas',
validate: CKEDITOR.dialog.validate.notEmpty("InsertDatas field cannot be empty."),

setup: function (element) {
this.setValue(element.getAttribute("title"));
},

commit: function (element) {
element.setAttribute("title", this.getValue());
}
}

]
},
],

onShow: function () {

var selection = editor.getSelection();
var element = selection.getStartElement();

if (element)
element = element.getAscendant('insertData', true);

if (!element || element.getName() != 'insertData') {
element = editor.document.createElement('insertData');

this.insertMode = true;
} else
this.insertMode = false;

this.element = element;
this.setupContent(this.element);
},

onOk: function () {
var insertData = this.element;

this.commitContent(insertData);
if (this.insertMode)
editor.insertElement(insertData);
}
};
});
}
});

为什么下拉列表中的字段没有加载?

感谢您的回复!

最佳答案

您忘记调用 .add()在插件对话框的 onLoad 回调中选择:

selectedList.forEach((item) => {
this.add(item);
});

或者如果 ES6 不可用:

var _self = this;
selectedList.forEach(function(item) {
_self.add(item);
});

修改后的代码笔:https://codepen.io/anon/pen/pZaVXz?editors=1010

关于javascript - Ckeditor - 在 "select"字段中加载动态字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574911/

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