📄 kupubasetools.js
字号:
/***************************************************************************** * * Copyright (c) 2003-2004 Kupu Contributors. All rights reserved. * * This software is distributed under the terms of the Kupu * License. See LICENSE.txt for license text. For a list of Kupu * Contributors see CREDITS.txt. * *****************************************************************************/// $Id: kupubasetools.js,v 1.1 2005/03/26 20:31:36 svieujot Exp $//----------------------------------------------------------------------------//// Toolboxes//// These are addons for Kupu, simple plugins that implement a certain // interface to provide functionality and control view aspects.////----------------------------------------------------------------------------//----------------------------------------------------------------------------// Superclasses//----------------------------------------------------------------------------function KupuTool() { /* Superclass (or actually more of an interface) for tools Tools must implement at least an initialize method and an updateState method, and can implement other methods to add certain extra functionality (e.g. createContextMenuElements). */ this.toolboxes = {}; // methods this.initialize = function(editor) { /* Initialize the tool. Obviously this can be overriden but it will do for the most simple cases */ this.editor = editor; }; this.registerToolBox = function(id, toolbox) { /* register a ui box Note that this needs to be called *after* the tool has been registered to the KupuEditor */ this.toolboxes[id] = toolbox; toolbox.initialize(this, this.editor); }; this.updateState = function(selNode, event) { /* Is called when user moves cursor to other element Calls the updateState for all toolboxes and may want perform some actions itself */ for (id in this.toolboxes) { this.toolboxes[id].updateState(selNode, event); }; }; // private methods addEventHandler = addEventHandler; this._selectSelectItem = function(select, item) { this.editor.logMessage('Deprecation warning: KupuTool._selectSelectItem'); }; this._fixTabIndex = function(element) { var tabIndex = this.editor.getDocument().getEditable().tabIndex-1; if (tabIndex && !element.tabIndex) { element.tabIndex = tabIndex; } }}function KupuToolBox() { /* Superclass for a user-interface object that controls a tool */ this.initialize = function(tool, editor) { /* store a reference to the tool and the editor */ this.tool = tool; this.editor = editor; }; this.updateState = function(selNode, event) { /* update the toolbox according to the current iframe's situation */ }; this._selectSelectItem = function(select, item) { this.editor.logMessage('Deprecation warning: KupuToolBox._selectSelectItem'); };};function NoContextMenu(object) { /* Decorator for a tool to suppress the context menu */ object.createContextMenuElements = function(selNode, event) { return []; } return object;}//----------------------------------------------------------------------------// Implementations//----------------------------------------------------------------------------function KupuButton(buttonid, commandfunc, tool) { /* Base prototype for kupu button tools */ this.button = window.document.getElementById(buttonid); this.commandfunc = commandfunc; this.tool = tool; this.initialize = function(editor) { this.editor = editor; this._fixTabIndex(this.button); addEventHandler(this.button, 'click', this.execCommand, this); }; this.execCommand = function() { /* exec this button's command */ this.commandfunc(this, this.editor, this.tool); this.editor.focusDocument(); }; this.updateState = function(selNode, event) { /* override this in subclasses to determine whether a button should look 'pressed in' or not */ };};KupuButton.prototype = new KupuTool;function KupuStateButton(buttonid, commandfunc, checkfunc, offclass, onclass) { /* A button that can have two states (e.g. pressed and not-pressed) based on CSS classes */ this.button = window.document.getElementById(buttonid); this.commandfunc = commandfunc; this.checkfunc = checkfunc; this.offclass = offclass; this.onclass = onclass; this.pressed = false; this.execCommand = function() { /* exec this button's command */ this.commandfunc(this, this.editor); this.button.className = (this.pressed ? this.offclass : this.onclass); this.pressed = !this.pressed; this.editor.focusDocument(); }; this.updateState = function(selNode, event) { /* check if we need to be clicked or unclicked, and update accordingly if the state of the button should be changed, we set the class */ var currclass = this.button.className; var newclass = null; if (this.checkfunc(selNode, this, this.editor, event)) { newclass = this.onclass; this.pressed = true; } else { newclass = this.offclass; this.pressed = false; }; if (currclass != newclass) { this.button.className = newclass; }; };};KupuStateButton.prototype = new KupuButton;function KupuRemoveElementButton(buttonid, element_name, cssclass) { /* A button specialized in removing elements in the current node context. Typical usages include removing links, images, etc. */ this.button = window.document.getElementById(buttonid); this.onclass = 'invisible'; this.offclass = cssclass; this.pressed = false; this.commandfunc = function(button, editor) { editor.removeNearestParentOfType(editor.getSelectedNode(), element_name); }; this.checkfunc = function(currnode, button, editor, event) { var element = editor.getNearestParentOfType(currnode, element_name); return (element ? false : true); };};KupuRemoveElementButton.prototype = new KupuStateButton;function KupuUI(textstyleselectid) { /* View This is the main view, which controls most of the toolbar buttons. Even though this is probably never going to be removed from the view, it was easier to implement this as a plain tool (plugin) as well. */ // attributes this.tsselect = document.getElementById(textstyleselectid); this.initialize = function(editor) { /* initialize the ui like tools */ this.editor = editor; this._fixTabIndex(this.tsselect); addEventHandler(this.tsselect, 'change', this.setTextStyleHandler, this); }; this.setTextStyleHandler = function(event) { this.setTextStyle(this.tsselect.options[this.tsselect.selectedIndex].value); }; // event handlers this.basicButtonHandler = function(action) { /* event handler for basic actions (toolbar buttons) */ this.editor.execCommand(action); this.editor.updateState(); }; this.saveButtonHandler = function() { /* handler for the save button */ this.editor.saveDocument(); }; this.saveAndExitButtonHandler = function(redirect_url) { /* save the document and, if successful, redirect */ this.editor.saveDocument(redirect_url); }; this.cutButtonHandler = function() { try { this.editor.execCommand('Cut'); } catch (e) { if (this.editor.getBrowserName() == 'Mozilla') { alert('Cutting from JavaScript is disabled on your Mozilla due to security settings. For more information, read http://www.mozilla.org/editor/midasdemo/securityprefs.html'); } else { throw e; }; }; this.editor.updateState(); }; this.copyButtonHandler = function() { try { this.editor.execCommand('Copy'); } catch (e) { if (this.editor.getBrowserName() == 'Mozilla') { alert('Copying from JavaScript is disabled on your Mozilla due to security settings. For more information, read http://www.mozilla.org/editor/midasdemo/securityprefs.html'); } else { throw e; }; }; this.editor.updateState(); }; this.pasteButtonHandler = function() { try { this.editor.execCommand('Paste'); } catch (e) { if (this.editor.getBrowserName() == 'Mozilla') { alert('Pasting from JavaScript is disabled on your Mozilla due to security settings. For more information, read http://www.mozilla.org/editor/midasdemo/securityprefs.html'); } else { throw e; }; }; this.editor.updateState(); }; this.setTextStyle = function(style) { /* method for the text style pulldown */ // XXX Yuck!! if (this.editor.getBrowserName() == "IE") { style = '<' + style + '>'; }; this.editor.execCommand('formatblock', style); }; this.updateState = function(selNode) { /* set the text-style pulldown */ // first get the nearest style var styles = {}; // use an object here so we can use the 'in' operator later on for (var i=0; i < this.tsselect.options.length; i++) { // XXX we should cache this styles[this.tsselect.options[i].value.toUpperCase()] = i; } var currnode = selNode; var index = 0; while (currnode) { if (currnode.nodeName.toUpperCase() in styles) { index = styles[currnode.nodeName.toUpperCase()]; break } currnode = currnode.parentNode; } this.tsselect.selectedIndex = index; }; this.createContextMenuElements = function(selNode, event) { var ret = new Array(); ret.push(new ContextMenuElement('Cut', this.cutButtonHandler, this)); ret.push(new ContextMenuElement('Copy', this.copyButtonHandler, this)); ret.push(new ContextMenuElement('Paste', this.pasteButtonHandler, this)); return ret; };}KupuUI.prototype = new KupuTool;function ColorchooserTool(fgcolorbuttonid, hlcolorbuttonid, colorchooserid) { /* the colorchooser */ this.fgcolorbutton = document.getElementById(fgcolorbuttonid); this.hlcolorbutton = document.getElementById(hlcolorbuttonid); this.ccwindow = document.getElementById(colorchooserid); this.command = null; this.initialize = function(editor) { /* attach the event handlers */ this.editor = editor; this.createColorchooser(this.ccwindow); addEventHandler(this.fgcolorbutton, "click", this.openFgColorChooser, this); addEventHandler(this.hlcolorbutton, "click", this.openHlColorChooser, this); addEventHandler(this.ccwindow, "click", this.chooseColor, this); this.hide(); this.editor.logMessage('Colorchooser tool initialized'); }; this.updateState = function(selNode) { /* update state of the colorchooser */ this.hide(); }; this.openFgColorChooser = function() { /* event handler for opening the colorchooser */ this.command = "forecolor"; this.show(); };
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -