Skip to content

Commit

Permalink
Migrate cursor API usage over to range
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Feb 8, 2016
1 parent 6e972dd commit b56aa16
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 160 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,14 @@ callback function. For example:

```js
editor.didUpdatePost(postEditor => {
let { offsets } = editor.cursor,
cursorSection;
let { range } = editor;
let cursorSection = range.head.section;

if (offset.headSection.text === 'add-section-when-i-type-this') {
if (cursorSection.text === 'add-section-when-i-type-this') {
let section = editor.builder.createMarkupSection('p');
postEditor.insertSectionBefore(section, cursorSection.next);
cursorSection = section;
postEditor.setRange(new Mobiledoc.Range(section.headPosition));
}

postEditor.scheduleRerender();
postEditor.schedule(() => {
if (cursorSection) {
editor.moveToSection(cursorSection, 0);
}
});
});
```

Expand Down
10 changes: 5 additions & 5 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ class Editor {
* @private
*/
handleDeletion(event=null) {
const range = this.cursor.offsets;
let { range } = this;

if (this.cursor.hasSelection()) {
if (!range.isCollapsed) {
this.run(postEditor => {
let nextPosition = postEditor.deleteRange(range);
postEditor.setRange(new Range(nextPosition));
Expand All @@ -247,7 +247,7 @@ class Editor {

event.preventDefault();

let range = this.cursor.offsets;
let { range } = this;
this.run(postEditor => {
let cursorSection;
if (!range.isCollapsed) {
Expand Down Expand Up @@ -386,7 +386,7 @@ class Editor {

get markupsInSelection() {
if (this.cursor.hasCursor()) {
const range = this.cursor.offsets;
let { range } = this;
return this.post.markupsInRange(range);
} else {
return [];
Expand Down Expand Up @@ -477,7 +477,7 @@ class Editor {
*
* Usage:
*
* let markerRange = this.cursor.offsets;
* let markerRange = this.range;
* editor.run((postEditor) => {
* postEditor.deleteRange(markerRange);
* // editing surface not updated yet
Expand Down
38 changes: 19 additions & 19 deletions src/js/editor/key-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ import Browser from '../utils/browser';
export const DEFAULT_KEY_COMMANDS = [{
str: 'META+B',
run(editor) {
if (editor.cursor.hasSelection()) {
editor.run(postEditor => postEditor.toggleMarkup('strong'));
} else {
if (editor.range.isCollapsed) {
document.execCommand('bold', false, null);
} else {
editor.run(postEditor => postEditor.toggleMarkup('strong'));
}
}
}, {
str: 'CTRL+B',
run(editor) {
if (editor.cursor.hasSelection()) {
editor.run(postEditor => postEditor.toggleMarkup('strong'));
} else {
if (editor.range.isCollapsed) {
document.execCommand('bold', false, null);
} else {
editor.run(postEditor => postEditor.toggleMarkup('strong'));
}
}
}, {
str: 'META+I',
run(editor) {
if (editor.cursor.hasSelection()) {
editor.run(postEditor => postEditor.toggleMarkup('em'));
} else {
if (editor.range.isCollapsed) {
document.execCommand('italic', false, null);
} else {
editor.run(postEditor => postEditor.toggleMarkup('em'));
}
}
}, {
str: 'CTRL+I',
run(editor) {
if (editor.cursor.hasSelection()) {
editor.run(postEditor => postEditor.toggleMarkup('em'));
} else {
if (editor.range.isCollapsed) {
document.execCommand('italic', false, null);
} else {
editor.run(postEditor => postEditor.toggleMarkup('em'));
}
}
}, {
str: 'CTRL+K',
run(editor) {
let range = editor.cursor.offsets;
if (!editor.cursor.hasSelection()) {
range.tail = range.head.section.tailPosition();
let { range } = editor;
if (range.isCollapsed) {
range = new Range(range.head, range.head.section.tailPosition());
}
editor.run(postEditor => {
let nextPosition = postEditor.deleteRange(range);
Expand All @@ -59,7 +59,7 @@ export const DEFAULT_KEY_COMMANDS = [{
if (!Browser.isMac) {
return false;
}
let range = editor.cursor.offsets;
let {range} = editor;
let {head: {section}} = range;
editor.run(postEditor => {
postEditor.setRange(new Range(section.headPosition()));
Expand All @@ -71,7 +71,7 @@ export const DEFAULT_KEY_COMMANDS = [{
if (!Browser.isMac) {
return false;
}
let range = editor.cursor.offsets;
let {range} = editor;
let {tail: {section}} = range;
editor.run(postEditor => {
postEditor.setRange(new Range(section.tailPosition()));
Expand All @@ -80,15 +80,15 @@ export const DEFAULT_KEY_COMMANDS = [{
}, {
str: 'META+K',
run(editor) {
if (!editor.cursor.hasSelection()) {
if (editor.range.isCollapsed) {
return;
}

let selectedText = editor.cursor.selectedText();
let defaultUrl = '';
if (selectedText.indexOf('http') !== -1) { defaultUrl = selectedText; }

let range = editor.range;
let {range} = editor;
let hasLink = editor.detectMarkupInRange(range, 'a');

if (hasLink) {
Expand Down
22 changes: 11 additions & 11 deletions src/js/editor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PostEditor {
*
* Usage:
*
* const range = editor.cursor.offsets;
* let { range } = editor;
* editor.run((postEditor) => {
* postEditor.deleteRange(range);
* });
Expand Down Expand Up @@ -531,7 +531,7 @@ class PostEditor {
/**
* Split markers at two positions, once at the head, and if necessary once
* at the tail. This method is designed to accept a range
* (e.g. `editor.cursor.offsets`) as an argument.
* (e.g. `editor.range`) as an argument.
*
* Usage:
*
Expand Down Expand Up @@ -772,14 +772,14 @@ class PostEditor {
}

/**
* Given a markerRange (for example `editor.cursor.offsets`) mark all markers
* Given a markerRange (for example `editor.range`) mark all markers
* inside it as a given markup. The markup must be provided as a post
* abstract node.
*
* Usage:
*
* const range = editor.cursor.offsets;
* const strongMarkup = editor.builder.createMarkup('strong');
* let range = editor.range;
* let strongMarkup = editor.builder.createMarkup('strong');
* editor.run((postEditor) => {
* postEditor.addMarkupToRange(range, strongMarkup);
* });
Expand All @@ -805,13 +805,13 @@ class PostEditor {
}

/**
* Given a markerRange (for example `editor.cursor.offsets`) remove the given
* Given a markerRange (for example `editor.range`) remove the given
* markup from all contained markers.
*
* Usage:
*
* const range = editor.cursor.offsets;
* const markup = markerRange.headMarker.markups[0];
* let { range } = editor;
* let markup = markerRange.headMarker.markups[0];
* editor.run(postEditor => {
* postEditor.removeMarkupFromRange(range, markup);
* });
Expand Down Expand Up @@ -1105,7 +1105,7 @@ class PostEditor {
*
* Usage:
*
* let markerRange = editor.cursor.offsets;
* let markerRange = editor.range;
* let sectionWithCursor = markerRange.headMarker.section;
* let section = editor.builder.createCardSection('my-image');
* let collection = sectionWithCursor.parent.sections;
Expand Down Expand Up @@ -1168,8 +1168,8 @@ class PostEditor {
*
* Usage:
*
* const range = editor.cursor.offsets;
* const sectionWithCursor = range.head.section;
* let { range } = editor;
* let sectionWithCursor = range.head.section;
* editor.run((postEditor) => {
* postEditor.removeSection(sectionWithCursor);
* });
Expand Down
7 changes: 3 additions & 4 deletions src/js/utils/paste-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ function parsePostFromText(text, builder, plugins) {
* @return null
*/
export function setClipboardCopyData(copyEvent, editor) {
const { cursor, post } = editor;
const { clipboardData } = copyEvent;
let { range, post } = editor;
let { clipboardData } = copyEvent;

const range = cursor.offsets;
const mobiledoc = post.cloneRange(range);
let mobiledoc = post.cloneRange(range);

let unknownCardHandler = () => {}; // ignore unknown cards
let unknownAtomHandler = () => {}; // ignore unknown atoms
Expand Down
Loading

0 comments on commit b56aa16

Please sign in to comment.