From 22e3b34bf8d927591ec441d8a1af0002d9ebbbb3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Fri, 25 Apr 2014 15:23:49 +0200 Subject: [PATCH] editor: span icon clickable when cursor in appropriate fragment & enables span removal --- src/editor/plugins/core/core.js | 109 +++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 37 deletions(-) diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js index ed0cf28..38675fb 100644 --- a/src/editor/plugins/core/core.js +++ b/src/editor/plugins/core/core.js @@ -177,46 +177,81 @@ var createWrapTextAction = function(createParams) { }, parent; - if( - !params.fragment || !params.fragment.isValid() || - !(params.fragment instanceof params.fragment.TextRangeFragment) || - !params.fragment.hasSiblingBoundries()) { - return _.extend(state, {allowed: false}); - } - - parent = params.fragment.startNode.parent(); - if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) { + if(!params.fragment || !params.fragment.isValid()) { return _.extend(state, {allowed: false}); } - return _.extend(state, { - allowed: true, - description: createParams.description, - execute: function(callback, params) { - params.fragment.document.transaction(function() { - var parent = params.fragment.startNode.parent(), - doc = params.fragment.document, - wrapper, lastTextNode; - - wrapper = parent.wrapText({ - _with: {tagName: 'span', attrs: {'class': createParams.klass}}, - offsetStart: params.fragment.startOffset, - offsetEnd: params.fragment.endOffset, - textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] - }); - - lastTextNode = wrapper.getLastTextNode(); - if(lastTextNode) { - return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length}); + if(params.fragment instanceof params.fragment.CaretFragment && params.fragment.node.isInside(createParams.klass)) { + return _.extend(state, { + allowed: true, + toggled: true, + description: createParams.unwrapDescription, + execute: function(callback, params) { + var node = params.fragment.node, + doc = node.document, + toRemove = node.getParent(createParams.klass), + prefLen = 0; + + if(node.sameNode(toRemove.contents()[0]) && toRemove.isPrecededByTextNode()) { + prefLen = toRemove.prev().getText().length; } - }, { - metadata: { - description: createParams.description - }, - success: callback - }); + + doc.transaction(function() { + var ret = toRemove.unwrapContent(), + newFragment = params.fragment; + if(!newFragment.isValid()) { + newFragment = doc.createFragment(doc.CaretFragment, { + node: ret.element1, + offset: prefLen + params.fragment.offset + }); + } + return newFragment; + }, { + metadata: { + description: createParams.unwrapDescription + }, + success: callback + }); + } + }); + } + + if(params.fragment instanceof params.fragment.TextRangeFragment && params.fragment.hasSiblingBoundries()) { + parent = params.fragment.startNode.parent(); + if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) { + return _.extend(state, {allowed: false}); } - }); + + return _.extend(state, { + allowed: true, + description: createParams.wrapDescription, + execute: function(callback, params) { + params.fragment.document.transaction(function() { + var parent = params.fragment.startNode.parent(), + doc = params.fragment.document, + wrapper, lastTextNode; + + wrapper = parent.wrapText({ + _with: {tagName: 'span', attrs: {'class': createParams.klass}}, + offsetStart: params.fragment.startOffset, + offsetEnd: params.fragment.endOffset, + textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()] + }); + + lastTextNode = wrapper.getLastTextNode(); + if(lastTextNode) { + return doc.createFragment(doc.CaretFragment, {node: lastTextNode, offset: lastTextNode.getText().length}); + } + }, { + metadata: { + description: createParams.wrapDescription + }, + success: callback + }); + } + }); + } + return _.extend(state, {allowed: false}); } }; }; @@ -327,8 +362,8 @@ plugin.actions = [ undoRedoAction('undo'), undoRedoAction('redo'), commentAction, - createWrapTextAction({name: 'emphasis', klass: 'emp', description: gettext('Mark as emphasized')}), - createWrapTextAction({name: 'cite', klass: 'cite', description: gettext('Mark as citation')}), + createWrapTextAction({name: 'emphasis', klass: 'emp', wrapDescription: gettext('Mark as emphasized'), unwrapDescription: gettext('Remove emphasis')}), + createWrapTextAction({name: 'cite', klass: 'cite', wrapDescription: gettext('Mark as citation'), unwrapDescription: gettext('Remove citation')}), linkAction ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions); -- 2.20.1