first commit

This commit is contained in:
2026-06-24 09:48:54 +02:00
commit 41e62ddcad
33739 changed files with 4266226 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
var Delta, Document, Format, Line, LinkedList, Normalizer, dom, _;
_ = require('lodash');
Delta = require('rich-text').Delta;
dom = require('../lib/dom');
Format = require('./format');
Line = require('./line');
LinkedList = require('../lib/linked-list');
Normalizer = require('../lib/normalizer');
Document = (function() {
function Document(root, options) {
this.root = root;
if (options == null) {
options = {};
}
this.formats = {};
_.each(options.formats, _.bind(this.addFormat, this));
this.setHTML(this.root.innerHTML);
}
Document.prototype.addFormat = function(name, config) {
if (!_.isObject(config)) {
config = Format.FORMATS[name];
}
if (this.formats[name] != null) {
console.warn('Overwriting format', name, this.formats[name]);
}
return this.formats[name] = new Format(this.root.ownerDocument, config);
};
Document.prototype.appendLine = function(lineNode) {
return this.insertLineBefore(lineNode, null);
};
Document.prototype.findLeafAt = function(index, inclusive) {
var line, offset, _ref;
_ref = this.findLineAt(index), line = _ref[0], offset = _ref[1];
if (line != null) {
return line.findLeafAt(offset, inclusive);
} else {
return [null, offset];
}
};
Document.prototype.findLine = function(node) {
var line;
while ((node != null) && (dom.BLOCK_TAGS[node.tagName] == null)) {
node = node.parentNode;
}
line = node != null ? this.lineMap[node.id] : null;
if ((line != null ? line.node : void 0) === node) {
return line;
} else {
return null;
}
};
Document.prototype.findLineAt = function(index) {
var curLine, length;
if (!(this.lines.length > 0)) {
return [null, index];
}
length = this.toDelta().length();
if (index === length) {
return [this.lines.last, this.lines.last.length];
}
if (index > length) {
return [null, index - length];
}
curLine = this.lines.first;
while (curLine != null) {
if (index < curLine.length) {
return [curLine, index];
}
index -= curLine.length;
curLine = curLine.next;
}
return [null, index];
};
Document.prototype.insertLineBefore = function(newLineNode, refLine) {
var line;
line = new Line(this, newLineNode);
if (refLine != null) {
if (!dom(newLineNode.parentNode).isElement()) {
this.root.insertBefore(newLineNode, refLine.node);
}
this.lines.insertAfter(refLine.prev, line);
} else {
if (!dom(newLineNode.parentNode).isElement()) {
this.root.appendChild(newLineNode);
}
this.lines.append(line);
}
this.lineMap[line.id] = line;
return line;
};
Document.prototype.mergeLines = function(line, lineToMerge) {
if (lineToMerge.length > 1) {
if (line.length === 1) {
dom(line.leaves.last.node).remove();
}
_.each(dom(lineToMerge.node).childNodes(), function(child) {
if (child.tagName !== dom.DEFAULT_BREAK_TAG) {
return line.node.appendChild(child);
}
});
}
this.removeLine(lineToMerge);
return line.rebuild();
};
Document.prototype.optimizeLines = function() {
return _.each(this.lines.toArray(), function(line, i) {
line.optimize();
return true;
});
};
Document.prototype.rebuild = function() {
var lineNode, lines, _results;
lines = this.lines.toArray();
lineNode = this.root.firstChild;
if ((lineNode != null) && (dom.LIST_TAGS[lineNode.tagName] != null)) {
lineNode = lineNode.firstChild;
}
_.each(lines, (function(_this) {
return function(line, index) {
var newLine, _ref;
while (line.node !== lineNode) {
if (line.node.parentNode === _this.root || ((_ref = line.node.parentNode) != null ? _ref.parentNode : void 0) === _this.root) {
lineNode = Normalizer.normalizeLine(lineNode);
newLine = _this.insertLineBefore(lineNode, line);
lineNode = dom(lineNode).nextLineNode(_this.root);
} else {
return _this.removeLine(line);
}
}
if (line.outerHTML !== lineNode.outerHTML) {
line.node = Normalizer.normalizeLine(line.node);
line.rebuild();
}
return lineNode = dom(lineNode).nextLineNode(_this.root);
};
})(this));
_results = [];
while (lineNode != null) {
lineNode = Normalizer.normalizeLine(lineNode);
this.appendLine(lineNode);
_results.push(lineNode = dom(lineNode).nextLineNode(this.root));
}
return _results;
};
Document.prototype.removeLine = function(line) {
if (line.node.parentNode != null) {
if (dom.LIST_TAGS[line.node.parentNode.tagName] && line.node.parentNode.childNodes.length === 1) {
dom(line.node.parentNode).remove();
} else {
dom(line.node).remove();
}
}
delete this.lineMap[line.id];
return this.lines.remove(line);
};
Document.prototype.setHTML = function(html) {
html = Normalizer.stripComments(html);
html = Normalizer.stripWhitespace(html);
this.root.innerHTML = html;
this.lines = new LinkedList();
this.lineMap = {};
return this.rebuild();
};
Document.prototype.splitLine = function(line, offset) {
var lineNode1, lineNode2, newLine, _ref;
offset = Math.min(offset, line.length - 1);
_ref = dom(line.node).split(offset, true), lineNode1 = _ref[0], lineNode2 = _ref[1];
line.node = lineNode1;
line.rebuild();
newLine = this.insertLineBefore(lineNode2, line.next);
newLine.formats = _.clone(line.formats);
newLine.resetContent();
return newLine;
};
Document.prototype.toDelta = function() {
var delta, lines;
lines = this.lines.toArray();
delta = new Delta();
lines.forEach(function(line) {
return line.delta.ops.forEach(function(op) {
return delta.push(op);
});
});
return delta;
};
return Document;
})();
module.exports = Document;
+242
View File
@@ -0,0 +1,242 @@
var Document, Editor, Line, Renderer, Selection, dom, _;
_ = require('lodash');
dom = require('../lib/dom');
Document = require('./document');
Line = require('./line');
Renderer = require('./renderer');
Selection = require('./selection');
Editor = (function() {
Editor.sources = {
API: 'api',
SILENT: 'silent',
USER: 'user'
};
function Editor(iframeContainer, quill, options) {
this.iframeContainer = iframeContainer;
this.quill = quill;
this.options = options != null ? options : {};
this.renderer = new Renderer(this.iframeContainer, this.options);
dom(this.iframeContainer).on('focus', this.focus.bind(this));
this.root = this.renderer.root;
this.doc = new Document(this.root, this.options);
this.delta = this.doc.toDelta();
this.selection = new Selection(this.doc, this.renderer.iframe, this.quill);
this.timer = setInterval(_.bind(this.checkUpdate, this), this.options.pollInterval);
if (!this.options.readOnly) {
this.enable();
}
}
Editor.prototype.disable = function() {
return this.enable(false);
};
Editor.prototype.enable = function(enabled) {
if (enabled == null) {
enabled = true;
}
return this.root.setAttribute('contenteditable', enabled);
};
Editor.prototype.applyDelta = function(delta, source) {
var localDelta;
localDelta = this._update();
if (localDelta) {
delta = localDelta.transform(delta, true);
localDelta = delta.transform(localDelta, false);
}
if (delta.ops.length > 0) {
delta = this._trackDelta((function(_this) {
return function() {
var index;
index = 0;
_.each(delta.ops, function(op) {
if (_.isString(op.insert)) {
_this._insertAt(index, op.insert, op.attributes);
return index += op.insert.length;
} else if (_.isNumber(op.insert)) {
_this._insertAt(index, dom.EMBED_TEXT, op.attributes);
return index += 1;
} else if (_.isNumber(op["delete"])) {
return _this._deleteAt(index, op["delete"]);
} else if (_.isNumber(op.retain)) {
_.each(op.attributes, function(value, name) {
return _this._formatAt(index, op.retain, name, value);
});
return index += op.retain;
}
});
return _this.selection.shiftAfter(0, 0, _.bind(_this.doc.optimizeLines, _this.doc));
};
})(this));
this.delta = this.doc.toDelta();
this.innerHTML = this.root.innerHTML;
if (delta && source !== Editor.sources.SILENT) {
this.quill.emit(this.quill.constructor.events.TEXT_CHANGE, delta, source);
}
}
if (localDelta && localDelta.ops.length > 0 && source !== Editor.sources.SILENT) {
return this.quill.emit(this.quill.constructor.events.TEXT_CHANGE, localDelta, Editor.sources.USER);
}
};
Editor.prototype.checkUpdate = function(source) {
var delta;
if (source == null) {
source = 'user';
}
if ((this.renderer.iframe.parentNode == null) || (this.root.parentNode == null)) {
return clearInterval(this.timer);
}
delta = this._update();
if (delta) {
this.delta.compose(delta);
this.quill.emit(this.quill.constructor.events.TEXT_CHANGE, delta, source);
}
if (delta) {
source = Editor.sources.SILENT;
}
return this.selection.update(source);
};
Editor.prototype.focus = function() {
if (dom.isIE(11)) {
this.selection.setRange(this.selection.range);
}
if (dom.isIOS()) {
this.renderer.iframe.focus();
}
return this.root.focus();
};
Editor.prototype.getDelta = function() {
return this.delta;
};
Editor.prototype._deleteAt = function(index, length) {
if (length <= 0) {
return;
}
return this.selection.shiftAfter(index, -1 * length, (function(_this) {
return function() {
var curLine, deleteLength, firstLine, mergeFirstLine, nextLine, offset, _ref;
_ref = _this.doc.findLineAt(index), firstLine = _ref[0], offset = _ref[1];
curLine = firstLine;
mergeFirstLine = firstLine.length - offset <= length && offset > 0;
while ((curLine != null) && length > 0) {
nextLine = curLine.next;
deleteLength = Math.min(curLine.length - offset, length);
if (offset === 0 && length >= curLine.length) {
_this.doc.removeLine(curLine);
} else {
curLine.deleteText(offset, deleteLength);
}
length -= deleteLength;
curLine = nextLine;
offset = 0;
}
if (mergeFirstLine && firstLine.next) {
return _this.doc.mergeLines(firstLine, firstLine.next);
}
};
})(this));
};
Editor.prototype._formatAt = function(index, length, name, value) {
return this.selection.shiftAfter(index, 0, (function(_this) {
return function() {
var formatLength, line, offset, _ref, _results;
_ref = _this.doc.findLineAt(index), line = _ref[0], offset = _ref[1];
_results = [];
while ((line != null) && length > 0) {
formatLength = Math.min(length, line.length - offset - 1);
line.formatText(offset, formatLength, name, value);
length -= formatLength;
if (length > 0) {
line.format(name, value);
}
length -= 1;
offset = 0;
_results.push(line = line.next);
}
return _results;
};
})(this));
};
Editor.prototype._insertAt = function(index, text, formatting) {
if (formatting == null) {
formatting = {};
}
return this.selection.shiftAfter(index, text.length, (function(_this) {
return function() {
var line, lineTexts, offset, _ref;
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
lineTexts = text.split('\n');
_ref = _this.doc.findLineAt(index), line = _ref[0], offset = _ref[1];
return _.each(lineTexts, function(lineText, i) {
var nextLine;
if ((line == null) || line.length <= offset) {
if (i < lineTexts.length - 1 || lineText.length > 0) {
line = _this.doc.appendLine(_this.root.ownerDocument.createElement(dom.DEFAULT_BLOCK_TAG));
offset = 0;
line.insertText(offset, lineText, formatting);
line.format(formatting);
nextLine = null;
}
} else {
line.insertText(offset, lineText, formatting);
if (i < lineTexts.length - 1) {
nextLine = _this.doc.splitLine(line, offset + lineText.length);
_.each(_.defaults({}, formatting, line.formats), function(value, format) {
return line.format(format, formatting[format]);
});
offset = 0;
}
}
return line = nextLine;
});
};
})(this));
};
Editor.prototype._trackDelta = function(fn) {
var delta, newDelta;
fn();
newDelta = this.doc.toDelta();
delta = this.delta.diff(newDelta);
return delta;
};
Editor.prototype._update = function() {
var delta;
if (this.innerHTML === this.root.innerHTML) {
return false;
}
delta = this._trackDelta((function(_this) {
return function() {
_this.selection.preserve(_.bind(_this.doc.rebuild, _this.doc));
return _this.selection.shiftAfter(0, 0, _.bind(_this.doc.optimizeLines, _this.doc));
};
})(this));
this.innerHTML = this.root.innerHTML;
if (delta.ops.length > 0) {
return delta;
} else {
return false;
}
};
return Editor;
})();
module.exports = Editor;
+261
View File
@@ -0,0 +1,261 @@
var Format, dom, _;
_ = require('lodash');
dom = require('../lib/dom');
Format = (function() {
Format.types = {
LINE: 'line'
};
Format.FORMATS = {
bold: {
tag: 'B',
prepare: 'bold'
},
italic: {
tag: 'I',
prepare: 'italic'
},
underline: {
tag: 'U',
prepare: 'underline'
},
strike: {
tag: 'S',
prepare: 'strikeThrough'
},
color: {
style: 'color',
"default": 'rgb(0, 0, 0)',
prepare: 'foreColor'
},
background: {
style: 'backgroundColor',
"default": 'rgb(255, 255, 255)',
prepare: 'backColor'
},
font: {
style: 'fontFamily',
"default": "'Helvetica', 'Arial', sans-serif",
prepare: 'fontName'
},
size: {
style: 'fontSize',
"default": '13px',
prepare: function(doc, value) {
return doc.execCommand('fontSize', false, dom.convertFontSize(value));
}
},
link: {
tag: 'A',
attribute: 'href'
},
image: {
tag: 'IMG',
attribute: 'src'
},
align: {
type: Format.types.LINE,
style: 'textAlign',
"default": 'left'
},
bullet: {
type: Format.types.LINE,
exclude: 'list',
parentTag: 'UL',
tag: 'LI'
},
list: {
type: Format.types.LINE,
exclude: 'bullet',
parentTag: 'OL',
tag: 'LI'
}
};
function Format(document, config) {
this.document = document;
this.config = config;
}
Format.prototype.add = function(node, value) {
var formatNode, inline, parentNode, _ref, _ref1;
if (!value) {
return this.remove(node);
}
if (this.value(node) === value) {
return node;
}
if (_.isString(this.config.parentTag)) {
parentNode = this.document.createElement(this.config.parentTag);
dom(node).wrap(parentNode);
if (node.parentNode.tagName === ((_ref = node.parentNode.previousSibling) != null ? _ref.tagName : void 0)) {
dom(node.parentNode.previousSibling).merge(node.parentNode);
}
if (node.parentNode.tagName === ((_ref1 = node.parentNode.nextSibling) != null ? _ref1.tagName : void 0)) {
dom(node.parentNode).merge(node.parentNode.nextSibling);
}
}
if (_.isString(this.config.tag)) {
formatNode = this.document.createElement(this.config.tag);
if (dom.VOID_TAGS[formatNode.tagName] != null) {
if (node.parentNode != null) {
dom(node).replace(formatNode);
}
node = formatNode;
} else if (this.isType(Format.types.LINE)) {
node = dom(node).switchTag(this.config.tag);
} else {
dom(node).wrap(formatNode);
node = formatNode;
}
}
if (_.isString(this.config.style) || _.isString(this.config.attribute) || _.isString(this.config["class"])) {
if (_.isString(this.config["class"])) {
node = this.remove(node);
}
if (dom(node).isTextNode()) {
inline = this.document.createElement(dom.DEFAULT_INLINE_TAG);
dom(node).wrap(inline);
node = inline;
}
if (_.isString(this.config.style)) {
if (value !== this.config["default"]) {
node.style[this.config.style] = value;
}
}
if (_.isString(this.config.attribute)) {
node.setAttribute(this.config.attribute, value);
}
if (_.isString(this.config["class"])) {
dom(node).addClass(this.config["class"] + value);
}
}
return node;
};
Format.prototype.isType = function(type) {
return type === this.config.type;
};
Format.prototype.match = function(node) {
var c, _i, _len, _ref, _ref1;
if (!dom(node).isElement()) {
return false;
}
if (_.isString(this.config.parentTag) && ((_ref = node.parentNode) != null ? _ref.tagName : void 0) !== this.config.parentTag) {
return false;
}
if (_.isString(this.config.tag) && node.tagName !== this.config.tag) {
return false;
}
if (_.isString(this.config.style) && (!node.style[this.config.style] || node.style[this.config.style] === this.config["default"])) {
return false;
}
if (_.isString(this.config.attribute) && !node.hasAttribute(this.config.attribute)) {
return false;
}
if (_.isString(this.config["class"])) {
_ref1 = dom(node).classes();
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
c = _ref1[_i];
if (c.indexOf(this.config["class"]) === 0) {
return true;
}
}
return false;
}
return true;
};
Format.prototype.prepare = function(value) {
if (_.isString(this.config.prepare)) {
return this.document.execCommand(this.config.prepare, false, value);
} else if (_.isFunction(this.config.prepare)) {
return this.config.prepare(this.document, value);
}
};
Format.prototype.remove = function(node) {
var c, _i, _len, _ref;
if (!this.match(node)) {
return node;
}
if (_.isString(this.config.style)) {
node.style[this.config.style] = '';
if (!node.getAttribute('style')) {
node.removeAttribute('style');
}
}
if (_.isString(this.config.attribute)) {
node.removeAttribute(this.config.attribute);
}
if (_.isString(this.config["class"])) {
_ref = dom(node).classes();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
if (c.indexOf(this.config["class"]) === 0) {
dom(node).removeClass(c);
}
}
if (!node.getAttribute('class')) {
node.removeAttribute('class');
}
}
if (_.isString(this.config.tag)) {
if (this.isType(Format.types.LINE)) {
if (_.isString(this.config.parentTag)) {
if (node.previousSibling != null) {
dom(node).splitAncestors(node.parentNode.parentNode);
}
if (node.nextSibling != null) {
dom(node.nextSibling).splitAncestors(node.parentNode.parentNode);
}
}
node = dom(node).switchTag(dom.DEFAULT_BLOCK_TAG);
} else {
node = dom(node).switchTag(dom.DEFAULT_INLINE_TAG);
if (dom.EMBED_TAGS[this.config.tag] != null) {
dom(node).text(dom.EMBED_TEXT);
}
}
}
if (_.isString(this.config.parentTag)) {
dom(node.parentNode).unwrap();
}
if (node.tagName === dom.DEFAULT_INLINE_TAG && !node.hasAttributes()) {
node = dom(node).unwrap();
}
return node;
};
Format.prototype.value = function(node) {
var c, _i, _len, _ref;
if (!this.match(node)) {
return void 0;
}
if (_.isString(this.config.attribute)) {
return node.getAttribute(this.config.attribute) || void 0;
} else if (_.isString(this.config.style)) {
return node.style[this.config.style] || void 0;
} else if (_.isString(this.config["class"])) {
_ref = dom(node).classes();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
if (c.indexOf(this.config["class"]) === 0) {
return c.slice(this.config["class"].length);
}
}
} else if (_.isString(this.config.tag)) {
return true;
}
return void 0;
};
return Format;
})();
module.exports = Format;
+66
View File
@@ -0,0 +1,66 @@
var Format, Leaf, LinkedList, dom, _,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
_ = require('lodash');
dom = require('../lib/dom');
Format = require('./format');
LinkedList = require('../lib/linked-list');
Leaf = (function(_super) {
__extends(Leaf, _super);
Leaf.ID_PREFIX = 'leaf-';
Leaf.isLeafNode = function(node) {
return dom(node).isTextNode() || (node.firstChild == null);
};
function Leaf(node, formats) {
this.node = node;
this.formats = _.clone(formats);
this.id = _.uniqueId(Leaf.ID_PREFIX);
this.text = dom(this.node).text();
this.length = this.text.length;
}
Leaf.prototype.deleteText = function(offset, length) {
var textNode;
if (!(length > 0)) {
return;
}
this.text = this.text.slice(0, offset) + this.text.slice(offset + length);
this.length = this.text.length;
if (dom.EMBED_TAGS[this.node.tagName] != null) {
textNode = this.node.ownerDocument.createTextNode(this.text);
return this.node = dom(this.node).replace(textNode);
} else {
return dom(this.node).text(this.text);
}
};
Leaf.prototype.insertText = function(offset, text) {
var textNode;
this.text = this.text.slice(0, offset) + text + this.text.slice(offset);
if (dom(this.node).isTextNode()) {
dom(this.node).text(this.text);
} else {
textNode = this.node.ownerDocument.createTextNode(text);
if (this.node.tagName === dom.DEFAULT_BREAK_TAG) {
this.node = dom(this.node).replace(textNode);
} else {
this.node.appendChild(textNode);
this.node = textNode;
}
}
return this.length = this.text.length;
};
return Leaf;
})(LinkedList.Node);
module.exports = Leaf;
+260
View File
@@ -0,0 +1,260 @@
var Delta, Format, Leaf, Line, LinkedList, Normalizer, dom, _,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
_ = require('lodash');
Delta = require('rich-text').Delta;
dom = require('../lib/dom');
Format = require('./format');
Leaf = require('./leaf');
Line = require('./line');
LinkedList = require('../lib/linked-list');
Normalizer = require('../lib/normalizer');
Line = (function(_super) {
__extends(Line, _super);
Line.CLASS_NAME = 'line';
Line.ID_PREFIX = 'line-';
function Line(doc, node) {
this.doc = doc;
this.node = node;
this.id = _.uniqueId(Line.ID_PREFIX);
this.formats = {};
dom(this.node).addClass(Line.CLASS_NAME);
this.rebuild();
Line.__super__.constructor.call(this, this.node);
}
Line.prototype.buildLeaves = function(node, formats) {
return _.each(dom(node).childNodes(), (function(_this) {
return function(node) {
var nodeFormats;
node = Normalizer.normalizeNode(node);
nodeFormats = _.clone(formats);
_.each(_this.doc.formats, function(format, name) {
if (!format.isType(Format.types.LINE) && format.match(node)) {
return nodeFormats[name] = format.value(node);
}
});
if (Leaf.isLeafNode(node)) {
return _this.leaves.append(new Leaf(node, nodeFormats));
} else {
return _this.buildLeaves(node, nodeFormats);
}
};
})(this));
};
Line.prototype.deleteText = function(offset, length) {
var deleteLength, leaf, _ref;
if (!(length > 0)) {
return;
}
_ref = this.findLeafAt(offset), leaf = _ref[0], offset = _ref[1];
while ((leaf != null) && length > 0) {
deleteLength = Math.min(length, leaf.length - offset);
leaf.deleteText(offset, deleteLength);
length -= deleteLength;
leaf = leaf.next;
offset = 0;
}
return this.rebuild();
};
Line.prototype.findLeaf = function(leafNode) {
var curLeaf;
curLeaf = this.leaves.first;
while (curLeaf != null) {
if (curLeaf.node === leafNode) {
return curLeaf;
}
curLeaf = curLeaf.next;
}
return null;
};
Line.prototype.findLeafAt = function(offset, inclusive) {
var leaf;
if (inclusive == null) {
inclusive = false;
}
if (offset >= this.length - 1) {
return [this.leaves.last, this.leaves.last.length];
}
leaf = this.leaves.first;
while (leaf != null) {
if (offset < leaf.length || (offset === leaf.length && inclusive)) {
return [leaf, offset];
}
offset -= leaf.length;
leaf = leaf.next;
}
return [this.leaves.last, offset - this.leaves.last.length];
};
Line.prototype.format = function(name, value) {
var formats;
if (_.isObject(name)) {
formats = name;
} else {
formats = {};
formats[name] = value;
}
_.each(formats, (function(_this) {
return function(value, name) {
var excludeFormat, format;
format = _this.doc.formats[name];
if (format.isType(Format.types.LINE)) {
if (format.config.exclude && _this.formats[format.config.exclude]) {
excludeFormat = _this.doc.formats[format.config.exclude];
if (excludeFormat != null) {
_this.node = excludeFormat.remove(_this.node);
delete _this.formats[format.config.exclude];
}
}
_this.node = format.add(_this.node, value);
}
if (value) {
return _this.formats[name] = value;
} else {
return delete _this.formats[name];
}
};
})(this));
return this.resetContent();
};
Line.prototype.formatText = function(offset, length, name, value) {
var format, leaf, leafOffset, leftNode, nextLeaf, rightNode, targetNode, _ref, _ref1, _ref2;
_ref = this.findLeafAt(offset), leaf = _ref[0], leafOffset = _ref[1];
format = this.doc.formats[name];
if (!((format != null) && format.config.type !== Format.types.LINE)) {
return;
}
while ((leaf != null) && length > 0) {
nextLeaf = leaf.next;
if ((value && leaf.formats[name] !== value) || (!value && (leaf.formats[name] != null))) {
targetNode = leaf.node;
if (leaf.formats[name] != null) {
dom(targetNode).splitAncestors(this.node);
while (!format.match(targetNode)) {
targetNode = targetNode.parentNode;
}
}
if (leafOffset > 0) {
_ref1 = dom(targetNode).split(leafOffset), leftNode = _ref1[0], targetNode = _ref1[1];
}
if (leaf.length > leafOffset + length) {
_ref2 = dom(targetNode).split(length), targetNode = _ref2[0], rightNode = _ref2[1];
}
format.add(targetNode, value);
}
length -= leaf.length - leafOffset;
leafOffset = 0;
leaf = nextLeaf;
}
return this.rebuild();
};
Line.prototype.insertText = function(offset, text, formats) {
var leaf, leafOffset, nextNode, node, prevNode, _ref, _ref1;
if (formats == null) {
formats = {};
}
if (!(text.length > 0)) {
return;
}
_ref = this.findLeafAt(offset), leaf = _ref[0], leafOffset = _ref[1];
if (_.isEqual(leaf.formats, formats)) {
leaf.insertText(leafOffset, text);
return this.resetContent();
} else {
node = _.reduce(formats, (function(_this) {
return function(node, value, name) {
return _this.doc.formats[name].add(node, value);
};
})(this), this.node.ownerDocument.createTextNode(text));
_ref1 = dom(leaf.node).split(leafOffset), prevNode = _ref1[0], nextNode = _ref1[1];
if (nextNode) {
nextNode = dom(nextNode).splitAncestors(this.node).get();
}
this.node.insertBefore(node, nextNode);
return this.rebuild();
}
};
Line.prototype.optimize = function() {
Normalizer.optimizeLine(this.node);
return this.rebuild();
};
Line.prototype.rebuild = function(force) {
if (force == null) {
force = false;
}
if (!force && (this.outerHTML != null) && this.outerHTML === this.node.outerHTML) {
if (_.all(this.leaves.toArray(), (function(_this) {
return function(leaf) {
return dom(leaf.node).isAncestor(_this.node);
};
})(this))) {
return false;
}
}
this.node = Normalizer.normalizeNode(this.node);
if (dom(this.node).length() === 0 && !this.node.querySelector(dom.DEFAULT_BREAK_TAG)) {
this.node.appendChild(this.node.ownerDocument.createElement(dom.DEFAULT_BREAK_TAG));
}
this.leaves = new LinkedList();
this.formats = _.reduce(this.doc.formats, (function(_this) {
return function(formats, format, name) {
if (format.isType(Format.types.LINE)) {
if (format.match(_this.node)) {
formats[name] = format.value(_this.node);
} else {
delete formats[name];
}
}
return formats;
};
})(this), this.formats);
this.buildLeaves(this.node, {});
this.resetContent();
return true;
};
Line.prototype.resetContent = function() {
if (this.node.id !== this.id) {
this.node.id = this.id;
}
this.outerHTML = this.node.outerHTML;
this.length = 1;
this.delta = new Delta();
this.leaves.toArray().forEach((function(_this) {
return function(leaf) {
_this.length += leaf.length;
if (dom.EMBED_TAGS[leaf.node.tagName] != null) {
return _this.delta.insert(1, leaf.formats);
} else {
return _this.delta.insert(leaf.text, leaf.formats);
}
};
})(this));
return this.delta.insert('\n', this.formats);
};
return Line;
})(LinkedList.Node);
module.exports = Line;
+177
View File
@@ -0,0 +1,177 @@
var DEFAULT_STYLES, LIST_STYLES, Normalizer, Renderer, dom, rule, _;
_ = require('lodash');
dom = require('../lib/dom');
Normalizer = require('../lib/normalizer');
DEFAULT_STYLES = {
'html': {
'height': '100%',
'width': '100%'
},
'body': {
'box-sizing': 'border-box',
'cursor': 'text',
'font-family': "'Helvetica', 'Arial', sans-serif",
'font-size': '13px',
'height': '100%',
'line-height': '1.42',
'margin': '0px',
'overflow-x': 'hidden',
'overflow-y': 'auto',
'padding': '12px 15px'
},
'.editor-container': {
'height': '100%',
'outline': 'none',
'position': 'relative',
'tab-size': '4',
'white-space': 'pre-wrap'
},
'.editor-container div': {
'margin': '0',
'padding': '0'
},
'.editor-container a': {
'text-decoration': 'underline'
},
'.editor-container b': {
'font-weight': 'bold'
},
'.editor-container i': {
'font-style': 'italic'
},
'.editor-container s': {
'text-decoration': 'line-through'
},
'.editor-container u': {
'text-decoration': 'underline'
},
'.editor-container img': {
'max-width': '100%'
},
'.editor-container blockquote': {
'margin': '0 0 0 2em',
'padding': '0'
},
'.editor-container ol': {
'margin': '0 0 0 2em',
'padding': '0',
'list-style-type': 'decimal'
},
'.editor-container ul': {
'margin': '0 0 0 2em',
'padding': '0',
'list-style-type': 'disc'
}
};
LIST_STYLES = ['decimal', 'lower-alpha', 'lower-roman'];
rule = '.editor-container ol > li';
_.each([1, 2, 3, 4, 5, 6, 7, 8, 9], function(i) {
rule += ' > ol';
DEFAULT_STYLES[rule] = {
'list-style-type': LIST_STYLES[i % 3]
};
return rule += ' > li';
});
if (dom.isIE(10)) {
DEFAULT_STYLES[dom.DEFAULT_BREAK_TAG] = {
'display': 'none'
};
}
Renderer = (function() {
Renderer.objToCss = function(obj) {
return _.map(obj, function(value, key) {
var innerStr;
innerStr = _.map(value, function(innerValue, innerKey) {
return "" + innerKey + ": " + innerValue + ";";
}).join(' ');
return "" + key + " { " + innerStr + " }";
}).join("\n");
};
Renderer.buildFrame = function(container) {
var iframe, iframeDoc, root;
iframe = container.ownerDocument.createElement('iframe');
dom(iframe).attributes({
frameBorder: '0',
height: '100%',
width: '100%',
title: 'Quill Rich Text Editor',
role: 'presentation'
});
container.appendChild(iframe);
iframeDoc = iframe.contentWindow.document;
iframeDoc.open();
iframeDoc.write('<!DOCTYPE html>');
iframeDoc.close();
root = iframeDoc.createElement('div');
iframeDoc.body.appendChild(root);
return [root, iframe];
};
function Renderer(container, options) {
var _ref;
this.container = container;
this.options = options != null ? options : {};
this.container.innerHTML = '';
_ref = Renderer.buildFrame(this.container), this.root = _ref[0], this.iframe = _ref[1];
this.root.setAttribute('id', this.options.id);
this.iframe.setAttribute('name', this.options.id);
dom(this.root).addClass('editor-container');
dom(this.container).addClass('ql-container');
if (dom.isIOS()) {
dom(this.container).styles({
'overflow': 'auto',
'-webkit-overflow-scrolling': 'touch'
});
}
this.addStyles(DEFAULT_STYLES);
if (this.options.styles != null) {
_.defer(_.bind(this.addStyles, this, this.options.styles));
}
}
Renderer.prototype.addContainer = function(className, before) {
var container, refNode;
if (before == null) {
before = false;
}
refNode = before ? this.root : null;
container = this.root.ownerDocument.createElement('div');
dom(container).addClass(className);
this.root.parentNode.insertBefore(container, refNode);
return container;
};
Renderer.prototype.addStyles = function(css) {
var link, style;
if (typeof css === 'object') {
style = this.root.ownerDocument.createElement('style');
style.type = 'text/css';
css = Renderer.objToCss(css);
style.appendChild(this.root.ownerDocument.createTextNode(css));
return this.root.ownerDocument.head.appendChild(style);
} else if (typeof css === 'string') {
link = this.root.ownerDocument.createElement('link');
dom(link).attributes({
type: 'text/css',
rel: 'stylesheet',
href: css
});
return this.root.ownerDocument.head.appendChild(link);
}
};
return Renderer;
})();
module.exports = Renderer;
+230
View File
@@ -0,0 +1,230 @@
var Leaf, Normalizer, Range, Selection, dom, _;
_ = require('lodash');
dom = require('../lib/dom');
Leaf = require('./leaf');
Normalizer = require('../lib/normalizer');
Range = require('../lib/range');
Selection = (function() {
function Selection(doc, iframe, emitter) {
this.doc = doc;
this.iframe = iframe;
this.emitter = emitter;
this.document = this.doc.root.ownerDocument;
this.focus = false;
this.range = new Range(0, 0);
this.nullDelay = false;
this.update('silent');
}
Selection.prototype.checkFocus = function() {
return this.document.activeElement === this.doc.root && document.activeElement === this.iframe;
};
Selection.prototype.getRange = function(ignoreFocus) {
var end, nativeRange, start;
if (ignoreFocus == null) {
ignoreFocus = false;
}
if (this.checkFocus()) {
nativeRange = this._getNativeRange();
if (nativeRange == null) {
return null;
}
start = this._positionToIndex(nativeRange.startContainer, nativeRange.startOffset);
if (nativeRange.startContainer === nativeRange.endContainer && nativeRange.startOffset === nativeRange.endOffset) {
end = start;
} else {
end = this._positionToIndex(nativeRange.endContainer, nativeRange.endOffset);
}
return new Range(Math.min(start, end), Math.max(start, end));
} else if (ignoreFocus) {
return this.range;
} else {
return null;
}
};
Selection.prototype.preserve = function(fn) {
var endNode, endOffset, nativeRange, startNode, startOffset, _ref, _ref1, _ref2, _ref3;
nativeRange = this._getNativeRange();
if ((nativeRange != null) && this.checkFocus()) {
_ref = this._encodePosition(nativeRange.startContainer, nativeRange.startOffset), startNode = _ref[0], startOffset = _ref[1];
_ref1 = this._encodePosition(nativeRange.endContainer, nativeRange.endOffset), endNode = _ref1[0], endOffset = _ref1[1];
fn();
_ref2 = this._decodePosition(startNode, startOffset), startNode = _ref2[0], startOffset = _ref2[1];
_ref3 = this._decodePosition(endNode, endOffset), endNode = _ref3[0], endOffset = _ref3[1];
return this._setNativeRange(startNode, startOffset, endNode, endOffset);
} else {
return fn();
}
};
Selection.prototype.setRange = function(range, source) {
var endNode, endOffset, startNode, startOffset, _ref, _ref1, _ref2;
if (range != null) {
_ref = this._indexToPosition(range.start), startNode = _ref[0], startOffset = _ref[1];
if (range.isCollapsed()) {
_ref1 = [startNode, startOffset], endNode = _ref1[0], endOffset = _ref1[1];
} else {
_ref2 = this._indexToPosition(range.end), endNode = _ref2[0], endOffset = _ref2[1];
}
this._setNativeRange(startNode, startOffset, endNode, endOffset);
} else {
this._setNativeRange(null);
}
return this.update(source);
};
Selection.prototype.shiftAfter = function(index, length, fn) {
var range;
range = this.getRange();
fn();
if (range != null) {
range.shift(index, length);
return this.setRange(range, 'silent');
}
};
Selection.prototype.update = function(source) {
var emit, focus, range, toEmit;
focus = this.checkFocus();
range = this.getRange(true);
emit = source !== 'silent' && (!Range.compare(range, this.range) || focus !== this.focus);
toEmit = focus ? range : null;
if (toEmit === null && source === 'user' && !this.nullDelay) {
return this.nullDelay = true;
} else {
this.nullDelay = false;
this.range = range;
this.focus = focus;
if (emit) {
return this.emitter.emit(this.emitter.constructor.events.SELECTION_CHANGE, toEmit, source);
}
}
};
Selection.prototype._decodePosition = function(node, offset) {
var childIndex;
if (dom(node).isElement()) {
childIndex = _.indexOf(dom(node.parentNode).childNodes(), node);
offset += childIndex;
node = node.parentNode;
}
return [node, offset];
};
Selection.prototype._encodePosition = function(node, offset) {
var text;
while (true) {
if (dom(node).isTextNode() || node.tagName === dom.DEFAULT_BREAK_TAG || (dom.EMBED_TAGS[node.tagName] != null)) {
return [node, offset];
} else if (offset < node.childNodes.length) {
node = node.childNodes[offset];
offset = 0;
} else if (node.childNodes.length === 0) {
if (Normalizer.TAGS[node.tagName] == null) {
text = node.ownerDocument.createTextNode('');
node.appendChild(text);
node = text;
}
return [node, 0];
} else {
node = node.lastChild;
if (dom(node).isElement()) {
if (node.tagName === dom.DEFAULT_BREAK_TAG || (dom.EMBED_TAGS[node.tagName] != null)) {
return [node, 1];
} else {
offset = node.childNodes.length;
}
} else {
return [node, dom(node).length()];
}
}
}
};
Selection.prototype._getNativeRange = function() {
var range, selection;
selection = this.document.getSelection();
if ((selection != null ? selection.rangeCount : void 0) > 0) {
range = selection.getRangeAt(0);
if (dom(range.startContainer).isAncestor(this.doc.root, true)) {
if (range.startContainer === range.endContainer || dom(range.endContainer).isAncestor(this.doc.root, true)) {
return range;
}
}
}
return null;
};
Selection.prototype._indexToPosition = function(index) {
var leaf, offset, _ref;
if (this.doc.lines.length === 0) {
return [this.doc.root, 0];
}
_ref = this.doc.findLeafAt(index, true), leaf = _ref[0], offset = _ref[1];
return this._decodePosition(leaf.node, offset);
};
Selection.prototype._positionToIndex = function(node, offset) {
var leaf, leafNode, leafOffset, line, lineOffset, _ref;
_ref = this._encodePosition(node, offset), leafNode = _ref[0], offset = _ref[1];
line = this.doc.findLine(leafNode);
if (line == null) {
return 0;
}
leaf = line.findLeaf(leafNode);
lineOffset = 0;
while (line.prev != null) {
line = line.prev;
lineOffset += line.length;
}
if (leaf == null) {
return lineOffset;
}
leafOffset = 0;
while (leaf.prev != null) {
leaf = leaf.prev;
leafOffset += leaf.length;
}
return lineOffset + leafOffset + offset;
};
Selection.prototype._setNativeRange = function(startNode, startOffset, endNode, endOffset) {
var nativeRange, selection;
selection = this.document.getSelection();
if (!selection) {
return;
}
if (startNode != null) {
if (!this.checkFocus()) {
this.doc.root.focus();
}
nativeRange = this._getNativeRange();
if ((nativeRange == null) || startNode !== nativeRange.startContainer || startOffset !== nativeRange.startOffset || endNode !== nativeRange.endContainer || endOffset !== nativeRange.endOffset) {
selection.removeAllRanges();
nativeRange = this.document.createRange();
nativeRange.setStart(startNode, startOffset);
nativeRange.setEnd(endNode, endOffset);
selection.addRange(nativeRange);
if (!this.checkFocus()) {
return this.doc.root.focus();
}
}
} else {
selection.removeAllRanges();
return this.doc.root.blur();
}
};
return Selection;
})();
module.exports = Selection;