SubEditor will render itself in the following format:
<div class="SubEditor"> <div class="SubEditorToolbar"></div> <div class="SubEditorContentContainer"> <div class="SubEditorContent"></div> </div> <div class="SubEditorFooter"></div> <textarea class="SubEditorTextarea"></textarea> </div>
dom element of SubEditor.
var subeditor = new SubEditor(elm, options); console.log(document.querySelector(".SubEditor") === subeditor.refEditor); //true
The shadow textarea that contains the content for source editing, the value is updated as the editor content changes. If a textarea element is provide to initialize SubEditor, refTextarea will be same as the initialized element.
var subeditor = new SubEditor(elm, options); console.log(document.querySelector(".SubEditorTextarea") === subeditor.refTextarea); //true if(elm.nodeName === "TEXTAREA") { console.log(elm === subeditor.refTextarea); //true }
dom element of toobar.
var subeditor = new SubEditor(elm, options); console.log(document.querySelector(".SubEditorToolbar") === subeditor.refToolbar); //true
dom element of content.
var subeditor = new SubEditor(elm, options); console.log(document.querySelector(".SubEditorContent") === subeditor.refContent); //true
dom element of footer.
var subeditor = new SubEditor(elm, options); console.log(document.querySelector(".SubEditorFooter") === subeditor.refFooter); //true
current version (static). In case of breaking changes, plugin developer can use version to determine behaviors base on different versions.
console.log(SubEditor.version); //0.6.0
list of svg icons for ui (static). Icons can be added or replaced by editing this value directly or using the method SubEditor.presetSvg. Changing this will affect all instances of SubEditor.
console.log(SubEditor.svgList); // { i : "<svg></svg>" ....}
list of translation languages (static). Languages can be added or updated by editing this value directly or using the method SubEditor.presetLang. Changing this will affect all instances of SubEditor.
console.log(SubEditor.langList); // { en : {"bold" : "bold",...} ....}
This method takes a function that accepts an editor instance and returns a ToolbarItem. Item set by presetToolbarItem will have the highest priority, it shall replace the item set by plugin (registerToolbarItem) and default item with same name.
list of user defined plugins (static), available globally and shared across all instances. Using same name will overwrite the default plugin. Use the method SubEditor.presetPlugin(pluginName : string, plugin : SubEditorEvent[]).
user defined css in string format (static), available globally and shared across all instances. Use the method SubEditor.presetCss(cssString : string).
Stores config variables for plugins.
Stores temporary variables, to be passed between plugins or in UI flow.
Serialized value of current selection, this is updated on every selection change. The serialized object uses Selection Serializer.
An analysed dom object representation at current cursor. Default null and will change upon selection change.
class Feature { path : string[] = []; pathNode : HTMLElement[] = []; node : HTMLElement | undefined = undefined; nodeName : string = ""; formatEL : string = ""; color : string = ""; background : string = ""; indent : string = ""; align : string = ""; bold = false; italic = false; underline = false; strikethrough = false; subscript = false; superscript = false; a : FeatureLink = { href : "", target : "", node : undefined, }; img : FeatureImage = { src : "", width : "", height : "", node : undefined }; video? : FeatureVideo = { controls : boolean, autoplay : boolean, width : "", height : "", node : HTMLElement | undefined, sources : FeatureSource[] }; audio? : FeatureAudio = { controls : boolean, autoplay : boolean, width : "", height : "", node : HTMLElement | undefined, sources : FeatureSource[] }; }
method: non-static params: (type : string, value : any[] = [])
trigger plugin command.
var subeditor = new SubEditor(elm, options); subeditor.command("hr",[]); //add an hr into content
method: non-static params: none
destroy and clean up current instance.
var subeditor = new SubEditor(elm, options); subeditor.destroy();
method: non-static params: (key : string)
return the translated language value.
var subeditor = new SubEditor(elm, {lang : "zhCN"}); console.log(subeditor.ln("exit fullscreen")); //退出全屏
method: non-static params: (key : string, fn : Function)
register callback function for use later.
var subeditor = new SubEditor(elm, options); subeditor.registerCallback("logme", (str) => { console.log(str);});
method: non-static params: (key : string, args : any = undefined)
var subeditor = new SubEditor(elm, options); subeditor.getCallback("logme", "output to console"); //output to console
method: non-static params: (plugins : (SubEditorEvent[] | string)[])
add plugin to the editor after initialization, available only to current instance. Use string value to enable default preset, or include your own plugin code.
var subeditor = new SubEditor(elm, options); //enable default redo plugin: subeditor.initPlugins(["redo"]); //or register a "redo" command with full code, please note that you should not register same event twice editor.initPlugins([ { event : "onCommand", target : ["redo"], callback : (editor :SubEditor, type : string, value : any) => { editor.handleChange(editor.history.Redo()); } }]);
method: static params: (pluginName : string, plugin : SubEditorEvent[])
pre-define a plugin available globally and share among all instances. Using the same name will replace default plugin. List of all default plugins are: "fullscreen","hr", "color","source","align","text","undo","redo","indent","format","remove_format","link", "paste","list", "table","image"
Parse the current selection state of editor content.
var subeditor = new SubEditor(elm, options); subeditor.handleFeature(); console.log(subeditor.feature); /* null or: Feature { path : string[] = []; pathNode : HTMLElement[] = []; node : HTMLElement | undefined = undefined; nodeName : string = ""; formatEL : string = ""; color : string = ""; background : string = ""; indent : string = ""; align : string = ""; bold = false; italic = false; underline = false; strikethrough = false; subscript = false; superscript = false; a : FeatureLink = { href : "", target : "", node : undefined, }; img : FeatureImage = { src : "", width : "", height : "", node : undefined }; video? : FeatureVideo = { controls : boolean, autoplay : boolean, width : "", height : "", node : HTMLElement | undefined, sources : FeatureSource[] }; audio? : FeatureAudio = { controls : boolean, autoplay : boolean, width : "", height : "", node : HTMLElement | undefined, sources : FeatureSource[] }; } */
method: non-static params: (sel : SelectionSlimState | undefined = undefined)
restore the selection to the provided serialized selection state.
var subeditor = new SubEditor(elm, options); subeditor.changeValue("<h1>title</h1>"); var cachedSelection = editor.selection; //assuming cursor or focus has moved, reset it back to cached subeditor.restoreSelection(cachedSelection);
return the selection and range.
var subeditor = new SubEditor(elm, options); console.log(subeditor.getSelectionRange()); // {selection : object, range : object}
method: non-static params: (key : string, value : any)
save variable for use later.
var subeditor = new SubEditor(elm, options); subeditor.setCache("myVar", "test123");
get cached variable.
console.log(subeditor.getCache("myVar")); //test123
set or change config variable.
var subeditor = new SubEditor(elm, options); //limit the number of files to be handled in image upload subeditor.setCfg("image.upload.accept.files", 1);
get current cfg variable.
console.log(subeditor.getCache("image.upload.accept.files")); //1
method: non-static params: (height: number)
disable and hide footer
var subeditor = new SubEditor(elm, options); subeditor.enableFooter(15); //show a footer of height = 15
var subeditor = new SubEditor(elm, options); subeditor.disableFooter();
method: non-static params: (grow : boolean)
enable or disable content height auto grow
var subeditor = new SubEditor(elm, options); subeditor.setAutoGrow(true);
method: static params: (langList : {[key: string]:{[key: string]: string}})
add or change language, this will affect all instances.
SubEditor.presetLang({ fr : { //some translations } });
method: static params: (_svg : {[key: string]: string})
add or change svg icon, this will affect all instances.
SubEditor.presetSvg({ italic : '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M12 9l-2 10h2l2-10Zm0.75-3a1 1 0 001 1a1 1 0 10-1-1Z"></path></svg>' });
method: static params: (cssString: string)
Css will inject into document head upon each initialization. Css will take the following order:
Styles are shared among all instances, initialization will refresh the style if it detect changes.
//replace existing preset css string SubEditor.presetCss(".SubEditorContent {border:1px solid red}"); //or if you already have some defined before but want to append more: SubEditor.presetCss(SubEditor.presetCssString + ".SubEditorContent {border:1px solid red}");
method: static params: none
Return the last computed css style in string format.
method: non-static params: (str : string)
change the content.
var subeditor = new SubEditor(elm, options); subeditor.changeValue("<h1>New Content</h1>"); console.log(subeditor.value()); //<h1>New Content</h1>
if content is modified in code, triggerChange will need to be called to create a changed copy.
var subeditor = new SubEditor(elm, {onChange : (changed) => {console.log("changed: ",changed);}}); subeditor.changeValue("<h1>New Content</h1>"); subeditor.triggerChange(); //changed: {key : 1, content : '<h1>New Content</h1>', change : {delta : array, selection : object}}
return the content value in string.
var subeditor = new SubEditor(elm, options); console.log(subeditor.value());