Initial commit as of 2018-10-16
This commit is contained in:
174
assets/js/blots/block.js
Normal file
174
assets/js/blots/block.js
Normal file
@@ -0,0 +1,174 @@
|
||||
import extend from 'extend';
|
||||
import Delta from 'quill-delta';
|
||||
import Parchment from 'parchment';
|
||||
import Break from './break';
|
||||
import Inline from './inline';
|
||||
import TextBlot from './text';
|
||||
|
||||
|
||||
const NEWLINE_LENGTH = 1;
|
||||
|
||||
|
||||
class BlockEmbed extends Parchment.Embed {
|
||||
attach() {
|
||||
super.attach();
|
||||
this.attributes = new Parchment.Attributor.Store(this.domNode);
|
||||
}
|
||||
|
||||
delta() {
|
||||
return new Delta().insert(this.value(), extend(this.formats(), this.attributes.values()));
|
||||
}
|
||||
|
||||
format(name, value) {
|
||||
let attribute = Parchment.query(name, Parchment.Scope.BLOCK_ATTRIBUTE);
|
||||
if (attribute != null) {
|
||||
this.attributes.attribute(attribute, value);
|
||||
}
|
||||
}
|
||||
|
||||
formatAt(index, length, name, value) {
|
||||
this.format(name, value);
|
||||
}
|
||||
|
||||
insertAt(index, value, def) {
|
||||
if (typeof value === 'string' && value.endsWith('\n')) {
|
||||
let block = Parchment.create(Block.blotName);
|
||||
this.parent.insertBefore(block, index === 0 ? this : this.next);
|
||||
block.insertAt(0, value.slice(0, -1));
|
||||
} else {
|
||||
super.insertAt(index, value, def);
|
||||
}
|
||||
}
|
||||
}
|
||||
BlockEmbed.scope = Parchment.Scope.BLOCK_BLOT;
|
||||
// It is important for cursor behavior BlockEmbeds use tags that are block level elements
|
||||
|
||||
|
||||
class Block extends Parchment.Block {
|
||||
constructor(domNode) {
|
||||
super(domNode);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
delta() {
|
||||
if (this.cache.delta == null) {
|
||||
this.cache.delta = this.descendants(Parchment.Leaf).reduce((delta, leaf) => {
|
||||
if (leaf.length() === 0) {
|
||||
return delta;
|
||||
} else {
|
||||
return delta.insert(leaf.value(), bubbleFormats(leaf));
|
||||
}
|
||||
}, new Delta()).insert('\n', bubbleFormats(this));
|
||||
}
|
||||
return this.cache.delta;
|
||||
}
|
||||
|
||||
deleteAt(index, length) {
|
||||
super.deleteAt(index, length);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
formatAt(index, length, name, value) {
|
||||
if (length <= 0) return;
|
||||
if (Parchment.query(name, Parchment.Scope.BLOCK)) {
|
||||
if (index + length === this.length()) {
|
||||
this.format(name, value);
|
||||
}
|
||||
} else {
|
||||
super.formatAt(index, Math.min(length, this.length() - index - 1), name, value);
|
||||
}
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
insertAt(index, value, def) {
|
||||
if (def != null) return super.insertAt(index, value, def);
|
||||
if (value.length === 0) return;
|
||||
let lines = value.split('\n');
|
||||
let text = lines.shift();
|
||||
if (text.length > 0) {
|
||||
if (index < this.length() - 1 || this.children.tail == null) {
|
||||
super.insertAt(Math.min(index, this.length() - 1), text);
|
||||
} else {
|
||||
this.children.tail.insertAt(this.children.tail.length(), text);
|
||||
}
|
||||
this.cache = {};
|
||||
}
|
||||
let block = this;
|
||||
lines.reduce(function(index, line) {
|
||||
block = block.split(index, true);
|
||||
block.insertAt(0, line);
|
||||
return line.length;
|
||||
}, index + text.length);
|
||||
}
|
||||
|
||||
insertBefore(blot, ref) {
|
||||
let head = this.children.head;
|
||||
super.insertBefore(blot, ref);
|
||||
if (head instanceof Break) {
|
||||
head.remove();
|
||||
}
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
length() {
|
||||
if (this.cache.length == null) {
|
||||
this.cache.length = super.length() + NEWLINE_LENGTH;
|
||||
}
|
||||
return this.cache.length;
|
||||
}
|
||||
|
||||
moveChildren(target, ref) {
|
||||
super.moveChildren(target, ref);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
optimize(context) {
|
||||
super.optimize(context);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
path(index) {
|
||||
return super.path(index, true);
|
||||
}
|
||||
|
||||
removeChild(child) {
|
||||
super.removeChild(child);
|
||||
this.cache = {};
|
||||
}
|
||||
|
||||
split(index, force = false) {
|
||||
if (force && (index === 0 || index >= this.length() - NEWLINE_LENGTH)) {
|
||||
let clone = this.clone();
|
||||
if (index === 0) {
|
||||
this.parent.insertBefore(clone, this);
|
||||
return this;
|
||||
} else {
|
||||
this.parent.insertBefore(clone, this.next);
|
||||
return clone;
|
||||
}
|
||||
} else {
|
||||
let next = super.split(index, force);
|
||||
this.cache = {};
|
||||
return next;
|
||||
}
|
||||
}
|
||||
}
|
||||
Block.blotName = 'block';
|
||||
Block.tagName = 'P';
|
||||
Block.defaultChild = 'break';
|
||||
Block.allowedChildren = [Inline, Parchment.Embed, TextBlot];
|
||||
|
||||
|
||||
function bubbleFormats(blot, formats = {}) {
|
||||
if (blot == null) return formats;
|
||||
if (typeof blot.formats === 'function') {
|
||||
formats = extend(formats, blot.formats());
|
||||
}
|
||||
if (blot.parent == null || blot.parent.blotName == 'scroll' || blot.parent.statics.scope !== blot.statics.scope) {
|
||||
return formats;
|
||||
}
|
||||
return bubbleFormats(blot.parent, formats);
|
||||
}
|
||||
|
||||
|
||||
export { bubbleFormats, BlockEmbed, Block as default };
|
29
assets/js/blots/break.js
Normal file
29
assets/js/blots/break.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import Parchment from 'parchment';
|
||||
|
||||
|
||||
class Break extends Parchment.Embed {
|
||||
static value() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
insertInto(parent, ref) {
|
||||
if (parent.children.length === 0) {
|
||||
super.insertInto(parent, ref);
|
||||
} else {
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
|
||||
length() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
value() {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Break.blotName = 'break';
|
||||
Break.tagName = 'BR';
|
||||
|
||||
|
||||
export default Break;
|
9
assets/js/blots/container.js
Normal file
9
assets/js/blots/container.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import Parchment from 'parchment';
|
||||
import Block, { BlockEmbed } from './block';
|
||||
|
||||
|
||||
class Container extends Parchment.Container { }
|
||||
Container.allowedChildren = [Block, BlockEmbed, Container];
|
||||
|
||||
|
||||
export default Container;
|
116
assets/js/blots/cursor.js
Normal file
116
assets/js/blots/cursor.js
Normal file
@@ -0,0 +1,116 @@
|
||||
import Parchment from 'parchment';
|
||||
import TextBlot from './text';
|
||||
|
||||
|
||||
class Cursor extends Parchment.Embed {
|
||||
static value() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
constructor(domNode, selection) {
|
||||
super(domNode);
|
||||
this.selection = selection;
|
||||
this.textNode = document.createTextNode(Cursor.CONTENTS);
|
||||
this.domNode.appendChild(this.textNode);
|
||||
this._length = 0;
|
||||
}
|
||||
|
||||
detach() {
|
||||
// super.detach() will also clear domNode.__blot
|
||||
if (this.parent != null) this.parent.removeChild(this);
|
||||
}
|
||||
|
||||
format(name, value) {
|
||||
if (this._length !== 0) {
|
||||
return super.format(name, value);
|
||||
}
|
||||
let target = this, index = 0;
|
||||
while (target != null && target.statics.scope !== Parchment.Scope.BLOCK_BLOT) {
|
||||
index += target.offset(target.parent);
|
||||
target = target.parent;
|
||||
}
|
||||
if (target != null) {
|
||||
this._length = Cursor.CONTENTS.length;
|
||||
target.optimize();
|
||||
target.formatAt(index, Cursor.CONTENTS.length, name, value);
|
||||
this._length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
index(node, offset) {
|
||||
if (node === this.textNode) return 0;
|
||||
return super.index(node, offset);
|
||||
}
|
||||
|
||||
length() {
|
||||
return this._length;
|
||||
}
|
||||
|
||||
position() {
|
||||
return [this.textNode, this.textNode.data.length];
|
||||
}
|
||||
|
||||
remove() {
|
||||
super.remove();
|
||||
this.parent = null;
|
||||
}
|
||||
|
||||
restore() {
|
||||
if (this.selection.composing || this.parent == null) return;
|
||||
let textNode = this.textNode;
|
||||
let range = this.selection.getNativeRange();
|
||||
let restoreText, start, end;
|
||||
if (range != null && range.start.node === textNode && range.end.node === textNode) {
|
||||
[restoreText, start, end] = [textNode, range.start.offset, range.end.offset];
|
||||
}
|
||||
// Link format will insert text outside of anchor tag
|
||||
while (this.domNode.lastChild != null && this.domNode.lastChild !== this.textNode) {
|
||||
this.domNode.parentNode.insertBefore(this.domNode.lastChild, this.domNode);
|
||||
}
|
||||
if (this.textNode.data !== Cursor.CONTENTS) {
|
||||
let text = this.textNode.data.split(Cursor.CONTENTS).join('');
|
||||
if (this.next instanceof TextBlot) {
|
||||
restoreText = this.next.domNode;
|
||||
this.next.insertAt(0, text);
|
||||
this.textNode.data = Cursor.CONTENTS;
|
||||
} else {
|
||||
this.textNode.data = text;
|
||||
this.parent.insertBefore(Parchment.create(this.textNode), this);
|
||||
this.textNode = document.createTextNode(Cursor.CONTENTS);
|
||||
this.domNode.appendChild(this.textNode);
|
||||
}
|
||||
}
|
||||
this.remove();
|
||||
if (start != null) {
|
||||
[start, end] = [start, end].map(function(offset) {
|
||||
return Math.max(0, Math.min(restoreText.data.length, offset - 1));
|
||||
});
|
||||
return {
|
||||
startNode: restoreText,
|
||||
startOffset: start,
|
||||
endNode: restoreText,
|
||||
endOffset: end
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
update(mutations, context) {
|
||||
if (mutations.some((mutation) => {
|
||||
return mutation.type === 'characterData' && mutation.target === this.textNode;
|
||||
})) {
|
||||
let range = this.restore();
|
||||
if (range) context.range = range;
|
||||
}
|
||||
}
|
||||
|
||||
value() {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Cursor.blotName = 'cursor';
|
||||
Cursor.className = 'ql-cursor';
|
||||
Cursor.tagName = 'span';
|
||||
Cursor.CONTENTS = "\uFEFF"; // Zero width no break space
|
||||
|
||||
|
||||
export default Cursor;
|
79
assets/js/blots/embed.js
Normal file
79
assets/js/blots/embed.js
Normal file
@@ -0,0 +1,79 @@
|
||||
import Parchment from 'parchment';
|
||||
import TextBlot from './text';
|
||||
|
||||
const GUARD_TEXT = "\uFEFF";
|
||||
|
||||
|
||||
class Embed extends Parchment.Embed {
|
||||
constructor(node) {
|
||||
super(node);
|
||||
this.contentNode = document.createElement('span');
|
||||
this.contentNode.setAttribute('contenteditable', false);
|
||||
[].slice.call(this.domNode.childNodes).forEach((childNode) => {
|
||||
this.contentNode.appendChild(childNode);
|
||||
});
|
||||
this.leftGuard = document.createTextNode(GUARD_TEXT);
|
||||
this.rightGuard = document.createTextNode(GUARD_TEXT);
|
||||
this.domNode.appendChild(this.leftGuard);
|
||||
this.domNode.appendChild(this.contentNode);
|
||||
this.domNode.appendChild(this.rightGuard);
|
||||
}
|
||||
|
||||
index(node, offset) {
|
||||
if (node === this.leftGuard) return 0;
|
||||
if (node === this.rightGuard) return 1;
|
||||
return super.index(node, offset);
|
||||
}
|
||||
|
||||
restore(node) {
|
||||
let range, textNode;
|
||||
let text = node.data.split(GUARD_TEXT).join('');
|
||||
if (node === this.leftGuard) {
|
||||
if (this.prev instanceof TextBlot) {
|
||||
let prevLength = this.prev.length();
|
||||
this.prev.insertAt(prevLength, text);
|
||||
range = {
|
||||
startNode: this.prev.domNode,
|
||||
startOffset: prevLength + text.length
|
||||
};
|
||||
} else {
|
||||
textNode = document.createTextNode(text);
|
||||
this.parent.insertBefore(Parchment.create(textNode), this);
|
||||
range = {
|
||||
startNode: textNode,
|
||||
startOffset: text.length
|
||||
};
|
||||
}
|
||||
} else if (node === this.rightGuard) {
|
||||
if (this.next instanceof TextBlot) {
|
||||
this.next.insertAt(0, text);
|
||||
range = {
|
||||
startNode: this.next.domNode,
|
||||
startOffset: text.length
|
||||
}
|
||||
} else {
|
||||
textNode = document.createTextNode(text);
|
||||
this.parent.insertBefore(Parchment.create(textNode), this.next);
|
||||
range = {
|
||||
startNode: textNode,
|
||||
startOffset: text.length
|
||||
};
|
||||
}
|
||||
}
|
||||
node.data = GUARD_TEXT;
|
||||
return range;
|
||||
}
|
||||
|
||||
update(mutations, context) {
|
||||
mutations.forEach((mutation) => {
|
||||
if (mutation.type === 'characterData' &&
|
||||
(mutation.target === this.leftGuard || mutation.target === this.rightGuard)) {
|
||||
let range = this.restore(mutation.target);
|
||||
if (range) context.range = range;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default Embed;
|
50
assets/js/blots/inline.js
Normal file
50
assets/js/blots/inline.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import Text from './text';
|
||||
import Parchment from 'parchment';
|
||||
|
||||
|
||||
class Inline extends Parchment.Inline {
|
||||
static compare(self, other) {
|
||||
let selfIndex = Inline.order.indexOf(self);
|
||||
let otherIndex = Inline.order.indexOf(other);
|
||||
if (selfIndex >= 0 || otherIndex >= 0) {
|
||||
return selfIndex - otherIndex;
|
||||
} else if (self === other) {
|
||||
return 0;
|
||||
} else if (self < other) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
formatAt(index, length, name, value) {
|
||||
if (Inline.compare(this.statics.blotName, name) < 0 && Parchment.query(name, Parchment.Scope.BLOT)) {
|
||||
let blot = this.isolate(index, length);
|
||||
if (value) {
|
||||
blot.wrap(name, value);
|
||||
}
|
||||
} else {
|
||||
super.formatAt(index, length, name, value);
|
||||
}
|
||||
}
|
||||
|
||||
optimize(context) {
|
||||
super.optimize(context);
|
||||
if (this.parent instanceof Inline &&
|
||||
Inline.compare(this.statics.blotName, this.parent.statics.blotName) > 0) {
|
||||
let parent = this.parent.isolate(this.offset(), this.length());
|
||||
this.moveChildren(parent);
|
||||
parent.wrap(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Inline.allowedChildren = [Inline, Parchment.Embed, Text];
|
||||
// Lower index means deeper in the DOM tree, since not found (-1) is for embeds
|
||||
Inline.order = [
|
||||
'cursor', 'inline', // Must be lower
|
||||
'underline', 'strike', 'italic', 'bold', 'script',
|
||||
'link', 'code' // Must be higher
|
||||
];
|
||||
|
||||
|
||||
export default Inline;
|
173
assets/js/blots/scroll.js
Normal file
173
assets/js/blots/scroll.js
Normal file
@@ -0,0 +1,173 @@
|
||||
import Parchment from 'parchment';
|
||||
import Emitter from '../core/emitter';
|
||||
import Block, { BlockEmbed } from './block';
|
||||
import Break from './break';
|
||||
import CodeBlock from '../formats/code';
|
||||
import Container from './container';
|
||||
|
||||
|
||||
function isLine(blot) {
|
||||
return (blot instanceof Block || blot instanceof BlockEmbed);
|
||||
}
|
||||
|
||||
|
||||
class Scroll extends Parchment.Scroll {
|
||||
constructor(domNode, config) {
|
||||
super(domNode);
|
||||
this.emitter = config.emitter;
|
||||
if (Array.isArray(config.whitelist)) {
|
||||
this.whitelist = config.whitelist.reduce(function(whitelist, format) {
|
||||
whitelist[format] = true;
|
||||
return whitelist;
|
||||
}, {});
|
||||
}
|
||||
// Some reason fixes composition issues with character languages in Windows/Chrome, Safari
|
||||
this.domNode.addEventListener('DOMNodeInserted', function() {});
|
||||
this.optimize();
|
||||
this.enable();
|
||||
}
|
||||
|
||||
batchStart() {
|
||||
this.batch = true;
|
||||
}
|
||||
|
||||
batchEnd() {
|
||||
this.batch = false;
|
||||
this.optimize();
|
||||
}
|
||||
|
||||
deleteAt(index, length) {
|
||||
let [first, offset] = this.line(index);
|
||||
let [last, ] = this.line(index + length);
|
||||
super.deleteAt(index, length);
|
||||
if (last != null && first !== last && offset > 0) {
|
||||
if (first instanceof BlockEmbed || last instanceof BlockEmbed) {
|
||||
this.optimize();
|
||||
return;
|
||||
}
|
||||
if (first instanceof CodeBlock) {
|
||||
let newlineIndex = first.newlineIndex(first.length(), true);
|
||||
if (newlineIndex > -1) {
|
||||
first = first.split(newlineIndex + 1);
|
||||
if (first === last) {
|
||||
this.optimize();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (last instanceof CodeBlock) {
|
||||
let newlineIndex = last.newlineIndex(0);
|
||||
if (newlineIndex > -1) {
|
||||
last.split(newlineIndex + 1);
|
||||
}
|
||||
}
|
||||
let ref = last.children.head instanceof Break ? null : last.children.head;
|
||||
first.moveChildren(last, ref);
|
||||
first.remove();
|
||||
}
|
||||
this.optimize();
|
||||
}
|
||||
|
||||
enable(enabled = true) {
|
||||
this.domNode.setAttribute('contenteditable', enabled);
|
||||
}
|
||||
|
||||
formatAt(index, length, format, value) {
|
||||
if (this.whitelist != null && !this.whitelist[format]) return;
|
||||
super.formatAt(index, length, format, value);
|
||||
this.optimize();
|
||||
}
|
||||
|
||||
insertAt(index, value, def) {
|
||||
if (def != null && this.whitelist != null && !this.whitelist[value]) return;
|
||||
if (index >= this.length()) {
|
||||
if (def == null || Parchment.query(value, Parchment.Scope.BLOCK) == null) {
|
||||
let blot = Parchment.create(this.statics.defaultChild);
|
||||
this.appendChild(blot);
|
||||
if (def == null && value.endsWith('\n')) {
|
||||
value = value.slice(0, -1);
|
||||
}
|
||||
blot.insertAt(0, value, def);
|
||||
} else {
|
||||
let embed = Parchment.create(value, def);
|
||||
this.appendChild(embed);
|
||||
}
|
||||
} else {
|
||||
super.insertAt(index, value, def);
|
||||
}
|
||||
this.optimize();
|
||||
}
|
||||
|
||||
insertBefore(blot, ref) {
|
||||
if (blot.statics.scope === Parchment.Scope.INLINE_BLOT) {
|
||||
let wrapper = Parchment.create(this.statics.defaultChild);
|
||||
wrapper.appendChild(blot);
|
||||
blot = wrapper;
|
||||
}
|
||||
super.insertBefore(blot, ref);
|
||||
}
|
||||
|
||||
leaf(index) {
|
||||
return this.path(index).pop() || [null, -1];
|
||||
}
|
||||
|
||||
line(index) {
|
||||
if (index === this.length()) {
|
||||
return this.line(index - 1);
|
||||
}
|
||||
return this.descendant(isLine, index);
|
||||
}
|
||||
|
||||
lines(index = 0, length = Number.MAX_VALUE) {
|
||||
let getLines = (blot, index, length) => {
|
||||
let lines = [], lengthLeft = length;
|
||||
blot.children.forEachAt(index, length, function(child, index, length) {
|
||||
if (isLine(child)) {
|
||||
lines.push(child);
|
||||
} else if (child instanceof Parchment.Container) {
|
||||
lines = lines.concat(getLines(child, index, lengthLeft));
|
||||
}
|
||||
lengthLeft -= length;
|
||||
});
|
||||
return lines;
|
||||
};
|
||||
return getLines(this, index, length);
|
||||
}
|
||||
|
||||
optimize(mutations = [], context = {}) {
|
||||
if (this.batch === true) return;
|
||||
super.optimize(mutations, context);
|
||||
if (mutations.length > 0) {
|
||||
this.emitter.emit(Emitter.events.SCROLL_OPTIMIZE, mutations, context);
|
||||
}
|
||||
}
|
||||
|
||||
path(index) {
|
||||
return super.path(index).slice(1); // Exclude self
|
||||
}
|
||||
|
||||
update(mutations) {
|
||||
if (this.batch === true) return;
|
||||
let source = Emitter.sources.USER;
|
||||
if (typeof mutations === 'string') {
|
||||
source = mutations;
|
||||
}
|
||||
if (!Array.isArray(mutations)) {
|
||||
mutations = this.observer.takeRecords();
|
||||
}
|
||||
if (mutations.length > 0) {
|
||||
this.emitter.emit(Emitter.events.SCROLL_BEFORE_UPDATE, source, mutations);
|
||||
}
|
||||
super.update(mutations.concat([])); // pass copy
|
||||
if (mutations.length > 0) {
|
||||
this.emitter.emit(Emitter.events.SCROLL_UPDATE, source, mutations);
|
||||
}
|
||||
}
|
||||
}
|
||||
Scroll.blotName = 'scroll';
|
||||
Scroll.className = 'ql-editor';
|
||||
Scroll.tagName = 'DIV';
|
||||
Scroll.defaultChild = 'block';
|
||||
Scroll.allowedChildren = [Block, BlockEmbed, Container];
|
||||
|
||||
|
||||
export default Scroll;
|
5
assets/js/blots/text.js
Normal file
5
assets/js/blots/text.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import Parchment from 'parchment';
|
||||
|
||||
class TextBlot extends Parchment.Text { }
|
||||
|
||||
export default TextBlot;
|
Reference in New Issue
Block a user