Refactoring
This commit is contained in:
7
web/bower_components/tinymce/plugins/textpattern/index.js
vendored
Normal file
7
web/bower_components/tinymce/plugins/textpattern/index.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
// Exports the "textpattern" plugin for usage with module loaders
|
||||
// Usage:
|
||||
// CommonJS:
|
||||
// require('tinymce/plugins/textpattern')
|
||||
// ES2015:
|
||||
// import 'tinymce/plugins/textpattern'
|
||||
require('./plugin.js');
|
||||
268
web/bower_components/tinymce/plugins/textpattern/plugin.js
vendored
Normal file
268
web/bower_components/tinymce/plugins/textpattern/plugin.js
vendored
Normal file
@@ -0,0 +1,268 @@
|
||||
/**
|
||||
* plugin.js
|
||||
*
|
||||
* Released under LGPL License.
|
||||
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
|
||||
*
|
||||
* License: http://www.tinymce.com/license
|
||||
* Contributing: http://www.tinymce.com/contributing
|
||||
*/
|
||||
|
||||
/*global tinymce:true */
|
||||
|
||||
tinymce.PluginManager.add('textpattern', function(editor) {
|
||||
var isPatternsDirty = true, patterns;
|
||||
|
||||
patterns = editor.settings.textpattern_patterns || [
|
||||
{start: '*', end: '*', format: 'italic'},
|
||||
{start: '**', end: '**', format: 'bold'},
|
||||
{start: '#', format: 'h1'},
|
||||
{start: '##', format: 'h2'},
|
||||
{start: '###', format: 'h3'},
|
||||
{start: '####', format: 'h4'},
|
||||
{start: '#####', format: 'h5'},
|
||||
{start: '######', format: 'h6'},
|
||||
{start: '1. ', cmd: 'InsertOrderedList'},
|
||||
{start: '* ', cmd: 'InsertUnorderedList'},
|
||||
{start: '- ', cmd: 'InsertUnorderedList'}
|
||||
];
|
||||
|
||||
// Returns a sorted patterns list, ordered descending by start length
|
||||
function getPatterns() {
|
||||
if (isPatternsDirty) {
|
||||
patterns.sort(function(a, b) {
|
||||
if (a.start.length > b.start.length) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (a.start.length < b.start.length) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
isPatternsDirty = false;
|
||||
}
|
||||
|
||||
return patterns;
|
||||
}
|
||||
|
||||
// Finds a matching pattern to the specified text
|
||||
function findPattern(text) {
|
||||
var patterns = getPatterns();
|
||||
|
||||
for (var i = 0; i < patterns.length; i++) {
|
||||
if (text.indexOf(patterns[i].start) !== 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (patterns[i].end && text.lastIndexOf(patterns[i].end) != text.length - patterns[i].end.length) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return patterns[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Finds the best matching end pattern
|
||||
function findEndPattern(text, offset, delta) {
|
||||
var patterns, pattern, i;
|
||||
|
||||
// Find best matching end
|
||||
patterns = getPatterns();
|
||||
for (i = 0; i < patterns.length; i++) {
|
||||
pattern = patterns[i];
|
||||
if (pattern.end && text.substr(offset - pattern.end.length - delta, pattern.end.length) == pattern.end) {
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handles inline formats like *abc* and **abc**
|
||||
function applyInlineFormat(space) {
|
||||
var selection, dom, rng, container, offset, startOffset, text, patternRng, pattern, delta, format;
|
||||
|
||||
function splitContainer() {
|
||||
// Split text node and remove start/end from text node
|
||||
container = container.splitText(startOffset);
|
||||
container.splitText(offset - startOffset - delta);
|
||||
container.deleteData(0, pattern.start.length);
|
||||
container.deleteData(container.data.length - pattern.end.length, pattern.end.length);
|
||||
}
|
||||
|
||||
selection = editor.selection;
|
||||
dom = editor.dom;
|
||||
|
||||
if (!selection.isCollapsed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
rng = selection.getRng(true);
|
||||
container = rng.startContainer;
|
||||
offset = rng.startOffset;
|
||||
text = container.data;
|
||||
delta = space ? 1 : 0;
|
||||
|
||||
if (container.nodeType != 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find best matching end
|
||||
pattern = findEndPattern(text, offset, delta);
|
||||
if (!pattern) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find start of matched pattern
|
||||
// TODO: Might need to improve this if there is nested formats
|
||||
startOffset = Math.max(0, offset - delta);
|
||||
startOffset = text.lastIndexOf(pattern.start, startOffset - pattern.end.length - 1);
|
||||
|
||||
if (startOffset === -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup a range for the matching word
|
||||
patternRng = dom.createRng();
|
||||
patternRng.setStart(container, startOffset);
|
||||
patternRng.setEnd(container, offset - delta);
|
||||
pattern = findPattern(patternRng.toString());
|
||||
|
||||
if (!pattern || !pattern.end) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If container match doesn't have anything between start/end then do nothing
|
||||
if (container.data.length <= pattern.start.length + pattern.end.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
format = editor.formatter.get(pattern.format);
|
||||
if (format && format[0].inline) {
|
||||
splitContainer();
|
||||
editor.formatter.apply(pattern.format, {}, container);
|
||||
return container;
|
||||
}
|
||||
}
|
||||
|
||||
// Handles block formats like ##abc or 1. abc
|
||||
function applyBlockFormat() {
|
||||
var selection, dom, container, firstTextNode, node, format, textBlockElm, pattern, walker, rng, offset;
|
||||
|
||||
selection = editor.selection;
|
||||
dom = editor.dom;
|
||||
|
||||
if (!selection.isCollapsed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
textBlockElm = dom.getParent(selection.getStart(), 'p');
|
||||
if (textBlockElm) {
|
||||
walker = new tinymce.dom.TreeWalker(textBlockElm, textBlockElm);
|
||||
while ((node = walker.next())) {
|
||||
if (node.nodeType == 3) {
|
||||
firstTextNode = node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstTextNode) {
|
||||
pattern = findPattern(firstTextNode.data);
|
||||
if (!pattern) {
|
||||
return;
|
||||
}
|
||||
|
||||
rng = selection.getRng(true);
|
||||
container = rng.startContainer;
|
||||
offset = rng.startOffset;
|
||||
|
||||
if (firstTextNode == container) {
|
||||
offset = Math.max(0, offset - pattern.start.length);
|
||||
}
|
||||
|
||||
if (tinymce.trim(firstTextNode.data).length == pattern.start.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pattern.format) {
|
||||
format = editor.formatter.get(pattern.format);
|
||||
if (format && format[0].block) {
|
||||
firstTextNode.deleteData(0, pattern.start.length);
|
||||
editor.formatter.apply(pattern.format, {}, firstTextNode);
|
||||
|
||||
rng.setStart(container, offset);
|
||||
rng.collapse(true);
|
||||
selection.setRng(rng);
|
||||
}
|
||||
}
|
||||
|
||||
if (pattern.cmd) {
|
||||
editor.undoManager.transact(function() {
|
||||
firstTextNode.deleteData(0, pattern.start.length);
|
||||
editor.execCommand(pattern.cmd);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleEnter() {
|
||||
var rng, wrappedTextNode;
|
||||
|
||||
wrappedTextNode = applyInlineFormat();
|
||||
if (wrappedTextNode) {
|
||||
rng = editor.dom.createRng();
|
||||
rng.setStart(wrappedTextNode, wrappedTextNode.data.length);
|
||||
rng.setEnd(wrappedTextNode, wrappedTextNode.data.length);
|
||||
editor.selection.setRng(rng);
|
||||
}
|
||||
|
||||
applyBlockFormat();
|
||||
}
|
||||
|
||||
function handleSpace() {
|
||||
var wrappedTextNode, lastChar, lastCharNode, rng, dom;
|
||||
|
||||
wrappedTextNode = applyInlineFormat(true);
|
||||
if (wrappedTextNode) {
|
||||
dom = editor.dom;
|
||||
lastChar = wrappedTextNode.data.slice(-1);
|
||||
|
||||
// Move space after the newly formatted node
|
||||
if (/[\u00a0 ]/.test(lastChar)) {
|
||||
wrappedTextNode.deleteData(wrappedTextNode.data.length - 1, 1);
|
||||
lastCharNode = dom.doc.createTextNode(lastChar);
|
||||
|
||||
if (wrappedTextNode.nextSibling) {
|
||||
dom.insertAfter(lastCharNode, wrappedTextNode.nextSibling);
|
||||
} else {
|
||||
wrappedTextNode.parentNode.appendChild(lastCharNode);
|
||||
}
|
||||
|
||||
rng = dom.createRng();
|
||||
rng.setStart(lastCharNode, 1);
|
||||
rng.setEnd(lastCharNode, 1);
|
||||
editor.selection.setRng(rng);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
editor.on('keydown', function(e) {
|
||||
if (e.keyCode == 13 && !tinymce.util.VK.modifierPressed(e)) {
|
||||
handleEnter();
|
||||
}
|
||||
}, true);
|
||||
|
||||
editor.on('keyup', function(e) {
|
||||
if (e.keyCode == 32 && !tinymce.util.VK.modifierPressed(e)) {
|
||||
handleSpace();
|
||||
}
|
||||
});
|
||||
|
||||
this.getPatterns = getPatterns;
|
||||
this.setPatterns = function(newPatterns) {
|
||||
patterns = newPatterns;
|
||||
isPatternsDirty = true;
|
||||
};
|
||||
});
|
||||
1
web/bower_components/tinymce/plugins/textpattern/plugin.min.js
vendored
Normal file
1
web/bower_components/tinymce/plugins/textpattern/plugin.min.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
tinymce.PluginManager.add("textpattern",function(e){function t(){return c&&(l.sort(function(e,t){return e.start.length>t.start.length?-1:e.start.length<t.start.length?1:0}),c=!1),l}function n(e){for(var n=t(),r=0;r<n.length;r++)if(0===e.indexOf(n[r].start)&&(!n[r].end||e.lastIndexOf(n[r].end)==e.length-n[r].end.length))return n[r]}function r(e,n,r){var i,o,a;for(i=t(),a=0;a<i.length;a++)if(o=i[a],o.end&&e.substr(n-o.end.length-r,o.end.length)==o.end)return o}function i(t){function i(){l=l.splitText(u),l.splitText(c-u-m),l.deleteData(0,p.start.length),l.deleteData(l.data.length-p.end.length,p.end.length)}var o,a,s,l,c,u,d,f,p,m,g;if(o=e.selection,a=e.dom,o.isCollapsed()&&(s=o.getRng(!0),l=s.startContainer,c=s.startOffset,d=l.data,m=t?1:0,3==l.nodeType&&(p=r(d,c,m),p&&(u=Math.max(0,c-m),u=d.lastIndexOf(p.start,u-p.end.length-1),u!==-1&&(f=a.createRng(),f.setStart(l,u),f.setEnd(l,c-m),p=n(f.toString()),p&&p.end&&!(l.data.length<=p.start.length+p.end.length))))))return g=e.formatter.get(p.format),g&&g[0].inline?(i(),e.formatter.apply(p.format,{},l),l):void 0}function o(){var t,r,i,o,a,s,l,c,u,d,f;if(t=e.selection,r=e.dom,t.isCollapsed()&&(l=r.getParent(t.getStart(),"p"))){for(u=new tinymce.dom.TreeWalker(l,l);a=u.next();)if(3==a.nodeType){o=a;break}if(o){if(c=n(o.data),!c)return;if(d=t.getRng(!0),i=d.startContainer,f=d.startOffset,o==i&&(f=Math.max(0,f-c.start.length)),tinymce.trim(o.data).length==c.start.length)return;c.format&&(s=e.formatter.get(c.format),s&&s[0].block&&(o.deleteData(0,c.start.length),e.formatter.apply(c.format,{},o),d.setStart(i,f),d.collapse(!0),t.setRng(d))),c.cmd&&e.undoManager.transact(function(){o.deleteData(0,c.start.length),e.execCommand(c.cmd)})}}}function a(){var t,n;n=i(),n&&(t=e.dom.createRng(),t.setStart(n,n.data.length),t.setEnd(n,n.data.length),e.selection.setRng(t)),o()}function s(){var t,n,r,o,a;t=i(!0),t&&(a=e.dom,n=t.data.slice(-1),/[\u00a0 ]/.test(n)&&(t.deleteData(t.data.length-1,1),r=a.doc.createTextNode(n),t.nextSibling?a.insertAfter(r,t.nextSibling):t.parentNode.appendChild(r),o=a.createRng(),o.setStart(r,1),o.setEnd(r,1),e.selection.setRng(o)))}var l,c=!0;l=e.settings.textpattern_patterns||[{start:"*",end:"*",format:"italic"},{start:"**",end:"**",format:"bold"},{start:"#",format:"h1"},{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:"1. ",cmd:"InsertOrderedList"},{start:"* ",cmd:"InsertUnorderedList"},{start:"- ",cmd:"InsertUnorderedList"}],e.on("keydown",function(e){13!=e.keyCode||tinymce.util.VK.modifierPressed(e)||a()},!0),e.on("keyup",function(e){32!=e.keyCode||tinymce.util.VK.modifierPressed(e)||s()}),this.getPatterns=t,this.setPatterns=function(e){l=e,c=!0}});
|
||||
Reference in New Issue
Block a user