Skip to content

Commit

Permalink
implement and use new apis
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jan 21, 2017
1 parent 8cc864c commit f3ed63a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
20 changes: 20 additions & 0 deletions core/quill.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,30 @@ class Quill {
}
}

getIndex(blot) {
return blot.offset(this.scroll);
}

getLength() {
return this.scroll.length();
}

getLeaf(index) {
return this.scroll.leaf(index);
}

getLine(index) {
return this.scroll.line(index);
}

getLines(index = 0, length = Number.MAX_VALUE) {
if (typeof index !== 'number') {
return this.scroll.lines(index.index, index.length);
} else {
return this.scroll.lines(index, length);
}
}

getModule(name) {
return this.theme.modules[name];
}
Expand Down
14 changes: 7 additions & 7 deletions modules/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class Keyboard extends Module {
if (bindings.length === 0) return;
let range = this.quill.getSelection();
if (range == null || !this.quill.hasFocus()) return;
let [line, offset] = this.quill.scroll.line(range.index);
let [leafStart, offsetStart] = this.quill.scroll.leaf(range.index);
let [leafEnd, offsetEnd] = range.length === 0 ? [leafStart, offsetStart] : this.quill.scroll.leaf(range.index + range.length);
let [line, offset] = this.quill.getLine(range.index);
let [leafStart, offsetStart] = this.quill.getLeaf(range.index);
let [leafEnd, offsetEnd] = range.length === 0 ? [leafStart, offsetStart] : this.quill.getLeaf(range.index + range.length);
let prefixText = leafStart instanceof Parchment.Text ? leafStart.value().slice(0, offsetStart) : '';
let suffixText = leafEnd instanceof Parchment.Text ? leafEnd.value().slice(offsetEnd) : '';
let curContext = {
Expand Down Expand Up @@ -208,7 +208,7 @@ Keyboard.DEFAULTS = {
format: { list: 'checked' },
handler: function(range) {
this.quill.scroll.insertAt(range.index, '\n');
let [line, ] = this.quill.scroll.line(range.index + 1);
let [line, ] = this.quill.getLine(range.index + 1);
line.format('list', 'unchecked');
this.quill.update(Quill.sources.USER);
this.quill.setSelection(range.index + 1, Quill.sources.SILENT);
Expand Down Expand Up @@ -257,7 +257,7 @@ Keyboard.DEFAULTS = {

function handleBackspace(range, context) {
if (range.index === 0) return;
let [line, ] = this.quill.scroll.line(range.index);
let [line, ] = this.quill.getLine(range.index);
let formats = {};
if (context.offset === 0) {
let curFormats = line.formats();
Expand Down Expand Up @@ -319,9 +319,9 @@ function makeCodeBlockHandler(indent) {
let index = range.index, length = range.length;
let [block, offset] = this.quill.scroll.descendant(CodeBlock, index);
if (block == null) return;
let scrollOffset = this.quill.scroll.offset(block);
let scrollIndex = this.quill.getIndex(block);
let start = block.newlineIndex(offset, true) + 1;
let end = block.newlineIndex(scrollOffset + offset + length);
let end = block.newlineIndex(scrollIndex + offset + length);
let lines = block.domNode.textContent.slice(start, end).split('\n');
offset = 0;
lines.forEach((line, i) => {
Expand Down
4 changes: 2 additions & 2 deletions themes/bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class BubbleTooltip extends BaseTooltip {
this.root.style.left = '0px';
this.root.style.width = '';
this.root.style.width = this.root.offsetWidth + 'px';
let lines = this.quill.scroll.lines(range.index, range.length);
let lines = this.quill.getLines(range.index, range.length);
if (lines.length === 1) {
this.position(this.quill.getBounds(range));
} else {
let lastLine = lines[lines.length - 1];
let index = lastLine.offset(this.quill.scroll);
let index = this.quill.getIndex(lastLine);
let length = Math.min(lastLine.length() - 1, range.index + range.length - index);
let bounds = this.quill.getBounds(new Range(index, length));
this.position(bounds);
Expand Down

0 comments on commit f3ed63a

Please sign in to comment.