-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
151 changed files
with
111,955 additions
and
247 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
var _utilsPlaceholderImageSrc = require('../utils/placeholder-image-src'); | ||
|
||
exports['default'] = { | ||
name: 'image', | ||
type: 'dom', | ||
|
||
render: function render(_ref) { | ||
var env = _ref.env; | ||
var options = _ref.options; | ||
var payload = _ref.payload; | ||
|
||
var img = document.createElement('img'); | ||
img.src = payload.src || _utilsPlaceholderImageSrc['default']; | ||
return img; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
'use strict'; | ||
|
||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })(); | ||
|
||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
|
||
var _parsersMobiledoc = require('../parsers/mobiledoc'); | ||
|
||
var _utilsFixedQueue = require('../utils/fixed-queue'); | ||
|
||
function findLeafSectionAtIndex(post, index) { | ||
var section = undefined; | ||
post.walkAllLeafSections(function (_section, _index) { | ||
if (index === _index) { | ||
section = _section; | ||
} | ||
}); | ||
return section; | ||
} | ||
|
||
var Snapshot = (function () { | ||
function Snapshot(editor) { | ||
_classCallCheck(this, Snapshot); | ||
|
||
this.mobiledoc = editor.serialize(); | ||
this.editor = editor; | ||
|
||
this.snapshotRange(); | ||
} | ||
|
||
_createClass(Snapshot, [{ | ||
key: 'snapshotRange', | ||
value: function snapshotRange() { | ||
var _editor = this.editor; | ||
var range = _editor.range; | ||
var cursor = _editor.cursor; | ||
|
||
if (cursor.hasCursor() && !range.isBlank) { | ||
var head = range.head; | ||
var tail = range.tail; | ||
|
||
this.range = { | ||
head: [head.leafSectionIndex, head.offset], | ||
tail: [tail.leafSectionIndex, tail.offset] | ||
}; | ||
} | ||
} | ||
}, { | ||
key: 'getRange', | ||
value: function getRange(post) { | ||
if (this.range) { | ||
var _range = this.range; | ||
var head = _range.head; | ||
var tail = _range.tail; | ||
var _head = head; | ||
|
||
var _head2 = _slicedToArray(_head, 2); | ||
|
||
var headLeafSectionIndex = _head2[0]; | ||
var headOffset = _head2[1]; | ||
var _tail = tail; | ||
|
||
var _tail2 = _slicedToArray(_tail, 2); | ||
|
||
var tailLeafSectionIndex = _tail2[0]; | ||
var tailOffset = _tail2[1]; | ||
|
||
var headSection = findLeafSectionAtIndex(post, headLeafSectionIndex); | ||
var tailSection = findLeafSectionAtIndex(post, tailLeafSectionIndex); | ||
|
||
head = headSection.toPosition(headOffset); | ||
tail = tailSection.toPosition(tailOffset); | ||
|
||
return head.toRange(tail); | ||
} | ||
} | ||
}]); | ||
|
||
return Snapshot; | ||
})(); | ||
|
||
exports.Snapshot = Snapshot; | ||
|
||
var EditHistory = (function () { | ||
function EditHistory(editor, queueLength) { | ||
_classCallCheck(this, EditHistory); | ||
|
||
this.editor = editor; | ||
this._undoStack = new _utilsFixedQueue['default'](queueLength); | ||
this._redoStack = new _utilsFixedQueue['default'](queueLength); | ||
|
||
this._pendingSnapshot = null; | ||
} | ||
|
||
_createClass(EditHistory, [{ | ||
key: 'snapshot', | ||
value: function snapshot() { | ||
// update the current snapshot with the range read from DOM | ||
if (this._pendingSnapshot) { | ||
this._pendingSnapshot.snapshotRange(); | ||
} | ||
} | ||
}, { | ||
key: 'storeSnapshot', | ||
value: function storeSnapshot() { | ||
// store pending snapshot | ||
if (this._pendingSnapshot) { | ||
this._undoStack.push(this._pendingSnapshot); | ||
this._redoStack.clear(); | ||
} | ||
|
||
// take new pending snapshot to store next time `storeSnapshot` is called | ||
this._pendingSnapshot = new Snapshot(this.editor); | ||
} | ||
}, { | ||
key: 'stepBackward', | ||
value: function stepBackward(postEditor) { | ||
// Throw away the pending snapshot | ||
this._pendingSnapshot = null; | ||
|
||
var snapshot = this._undoStack.pop(); | ||
if (snapshot) { | ||
this._redoStack.push(new Snapshot(this.editor)); | ||
this._restoreFromSnapshot(snapshot, postEditor); | ||
} | ||
} | ||
}, { | ||
key: 'stepForward', | ||
value: function stepForward(postEditor) { | ||
var snapshot = this._redoStack.pop(); | ||
if (snapshot) { | ||
this._undoStack.push(new Snapshot(this.editor)); | ||
this._restoreFromSnapshot(snapshot, postEditor); | ||
} | ||
postEditor.cancelSnapshot(); | ||
} | ||
}, { | ||
key: '_restoreFromSnapshot', | ||
value: function _restoreFromSnapshot(snapshot, postEditor) { | ||
var mobiledoc = snapshot.mobiledoc; | ||
var editor = this.editor; | ||
var builder = editor.builder; | ||
var post = editor.post; | ||
|
||
var restoredPost = _parsersMobiledoc['default'].parse(builder, mobiledoc); | ||
|
||
postEditor.removeAllSections(); | ||
postEditor.migrateSectionsFromPost(restoredPost); | ||
|
||
// resurrect snapshotted range if it exists | ||
var newRange = snapshot.getRange(post); | ||
if (newRange) { | ||
postEditor.setRange(newRange); | ||
} | ||
} | ||
}]); | ||
|
||
return EditHistory; | ||
})(); | ||
|
||
exports['default'] = EditHistory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
'use strict'; | ||
|
||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
|
||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
|
||
var _utilsArrayUtils = require('../utils/array-utils'); | ||
|
||
var _utilsCursorRange = require('../utils/cursor/range'); | ||
|
||
/** | ||
* Used by {@link Editor} to manage its current state (cursor, active markups | ||
* and active sections). | ||
* @private | ||
*/ | ||
|
||
var EditState = (function () { | ||
function EditState(editor) { | ||
_classCallCheck(this, EditState); | ||
|
||
this.editor = editor; | ||
|
||
var defaultState = { | ||
range: _utilsCursorRange['default'].blankRange(), | ||
activeMarkups: [], | ||
activeSections: [], | ||
activeSectionTagNames: [] | ||
}; | ||
|
||
this.prevState = this.state = defaultState; | ||
} | ||
|
||
_createClass(EditState, [{ | ||
key: 'updateRange', | ||
value: function updateRange(newRange) { | ||
this.prevState = this.state; | ||
this.state = this._readState(newRange); | ||
} | ||
}, { | ||
key: 'destroy', | ||
value: function destroy() { | ||
this.editor = null; | ||
this.prevState = this.state = null; | ||
} | ||
|
||
/** | ||
* @return {Boolean} | ||
*/ | ||
}, { | ||
key: 'rangeDidChange', | ||
value: function rangeDidChange() { | ||
var range = this.state.range; | ||
var prevRange = this.prevState.range; | ||
|
||
return !prevRange.isEqual(range); | ||
} | ||
|
||
/** | ||
* @return {Boolean} Whether the input mode (active markups or active section tag names) | ||
* has changed. | ||
*/ | ||
}, { | ||
key: 'inputModeDidChange', | ||
value: function inputModeDidChange() { | ||
var state = this.state; | ||
var prevState = this.prevState; | ||
|
||
return !(0, _utilsArrayUtils.isArrayEqual)(state.activeMarkups, prevState.activeMarkups) || !(0, _utilsArrayUtils.isArrayEqual)(state.activeSectionTagNames, prevState.activeSectionTagNames); | ||
} | ||
|
||
/** | ||
* @return {Range} | ||
*/ | ||
}, { | ||
key: 'toggleMarkupState', | ||
|
||
/** | ||
* Update the editor's markup state. This is used when, e.g., | ||
* a user types meta+B when the editor has a cursor but no selected text; | ||
* in this case the editor needs to track that it has an active "b" markup | ||
* and apply it to the next text the user types. | ||
*/ | ||
value: function toggleMarkupState(markup) { | ||
if ((0, _utilsArrayUtils.contains)(this.activeMarkups, markup)) { | ||
this._removeActiveMarkup(markup); | ||
} else { | ||
this._addActiveMarkup(markup); | ||
} | ||
} | ||
}, { | ||
key: '_readState', | ||
value: function _readState(range) { | ||
var state = { | ||
range: range, | ||
activeMarkups: this._readActiveMarkups(range), | ||
activeSections: this._readActiveSections(range) | ||
}; | ||
// Section objects are 'live', so to check that they changed, we | ||
// need to map their tagNames now (and compare to mapped tagNames later). | ||
// In addition, to catch changes from ul -> ol, we keep track of the | ||
// un-nested tag names (otherwise we'd only see li -> li change) | ||
state.activeSectionTagNames = state.activeSections.map(function (s) { | ||
return s.isNested ? s.parent.tagName : s.tagName; | ||
}); | ||
return state; | ||
} | ||
}, { | ||
key: '_readActiveSections', | ||
value: function _readActiveSections(range) { | ||
var head = range.head; | ||
var tail = range.tail; | ||
var post = this.editor.post; | ||
|
||
if (range.isBlank) { | ||
return []; | ||
} else { | ||
return post.sections.readRange(head.section, tail.section); | ||
} | ||
} | ||
}, { | ||
key: '_readActiveMarkups', | ||
value: function _readActiveMarkups(range) { | ||
var post = this.editor.post; | ||
|
||
return post.markupsInRange(range); | ||
} | ||
}, { | ||
key: '_removeActiveMarkup', | ||
value: function _removeActiveMarkup(markup) { | ||
var index = this.state.activeMarkups.indexOf(markup); | ||
this.state.activeMarkups.splice(index, 1); | ||
} | ||
}, { | ||
key: '_addActiveMarkup', | ||
value: function _addActiveMarkup(markup) { | ||
this.state.activeMarkups.push(markup); | ||
} | ||
}, { | ||
key: 'range', | ||
get: function get() { | ||
return this.state.range; | ||
} | ||
|
||
/** | ||
* @return {Section[]} | ||
*/ | ||
}, { | ||
key: 'activeSections', | ||
get: function get() { | ||
return this.state.activeSections; | ||
} | ||
|
||
/** | ||
* @return {Markup[]} | ||
*/ | ||
}, { | ||
key: 'activeMarkups', | ||
get: function get() { | ||
return this.state.activeMarkups; | ||
} | ||
}]); | ||
|
||
return EditState; | ||
})(); | ||
|
||
exports['default'] = EditState; |
Oops, something went wrong.