📄 xquared.js
字号:
if(!text) return; this.rdom.deleteSelection(); var a = this.rdom.createElement('A'); a.href = url; a.title = title; a.appendChild(this.rdom.createTextNode(text)); this.rdom.insertNode(a); } var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Called when enter key pressed. * * @param {boolean} skipAutocorrection if set true, skips autocorrection * @param {boolean} forceInsertParagraph if set true, inserts paragraph */ handleEnter: function(skipAutocorrection, forceInsertParagraph) { // If it has selection, perform default action. if(this.rdom.hasSelection()) return false; // Perform autocorrection if(!skipAutocorrection && this.handleAutocorrection()) return true; var atEmptyBlock = this.rdom.isCaretAtEmptyBlock(); var atStart = atEmptyBlock || this.rdom.isCaretAtBlockStart(); var atEnd = atEmptyBlock || (!atStart && this.rdom.isCaretAtBlockEnd()); var atEdge = atEmptyBlock || atStart || atEnd; if(!atEdge) { var block = this.rdom.getCurrentBlockElement(); var marker = this.rdom.pushMarker(); if(this.rdom.isFirstLiWithNestedList(block) && !forceInsertParagraph) { var parent = block.parentNode; this.rdom.unwrapElement(block); block = parent; } else if(block.nodeName != "LI" && this.rdom.tree.isBlockContainer(block)) { block = this.rdom.wrapAllInlineOrTextNodesAs("P", block, true).first(); } this.rdom.splitElementUpto(marker, block); this.rdom.popMarker(true); } else if(atEmptyBlock) { this._handleEnterAtEmptyBlock(); } else { this._handleEnterAtEdge(atStart, forceInsertParagraph); } return true; }, /** * Moves current block upward or downward * * @param {boolean} up moves current block upward */ handleMoveBlock: function(up) { var block = this.rdom.moveBlock(this.rdom.getCurrentBlockElement(), up); if(block) { this.rdom.selectElement(block, false); block.scrollIntoView(false); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); } return true; }, /** * Called when tab key pressed */ handleTab: function() { var hasSelection = this.rdom.hasSelection(); var table = this.rdom.getParentElementOf(this.rdom.getCurrentBlockElement(), ["TABLE"]); if(hasSelection) { this.handleIndent(); } else if (table && table.className == "datatable") { this.handleMoveToNextCell(); } else if (this.rdom.isCaretAtBlockStart()) { this.handleIndent(); } else { this.handleInsertTab(); } return true; }, /** * Called when shift+tab key pressed */ handleShiftTab: function() { var hasSelection = this.rdom.hasSelection(); var table = this.rdom.getParentElementOf(this.rdom.getCurrentBlockElement(), ["TABLE"]); if(hasSelection) { this.handleOutdent(); } else if (table && table.className == "datatable") { this.handleMoveToPreviousCell(); } else { this.handleOutdent(); } return true; }, /** * Inserts three non-breaking spaces */ handleInsertTab: function() { this.rdom.insertHtml(' '); this.rdom.insertHtml(' '); this.rdom.insertHtml(' '); return true; }, /** * Called when delete key pressed */ handleDelete: function() { if(this.rdom.hasSelection() || !this.rdom.isCaretAtBlockEnd()) return false; return this._handleMerge(true); }, /** * Called when backspace key pressed */ handleBackspace: function() { if(this.rdom.hasSelection() || !this.rdom.isCaretAtBlockStart()) return false; return this._handleMerge(false); }, _handleMerge: function(withNext) { var block = this.rdom.getCurrentBlockElement(); // save caret position; var marker = this.rdom.pushMarker(); // perform merge var merged = this.rdom.mergeElement(block, withNext, withNext); if(!merged && !withNext) this.rdom.extractOutElementFromParent(block); // restore caret position this.rdom.popMarker(true); if(merged) this.rdom.correctEmptyElement(merged); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return !!merged; }, /** * (in table) Moves caret to the next cell */ handleMoveToNextCell: function() { this._handleMoveToCell("next"); }, /** * (in table) Moves caret to the previous cell */ handleMoveToPreviousCell: function() { this._handleMoveToCell("prev"); }, /** * (in table) Moves caret to the above cell */ handleMoveToAboveCell: function() { this._handleMoveToCell("above"); }, /** * (in table) Moves caret to the below cell */ handleMoveToBelowCell: function() { this._handleMoveToCell("below"); }, _handleMoveToCell: function(dir) { var block = this.rdom.getCurrentBlockElement(); var cell = this.rdom.getParentElementOf(block, ["TD", "TH"]); var table = this.rdom.getParentElementOf(cell, ["TABLE"]); var rtable = new xq.RichTable(this.rdom, table); var target = null; if(["next", "prev"].indexOf(dir) != -1) { var toNext = dir == "next"; target = toNext ? rtable.getNextCellOf(cell) : rtable.getPreviousCellOf(cell); } else { var toBelow = dir == "below"; target = toBelow ? rtable.getBelowCellOf(cell) : rtable.getAboveCellOf(cell); } if(!target) { var finder = function(node) {return ['TD', 'TH'].indexOf(node.nodeName) == -1 && this.tree.isBlock(node) && !this.tree.hasBlocks(node);}.bind(this.rdom); var exitCondition = function(node) {return this.tree.isBlock(node) && !this.tree.isDescendantOf(this.getRoot(), node)}.bind(this.rdom); target = (toNext || toBelow) ? this.rdom.tree.findForward(cell, finder, exitCondition) : this.rdom.tree.findBackward(table, finder, exitCondition); } if(target) this.rdom.placeCaretAtStartOf(target); }, /** * Applies STRONG tag */ handleStrongEmphasis: function() { this.rdom.applyStrongEmphasis(); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Applies EM tag */ handleEmphasis: function() { this.rdom.applyEmphasis(); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Applies EM.underline tag */ handleUnderline: function() { this.rdom.applyUnderline(); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Applies SPAN.strike tag */ handleStrike: function() { this.rdom.applyStrike(); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Removes all style */ handleRemoveFormat: function() { this.rdom.applyRemoveFormat(); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Inserts table * * @param {Number} cols number of columns * @param {Number} rows number of rows * @param {String} headerPosition position of THs. "T" or "L" or "TL". "T" means top, "L" means left. */ handleTable: function(cols, rows, headerPositions) { var cur = this.rdom.getCurrentBlockElement(); if(this.rdom.getParentElementOf(cur, ["TABLE"])) return true; var rtable = xq.RichTable.create(this.rdom, cols, rows, headerPositions); if(this.rdom.tree.isBlockContainer(cur)) { var wrappers = this.rdom.wrapAllInlineOrTextNodesAs("P", cur, true); cur = wrappers.last(); } var tableDom = this.rdom.insertNodeAt(rtable.getDom(), cur, "after"); this.rdom.placeCaretAtStartOf(rtable.getCellAt(0, 0)); if(this.rdom.isEmptyBlock(cur)) this.rdom.deleteNode(cur, true); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, handleInsertNewRowAt: function(where) { var cur = this.rdom.getCurrentBlockElement(); var tr = this.rdom.getParentElementOf(cur, ["TR"]); if(!tr) return true; var table = this.rdom.getParentElementOf(tr, ["TABLE"]); var rtable = new xq.RichTable(this.rdom, table); var row = rtable.insertNewRowAt(tr, where); this.rdom.placeCaretAtStartOf(row.cells[0]); return true; }, handleInsertNewColumnAt: function(where) { var cur = this.rdom.getCurrentBlockElement(); var td = this.rdom.getParentElementOf(cur, ["TD"], true); if(!td) return true; var table = this.rdom.getParentElementOf(td, ["TABLE"]); var rtable = new xq.RichTable(this.rdom, table); rtable.insertNewCellAt(td, where); this.rdom.placeCaretAtStartOf(cur); return true; }, handleDeleteRow: function() { var cur = this.rdom.getCurrentBlockElement(); var tr = this.rdom.getParentElementOf(cur, ["TR"]); if(!tr) return true; var table = this.rdom.getParentElementOf(tr, ["TABLE"]); var rtable = new xq.RichTable(this.rdom, table); var blockToMove = rtable.deleteRow(tr); this.rdom.placeCaretAtStartOf(blockToMove); return true; }, handleDeleteColumn: function() { var cur = this.rdom.getCurrentBlockElement(); var td = this.rdom.getParentElementOf(cur, ["TD"], true); if(!td) return true; var table = this.rdom.getParentElementOf(td, ["TABLE"]); var rtable = new xq.RichTable(this.rdom, table); rtable.deleteCell(td); return true; }, /** * Performs block indentation */ handleIndent: function() { if(this.rdom.hasSelection()) { var blocks = this.rdom.getBlockElementsAtSelectionEdge(true, true); if(blocks.first() != blocks.last()) { var affected = this.rdom.indentElements(blocks.first(), blocks.last()); this.rdom.selectBlocksBetween(affected.first(), affected.last()); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; } } var block = this.rdom.getCurrentBlockElement(); var affected = this.rdom.indentElement(block); if(affected) { this.rdom.placeCaretAtStartOf(affected); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); } return true; }, /** * Performs block outdentation */ handleOutdent: function() { if(this.rdom.hasSelection()) { var blocks = this.rdom.getBlockElementsAtSelectionEdge(true, true); if(blocks.first() != blocks.last()) { var affected = this.rdom.outdentElements(blocks.first(), blocks.last()); this.rdom.selectBlocksBetween(affected.first(), affected.last()); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; } } var block = this.rdom.getCurrentBlockElement(); var affected = this.rdom.outdentElement(block); if(affected) { this.rdom.placeCaretAtStartOf(affected); var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); } return true; }, /** * Applies list. * * @param {String} type "UL" or "OL" or "CODE". CODE generates OL.code */ handleList: function(type) { if(this.rdom.hasSelection()) { var blocks = this.rdom.getBlockElementsAtSelectionEdge(true, true); if(blocks.first() != blocks.last()) { blocks = this.rdom.applyLists(blocks.first(), blocks.last(), type); } else { blocks[0] = blocks[1] = this.rdom.applyList(blocks.first(), type); } this.rdom.selectBlocksBetween(blocks.first(), blocks.last()); } else { var block = this.rdom.applyList(this.rdom.getCurrentBlockElement(), type); this.rdom.placeCaretAtStartOf(block); } var historyAdded = this.editHistory.onCommand(); this._fireOnCurrentContentChanged(this); return true; }, /** * Applies justification * * @param {String} dir "left", "center", "right" or "both" */ handleJustify: function(dir) { var block = this.rdom.getCurrentBlockElement(); var dir = (dir == "left" || dir == "both") && (block.style.textAlign == "left" || block.style.textAlign == "") ? "both" : dir; if(this.rdom.hasSelection()) { var blocks = this.rdom.getSelectedBlockElements(); this.rdom.justifyBlocks(blocks, dir); this.rdom.selectBlocksBetween(blocks.first(), blocks.last()); } else { this.rdom.justifyBlock(block, dir); } var historyAdded = this.editHistory.onC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -