📄 editor2.js
字号:
/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml*//* TODO: * - font selector * - test, bug fix, more features :)*/dojo.provide("dojo.widget.Editor2");dojo.require("dojo.io.*");dojo.require("dojo.html.*");dojo.require("dojo.html.layout");dojo.require("dojo.widget.*");dojo.require("dojo.widget.RichText");dojo.require("dojo.widget.Editor2Toolbar");// Object: Manager of current focused Editor2 Instance and available editor2 commandsdojo.widget.Editor2Manager = { _currentInstance: null, _loadedCommands: {}, // Object: state a command may be in commandState: {Disabled: 0, Latched: 1, Enabled: 2}, getCurrentInstance: function(){ // summary: Return the current focused Editor2 instance return this._currentInstance; }, setCurrentInstance: function(/*Widget*/inst){ // summary: Set current focused Editor2 instance this._currentInstance = inst; }, registerCommand: function(/*String*/name, /*Object*/cmd){ // summary: Register an Editor2 command // name: name of the command (case insensitive) // cmd: an object which implements interface dojo.widget.Editor2Command name = name.toLowerCase(); if(this._loadedCommands[name]){ delete this._loadedCommands[name]; } this._loadedCommands[name] = cmd; }, getCommand: function(/*String*/name){ // summary: Return Editor2 command with the given name // name: name of the command (case insensitive) name = name.toLowerCase(); var oCommand = this._loadedCommands[name]; if(oCommand){ return oCommand; } switch(name){ case 'htmltoggle': //Editor2 natively provide the htmltoggle functionalitity //and it is treated as a builtin command oCommand = new dojo.widget.Editor2BrowserCommand(name); break; case 'formatblock': oCommand = new dojo.widget.Editor2FormatBlockCommand(name); break; case 'anchor': oCommand = new dojo.widget.Editor2Command(name); break; //dialog command case 'createlink': oCommand = new dojo.widget.Editor2DialogCommand(name, {contentFile: "dojo.widget.Editor2Plugin.CreateLinkDialog", contentClass: "Editor2CreateLinkDialog", title: "Insert/Edit Link", width: "300px", height: "200px"}); break; case 'insertimage': oCommand = new dojo.widget.Editor2DialogCommand(name, {contentFile: "dojo.widget.Editor2Plugin.InsertImageDialog", contentClass: "Editor2InsertImageDialog", title: "Insert/Edit Image", width: "400px", height: "270px"}); break; // By default we assume that it is a builtin simple command. default: var curtInst = this.getCurrentInstance(); if((curtInst && curtInst.queryCommandAvailable(name)) || (!curtInst && dojo.widget.Editor2.prototype.queryCommandAvailable(name))){ oCommand = new dojo.widget.Editor2BrowserCommand(name); }else{ dojo.debug("dojo.widget.Editor2Manager.getCommand: Unknown command "+name); return; } } this._loadedCommands[name] = oCommand; return oCommand; }, destroy: function(){ // summary: Cleaning up. This is called automatically on page unload. this._currentInstance = null; for(var cmd in this._loadedCommands){ this._loadedCommands[cmd].destory(); } }};dojo.addOnUnload(dojo.widget.Editor2Manager, "destroy");// summary:// dojo.widget.Editor2Command is the base class for all command in Editor2dojo.lang.declare("dojo.widget.Editor2Command",null,{ initializer: function(name){ // summary: Constructor of this class this._name = name; }, //this function should be re-implemented in subclass execute: function(para){ // summary: Execute the command. should be implemented in subclass dojo.unimplemented("dojo.widget.Editor2Command.execute"); }, //default implemetation always returns Enabled getState: function(){ // summary: // Return the state of the command. The default behavior is // to always return Enabled return dojo.widget.Editor2Manager.commandState.Enabled; }, destory: function(){ // summary: Destructor } });// summary:// dojo.widget.Editor2BrowserCommand is the base class for all the browser built// in commandsdojo.lang.declare("dojo.widget.Editor2BrowserCommand", dojo.widget.Editor2Command, { execute: function(para){ var curInst = dojo.widget.Editor2Manager.getCurrentInstance(); if(curInst){ curInst.execCommand(this._name, para); } }, getState: function(){ var curInst = dojo.widget.Editor2Manager.getCurrentInstance(); if(curInst){ try{ if(curInst.queryCommandEnabled(this._name)){ if(curInst.queryCommandState(this._name)){ return dojo.widget.Editor2Manager.commandState.Latched; }else{ return dojo.widget.Editor2Manager.commandState.Enabled; } }else{ return dojo.widget.Editor2Manager.commandState.Disabled; } }catch (e) { //dojo.debug("exception when getting state for command "+this._name+": "+e); return dojo.widget.Editor2Manager.commandState.Enabled; } } return dojo.widget.Editor2Manager.commandState.Disabled; }, getValue: function(){ var curInst = dojo.widget.Editor2Manager.getCurrentInstance(); if(curInst){ try{ return curInst.queryCommandValue(this._name); }catch(e){} } } });dojo.lang.declare("dojo.widget.Editor2FormatBlockCommand", dojo.widget.Editor2BrowserCommand, { /* In none-ActiveX mode under IE, <p> and no <p> text can not be distinguished getCurrentValue: function(){ var curInst = dojo.widget.Editor2Manager.getCurrentInstance(); if(!curInst){ return ''; } var h = dojo.render.html; // safari f's us for selection primitives if(h.safari){ return ''; } var selectedNode = (h.ie) ? curInst.document.selection.createRange().parentElement() : curInst.window.getSelection().anchorNode; // make sure we actuall have an element while((selectedNode)&&(selectedNode.nodeType != 1)){ selectedNode = selectedNode.parentNode; } if(!selectedNode){ return ''; } var formats = ["p", "pre", "h1", "h2", "h3", "h4", "h5", "h6", "address"]; // gotta run some specialized updates for the various // formatting options var type = formats[dojo.lang.find(formats, selectedNode.nodeName.toLowerCase())]; while((selectedNode!=curInst.editNode)&&(!type)){ selectedNode = selectedNode.parentNode; if(!selectedNode){ break; } type = formats[dojo.lang.find(formats, selectedNode.nodeName.toLowerCase())]; } if(!type){ type = ""; } return type; }*/ });dojo.require("dojo.widget.FloatingPane");// summary:// dojo.widget.Editor2Dialog provides a Dialog which can be modal or normal for the Editor2.dojo.widget.defineWidget( "dojo.widget.Editor2Dialog", [dojo.widget.HtmlWidget, dojo.widget.FloatingPaneBase, dojo.widget.ModalDialogBase], { templatePath: dojo.uri.dojoUri("src/widget/templates/Editor2/EditorDialog.html"), // Boolean: Whether this is a modal dialog. True by default. modal: true,// refreshOnShow: true, //for debug for now // String: Wwidth of the dialog. None by default width: false, // String: Height of the dialog. None by default height: false, // String: startup state of the dialog windowState: "minimized", displayCloseAction: true, contentFile: "", contentClass: "", fillInTemplate: function(args, frag){ this.fillInFloatingPaneTemplate(args, frag); dojo.widget.Editor2Dialog.superclass.fillInTemplate.call(this, args, frag); }, postCreate: function(){ if(this.contentFile){ dojo.require(this.contentFile); } if(this.modal){ dojo.widget.ModalDialogBase.prototype.postCreate.call(this); }else{ with(this.domNode.style) { zIndex = 999; display = "none"; } } dojo.widget.FloatingPaneBase.prototype.postCreate.apply(this, arguments); dojo.widget.Editor2Dialog.superclass.postCreate.call(this); if(this.width && this.height){ with(this.domNode.style){ width = this.width; height = this.height; } } }, createContent: function(){ if(!this.contentWidget && this.contentClass){ this.contentWidget = dojo.widget.createWidget(this.contentClass); this.addChild(this.contentWidget); } }, show: function(){ if(!this.contentWidget){ //buggy IE: if the dialog is hidden, the button widgets //in the dialog can not be shown, so show it temporary (as the //dialog may decide not to show it in loadContent() later) dojo.widget.Editor2Dialog.superclass.show.apply(this, arguments); this.createContent(); dojo.widget.Editor2Dialog.superclass.hide.call(this); } if(!this.contentWidget || !this.contentWidget.loadContent()){ return; } this.showFloatingPane(); dojo.widget.Editor2Dialog.superclass.show.apply(this, arguments); if(this.modal){ this.showModalDialog(); } this.placeModalDialog(); if(this.modal){ //place the background div under this modal pane this.shared.bg.style.zIndex = this.domNode.style.zIndex-1; } }, onShow: function(){ dojo.widget.Editor2Dialog.superclass.onShow.call(this); this.onFloatingPaneShow(); }, closeWindow: function(){ this.hide(); dojo.widget.Editor2Dialog.superclass.closeWindow.apply(this, arguments); }, hide: function(){ if(this.modal){ this.hideModalDialog(); } dojo.widget.Editor2Dialog.superclass.hide.call(this); } });// summary:// dojo.widget.Editor2DialogContent is the actual content of a Editor2Dialog.// This class should be subclassed to provide the content.dojo.widget.defineWidget( "dojo.widget.Editor2DialogContent", dojo.widget.HtmlWidget,{ widgetsInTemplate: true, loadContent:function(){ // summary: Load the content. Called by Editor2Dialog when first shown return true; }, cancel: function(){ // summary: Default handler when cancel button is clicked. this.parent.hide(); }});// summary:// dojo.widget.Editor2DialogCommand provides an easy way to popup a dialog when// the command is executed.dojo.lang.declare("dojo.widget.Editor2DialogCommand", dojo.widget.Editor2BrowserCommand, function(name, dialogParas){ this.dialogParas = dialogParas; },
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -