一、首先分析源码
官方检索替换插件API——SearchReplace命令注册源码:
var register = function (editor, currentSearchState) {
editor.addCommand('SearchReplace', function () {
open(editor, currentSearchS);
});
};
可以看到,回调函数没有参数,即程序主动调用时,不支持将想要查询的内容赋值在SearchReplace的Modal框内。
再查看open方法源码(片段):
片段一逻辑,插件取值编辑器选中文本的纯文本值
var selectedText = global$1.trim(editor.selection.getContent({ format: 'text' }));
片段二逻辑,插件将片段一的值赋值给内置对象(此为Cell方法参数)
var initialData = {
findtext: selectedText,
replacetext: '',
wholewords: initialState.wholeWord,
matchcase: initialState.matchCase,
inselection: initialState.inSelection
};
源码总结:插件使用编辑器选中值作为初始查询条件,否则为空。
二、程序调用方法和修改源码
tinymce提供API——execCommand(cmd: string, ui?: boolean, value?: any, args?: any): boolean;
使用此api可以调用editor.addCommand注册的命令。
cmd:注册名
ui:本插件不需要
value、args:参数
ui、value、args将会传入editor.addCommand注册命令的回调函数中
现在逻辑分析清楚了,就很好修改了。
调用点:
tinymce.get(editorId).execCommand('SearchReplace', true, "我要这么搜!!!")
修改源码如下:
var register = function (editor, currentSearchState) {
editor.addCommand('SearchReplace', function (ui, value) {
open(editor, currentSearchState, value);
});
};
var open = function (editor, currentSearchState, selfText) {
var dialogApi = value();
editor.undoManager.add();
var selectedText = selfText ? selfText : global$1.trim(editor.selection.getContent({ format: 'text' }));
...
...
...
...
如果帮到你,请点个赞谢谢!