2014-03-16 02:13:42 +00:00
|
|
|
/* jshint -W084, -W099 */
|
2014-04-06 00:59:32 +00:00
|
|
|
// Credit to http://dabblet.com/
|
2014-03-16 02:13:42 +00:00
|
|
|
define([
|
|
|
|
'jquery',
|
2014-03-17 02:01:46 +00:00
|
|
|
'underscore',
|
2014-04-08 23:20:48 +00:00
|
|
|
'utils',
|
2014-03-18 01:10:22 +00:00
|
|
|
'settings',
|
2014-03-16 02:13:42 +00:00
|
|
|
'eventMgr',
|
|
|
|
'prism-core',
|
2014-03-25 00:23:42 +00:00
|
|
|
'diff_match_patch_uncompressed',
|
2014-03-27 00:20:08 +00:00
|
|
|
'jsondiffpatch',
|
2014-03-17 02:01:46 +00:00
|
|
|
'crel',
|
2014-04-06 00:59:32 +00:00
|
|
|
'rangy',
|
2014-03-21 00:36:28 +00:00
|
|
|
'MutationObservers',
|
2014-03-16 02:13:42 +00:00
|
|
|
'libs/prism-markdown'
|
2014-04-08 23:20:48 +00:00
|
|
|
], function ($, _, utils, settings, eventMgr, Prism, diff_match_patch, jsondiffpatch, crel, rangy) {
|
2014-03-17 02:01:46 +00:00
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
var editor = {};
|
|
|
|
var scrollTop = 0;
|
|
|
|
var inputElt;
|
2014-03-19 22:10:59 +00:00
|
|
|
var $inputElt;
|
2014-03-31 00:10:28 +00:00
|
|
|
var contentElt;
|
|
|
|
var $contentElt;
|
|
|
|
var marginElt;
|
|
|
|
var $marginElt;
|
2014-03-18 01:10:22 +00:00
|
|
|
var previewElt;
|
|
|
|
var pagedownEditor;
|
|
|
|
var refreshPreviewLater = (function() {
|
|
|
|
var elapsedTime = 0;
|
|
|
|
var refreshPreview = function() {
|
|
|
|
var startTime = Date.now();
|
|
|
|
pagedownEditor.refreshPreview();
|
|
|
|
elapsedTime = Date.now() - startTime;
|
|
|
|
};
|
|
|
|
if(settings.lazyRendering === true) {
|
|
|
|
return _.debounce(refreshPreview, 500);
|
|
|
|
}
|
|
|
|
return function() {
|
|
|
|
setTimeout(refreshPreview, elapsedTime < 2000 ? elapsedTime : 2000);
|
|
|
|
};
|
|
|
|
})();
|
2014-04-15 23:16:08 +00:00
|
|
|
eventMgr.addListener('onPagedownConfigure', function(pagedownEditorParam) {
|
|
|
|
pagedownEditor = pagedownEditorParam;
|
2014-03-16 02:13:42 +00:00
|
|
|
});
|
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
eventMgr.addListener('onSectionsCreated', function(newSectionList) {
|
|
|
|
updateSectionList(newSectionList);
|
|
|
|
highlightSections();
|
2014-03-18 01:10:22 +00:00
|
|
|
if(fileChanged === true) {
|
|
|
|
// Refresh preview synchronously
|
|
|
|
pagedownEditor.refreshPreview();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
refreshPreviewLater();
|
|
|
|
}
|
2014-03-17 02:01:46 +00:00
|
|
|
});
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
var fileChanged = true;
|
|
|
|
var fileDesc;
|
|
|
|
eventMgr.addListener('onFileSelected', function(selectedFileDesc) {
|
2014-03-17 02:01:46 +00:00
|
|
|
fileChanged = true;
|
2014-03-18 01:10:22 +00:00
|
|
|
fileDesc = selectedFileDesc;
|
2014-03-17 02:01:46 +00:00
|
|
|
});
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-03-30 01:44:51 +00:00
|
|
|
// Watcher used to detect editor changes
|
|
|
|
function Watcher() {
|
|
|
|
this.isWatching = false;
|
|
|
|
var contentObserver;
|
|
|
|
this.startWatching = function() {
|
|
|
|
this.isWatching = true;
|
|
|
|
contentObserver = contentObserver || new MutationObserver(checkContentChange);
|
2014-03-31 00:10:28 +00:00
|
|
|
contentObserver.observe(contentElt, {
|
2014-03-29 01:22:24 +00:00
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
characterData: true
|
|
|
|
});
|
2014-03-30 01:44:51 +00:00
|
|
|
};
|
|
|
|
this.stopWatching = function() {
|
|
|
|
contentObserver.disconnect();
|
|
|
|
this.isWatching = false;
|
|
|
|
};
|
|
|
|
this.noWatch = function(cb) {
|
|
|
|
if(this.isWatching === true) {
|
|
|
|
this.stopWatching();
|
|
|
|
cb();
|
|
|
|
this.startWatching();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var watcher = new Watcher();
|
|
|
|
editor.watcher = watcher;
|
|
|
|
|
2014-04-05 00:54:06 +00:00
|
|
|
var diffMatchPatch = new diff_match_patch();
|
|
|
|
var jsonDiffPatch = jsondiffpatch.create({
|
|
|
|
objectHash: function(obj) {
|
|
|
|
return JSON.stringify(obj);
|
|
|
|
},
|
|
|
|
arrays: {
|
|
|
|
detectMove: false,
|
|
|
|
},
|
|
|
|
textDiff: {
|
|
|
|
minLength: 9999999
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function SelectionMgr() {
|
2014-04-06 00:59:32 +00:00
|
|
|
var self = this;
|
2014-04-05 00:54:06 +00:00
|
|
|
this.selectionStart = 0;
|
|
|
|
this.selectionEnd = 0;
|
|
|
|
this.cursorY = 0;
|
|
|
|
this.findOffset = function(offset) {
|
|
|
|
var walker = document.createTreeWalker(contentElt, 4);
|
2014-04-16 23:29:51 +00:00
|
|
|
var text = '';
|
2014-04-05 00:54:06 +00:00
|
|
|
while(walker.nextNode()) {
|
2014-04-16 23:29:51 +00:00
|
|
|
text = walker.currentNode.nodeValue || '';
|
2014-04-05 00:54:06 +00:00
|
|
|
if (text.length > offset) {
|
|
|
|
return {
|
|
|
|
container: walker.currentNode,
|
|
|
|
offset: offset
|
|
|
|
};
|
|
|
|
}
|
|
|
|
offset -= text.length;
|
|
|
|
}
|
|
|
|
return {
|
2014-04-16 23:29:51 +00:00
|
|
|
container: walker.currentNode,
|
|
|
|
offset: text.length
|
2014-04-05 00:54:06 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
this.createRange = function(start, end) {
|
2014-04-16 23:29:51 +00:00
|
|
|
start = start < 0 ? 0 : start;
|
|
|
|
end = end < 0 ? 0 : end;
|
2014-04-05 00:54:06 +00:00
|
|
|
var range = document.createRange();
|
|
|
|
var offset = _.isObject(start) ? start : this.findOffset(start);
|
|
|
|
range.setStart(offset.container, offset.offset);
|
|
|
|
if (end && end != start) {
|
|
|
|
offset = _.isObject(end) ? end : this.findOffset(end);
|
|
|
|
}
|
|
|
|
range.setEnd(offset.container, offset.offset);
|
|
|
|
return range;
|
|
|
|
};
|
|
|
|
this.setSelectionStartEnd = function(start, end, range, skipSelectionUpdate) {
|
|
|
|
if(start === undefined) {
|
|
|
|
start = this.selectionStart;
|
|
|
|
}
|
|
|
|
if(end === undefined) {
|
|
|
|
end = this.selectionEnd;
|
|
|
|
}
|
2014-04-06 23:39:24 +00:00
|
|
|
var textContent = inputElt.textContent;
|
|
|
|
if(start === textContent.length && textContent[textContent.length -1 ] == '\n') {
|
|
|
|
start--;
|
2014-04-06 00:59:32 +00:00
|
|
|
range = undefined;
|
|
|
|
skipSelectionUpdate = false;
|
|
|
|
}
|
2014-04-06 23:39:24 +00:00
|
|
|
if(end === textContent.length && textContent[textContent.length - 1] == '\n') {
|
|
|
|
end--;
|
2014-04-06 00:59:32 +00:00
|
|
|
range = undefined;
|
|
|
|
skipSelectionUpdate = false;
|
|
|
|
}
|
|
|
|
this.selectionStart = start;
|
|
|
|
this.selectionEnd = end;
|
2014-04-05 00:54:06 +00:00
|
|
|
var min = Math.min(start, end);
|
|
|
|
var max = Math.max(start, end);
|
|
|
|
range = range || this.createRange(min, max);
|
|
|
|
if(!skipSelectionUpdate) {
|
2014-04-06 00:59:32 +00:00
|
|
|
var selection = rangy.getSelection();
|
2014-04-05 00:54:06 +00:00
|
|
|
selection.removeAllRanges();
|
2014-04-06 00:59:32 +00:00
|
|
|
selection.addRange(range, start > end);
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
|
|
|
fileDesc.editorStart = this.selectionStart;
|
|
|
|
fileDesc.editorEnd = this.selectionEnd;
|
|
|
|
// Update cursor coordinates
|
|
|
|
$inputElt.toggleClass('has-selection', this.selectionStart !== this.selectionEnd);
|
|
|
|
var coordinates = this.getCoordinates(this.selectionEnd, this.selectionEndContainer, this.selectionEndOffset);
|
|
|
|
if(this.cursorY !== coordinates.y) {
|
|
|
|
this.cursorY = coordinates.y;
|
|
|
|
eventMgr.onCursorCoordinates(coordinates.x, coordinates.y);
|
|
|
|
}
|
|
|
|
return range;
|
|
|
|
};
|
2014-04-06 00:59:32 +00:00
|
|
|
this.saveSelectionState = (function() {
|
|
|
|
function save() {
|
|
|
|
if(fileChanged === false) {
|
2014-04-06 23:39:24 +00:00
|
|
|
var selectionStart = self.selectionStart;
|
|
|
|
var selectionEnd = self.selectionEnd;
|
|
|
|
var range;
|
2014-04-06 00:59:32 +00:00
|
|
|
var selection = rangy.getSelection();
|
|
|
|
if(selection.rangeCount > 0) {
|
2014-04-16 23:29:51 +00:00
|
|
|
var selectionRange = selection.getRangeAt(0);
|
|
|
|
var element = selectionRange.startContainer;
|
2014-04-06 00:59:32 +00:00
|
|
|
if ((contentElt.compareDocumentPosition(element) & 0x10)) {
|
2014-04-16 23:29:51 +00:00
|
|
|
range = selectionRange;
|
2014-04-06 00:59:32 +00:00
|
|
|
var container = element;
|
|
|
|
var offset = range.startOffset;
|
|
|
|
do {
|
|
|
|
while (element = element.previousSibling) {
|
|
|
|
if (element.textContent) {
|
|
|
|
offset += element.textContent.length;
|
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
2014-04-06 00:59:32 +00:00
|
|
|
element = container = container.parentNode;
|
|
|
|
} while (element && element != inputElt);
|
2014-04-05 00:54:06 +00:00
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
if(selection.isBackwards()) {
|
2014-04-06 23:39:24 +00:00
|
|
|
selectionStart = offset + (range + '').length;
|
|
|
|
selectionEnd = offset;
|
2014-04-06 00:59:32 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-04-06 23:39:24 +00:00
|
|
|
selectionStart = offset;
|
|
|
|
selectionEnd = offset + (range + '').length;
|
2014-04-06 00:59:32 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-06 23:39:24 +00:00
|
|
|
self.setSelectionStartEnd(selectionStart, selectionEnd, range, true);
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
2014-04-06 00:59:32 +00:00
|
|
|
undoMgr.saveSelectionState();
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
2014-04-08 23:20:48 +00:00
|
|
|
var debouncedSave = utils.debounce(save);
|
2014-04-06 00:59:32 +00:00
|
|
|
return function(debounced) {
|
|
|
|
debounced ? debouncedSave() : save();
|
|
|
|
};
|
|
|
|
})();
|
2014-04-05 00:54:06 +00:00
|
|
|
this.getCoordinates = function(inputOffset, container, offset) {
|
|
|
|
if(!container) {
|
|
|
|
offset = this.findOffset(inputOffset);
|
|
|
|
container = offset.container;
|
|
|
|
offset = offset.offset;
|
|
|
|
}
|
|
|
|
var x = 0;
|
|
|
|
var y = 0;
|
|
|
|
if(container.textContent == '\n') {
|
|
|
|
y = container.parentNode.offsetTop + container.parentNode.offsetHeight / 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var selectedChar = textContent[inputOffset];
|
|
|
|
var startOffset = {
|
|
|
|
container: container,
|
|
|
|
offset: offset
|
|
|
|
};
|
|
|
|
var endOffset = {
|
|
|
|
container: container,
|
|
|
|
offset: offset
|
|
|
|
};
|
2014-04-06 00:59:32 +00:00
|
|
|
if(inputOffset > 0 && (selectedChar === undefined || selectedChar == '\n')) {
|
2014-04-05 00:54:06 +00:00
|
|
|
if(startOffset.offset === 0) {
|
|
|
|
startOffset = inputOffset - 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
startOffset.offset -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(endOffset.offset === container.textContent.length) {
|
|
|
|
endOffset = inputOffset + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
endOffset.offset += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var selectionRange = this.createRange(startOffset, endOffset);
|
|
|
|
var selectionRect = selectionRange.getBoundingClientRect();
|
2014-04-14 18:00:22 +00:00
|
|
|
y = selectionRect.top + selectionRect.height / 2 - inputElt.getBoundingClientRect().top + inputElt.scrollTop;
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionRange.detach();
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
x: x,
|
|
|
|
y: y
|
|
|
|
};
|
|
|
|
};
|
2014-04-06 23:39:24 +00:00
|
|
|
this.getClosestWordOffset = function(offset) {
|
|
|
|
var offsetStart = 0;
|
|
|
|
var offsetEnd = 0;
|
|
|
|
var nextOffset = 0;
|
|
|
|
textContent.split(/\s/).some(function(word) {
|
|
|
|
if(word) {
|
|
|
|
offsetStart = nextOffset;
|
|
|
|
offsetEnd = nextOffset + word.length;
|
|
|
|
if(offsetEnd > offset) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextOffset += word.length + 1;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
start: offsetStart,
|
|
|
|
end: offsetEnd
|
|
|
|
};
|
|
|
|
};
|
2014-04-05 00:54:06 +00:00
|
|
|
}
|
|
|
|
var selectionMgr = new SelectionMgr();
|
|
|
|
editor.selectionMgr = selectionMgr;
|
2014-04-16 23:29:51 +00:00
|
|
|
$(document).on('selectionchange', '.editor-content', _.bind(selectionMgr.saveSelectionState, selectionMgr, true));
|
2014-04-05 00:54:06 +00:00
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
var adjustCursorPosition = (function() {
|
2014-04-08 23:20:48 +00:00
|
|
|
var adjust = utils.debounce(function() {
|
2014-04-06 00:59:32 +00:00
|
|
|
var adjust = inputElt.offsetHeight / 2;
|
|
|
|
if(adjust > 130) {
|
|
|
|
adjust = 130;
|
|
|
|
}
|
|
|
|
var cursorMinY = inputElt.scrollTop + adjust;
|
|
|
|
var cursorMaxY = inputElt.scrollTop + inputElt.offsetHeight - adjust;
|
|
|
|
if(selectionMgr.cursorY < cursorMinY) {
|
|
|
|
inputElt.scrollTop += selectionMgr.cursorY - cursorMinY;
|
|
|
|
}
|
|
|
|
else if(selectionMgr.cursorY > cursorMaxY) {
|
|
|
|
inputElt.scrollTop += selectionMgr.cursorY - cursorMaxY;
|
|
|
|
}
|
2014-04-08 23:20:48 +00:00
|
|
|
});
|
2014-04-06 00:59:32 +00:00
|
|
|
return function() {
|
|
|
|
if(inputElt === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectionMgr.saveSelectionState(true);
|
|
|
|
adjust();
|
|
|
|
};
|
|
|
|
})();
|
2014-04-05 00:54:06 +00:00
|
|
|
eventMgr.addListener('onLayoutResize', adjustCursorPosition);
|
|
|
|
|
|
|
|
var textContent;
|
2014-03-30 01:44:51 +00:00
|
|
|
function setValue(value) {
|
2014-04-05 00:54:06 +00:00
|
|
|
var startOffset = diffMatchPatch.diff_commonPrefix(textContent, value);
|
2014-04-06 00:59:32 +00:00
|
|
|
if(startOffset === textContent.length) {
|
|
|
|
startOffset--;
|
|
|
|
}
|
2014-03-30 01:44:51 +00:00
|
|
|
var endOffset = Math.min(
|
2014-04-05 00:54:06 +00:00
|
|
|
diffMatchPatch.diff_commonSuffix(textContent, value),
|
|
|
|
textContent.length - startOffset,
|
2014-03-30 01:44:51 +00:00
|
|
|
value.length - startOffset
|
|
|
|
);
|
|
|
|
var replacement = value.substring(startOffset, value.length - endOffset);
|
2014-04-05 00:54:06 +00:00
|
|
|
var range = selectionMgr.createRange(startOffset, textContent.length - endOffset);
|
2014-03-30 01:44:51 +00:00
|
|
|
range.deleteContents();
|
|
|
|
range.insertNode(document.createTextNode(replacement));
|
|
|
|
}
|
2014-04-07 23:19:47 +00:00
|
|
|
editor.setValue = setValue;
|
2014-03-30 01:44:51 +00:00
|
|
|
|
2014-04-16 23:29:51 +00:00
|
|
|
function replacePreviousText(text, replacement) {
|
|
|
|
var offset = selectionMgr.selectionStart;
|
|
|
|
if(offset !== selectionMgr.selectionEnd) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var range = selectionMgr.createRange(offset - text.length, offset);
|
|
|
|
if('' + range != text) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
range.deleteContents();
|
|
|
|
range.insertNode(document.createTextNode(replacement));
|
|
|
|
offset = offset - text.length + replacement.length;
|
|
|
|
selectionMgr.setSelectionStartEnd(offset, offset);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
editor.replacePreviousText = replacePreviousText;
|
|
|
|
|
2014-03-30 01:44:51 +00:00
|
|
|
function setValueNoWatch(value) {
|
|
|
|
setValue(value);
|
2014-04-05 00:54:06 +00:00
|
|
|
textContent = value;
|
2014-03-30 01:44:51 +00:00
|
|
|
}
|
|
|
|
editor.setValueNoWatch = setValueNoWatch;
|
|
|
|
|
2014-04-05 00:54:06 +00:00
|
|
|
function getValue() {
|
|
|
|
return textContent;
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
2014-04-06 00:59:32 +00:00
|
|
|
editor.getValue = getValue;
|
2014-03-27 00:20:08 +00:00
|
|
|
|
2014-04-06 23:39:24 +00:00
|
|
|
function focus() {
|
|
|
|
$contentElt.focus();
|
|
|
|
selectionMgr.setSelectionStartEnd();
|
|
|
|
inputElt.scrollTop = scrollTop;
|
|
|
|
}
|
|
|
|
editor.focus = focus;
|
|
|
|
|
2014-04-05 00:54:06 +00:00
|
|
|
function UndoMgr() {
|
2014-03-26 00:29:34 +00:00
|
|
|
var undoStack = [];
|
|
|
|
var redoStack = [];
|
2014-03-25 00:23:42 +00:00
|
|
|
var lastTime;
|
2014-03-26 00:29:34 +00:00
|
|
|
var lastMode;
|
|
|
|
var currentState;
|
|
|
|
var selectionStartBefore;
|
|
|
|
var selectionEndBefore;
|
2014-03-30 01:44:51 +00:00
|
|
|
this.setCommandMode = function() {
|
|
|
|
this.currentMode = 'command';
|
2014-03-26 00:29:34 +00:00
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
this.setMode = function() {}; // For compatibility with PageDown
|
|
|
|
this.onButtonStateChange = function() {}; // To be overridden by PageDown
|
2014-04-08 23:20:48 +00:00
|
|
|
this.saveState = utils.debounce(function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
redoStack = [];
|
2014-03-25 00:23:42 +00:00
|
|
|
var currentTime = Date.now();
|
2014-03-30 01:44:51 +00:00
|
|
|
if(this.currentMode == 'comment' || (this.currentMode != lastMode && lastMode != 'newlines') || currentTime - lastTime > 1000) {
|
2014-03-26 00:29:34 +00:00
|
|
|
undoStack.push(currentState);
|
|
|
|
// Limit the size of the stack
|
|
|
|
if(undoStack.length === 100) {
|
|
|
|
undoStack.shift();
|
|
|
|
}
|
2014-03-25 00:23:42 +00:00
|
|
|
}
|
2014-03-26 00:29:34 +00:00
|
|
|
else {
|
|
|
|
selectionStartBefore = currentState.selectionStartBefore;
|
|
|
|
selectionEndBefore = currentState.selectionEndBefore;
|
2014-03-25 00:23:42 +00:00
|
|
|
}
|
2014-03-26 00:29:34 +00:00
|
|
|
currentState = {
|
|
|
|
selectionStartBefore: selectionStartBefore,
|
|
|
|
selectionEndBefore: selectionEndBefore,
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionStartAfter: selectionMgr.selectionStart,
|
|
|
|
selectionEndAfter: selectionMgr.selectionEnd,
|
|
|
|
content: textContent,
|
2014-03-27 00:20:08 +00:00
|
|
|
discussionListJSON: fileDesc.discussionListJSON
|
2014-03-26 00:29:34 +00:00
|
|
|
};
|
|
|
|
lastTime = currentTime;
|
2014-03-30 01:44:51 +00:00
|
|
|
lastMode = this.currentMode;
|
|
|
|
this.currentMode = undefined;
|
|
|
|
this.onButtonStateChange();
|
2014-04-08 23:20:48 +00:00
|
|
|
}, this);
|
2014-03-30 01:44:51 +00:00
|
|
|
this.saveSelectionState = _.debounce(function() {
|
|
|
|
if(this.currentMode === undefined) {
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionStartBefore = selectionMgr.selectionStart;
|
|
|
|
selectionEndBefore = selectionMgr.selectionEnd;
|
2014-03-25 00:23:42 +00:00
|
|
|
}
|
2014-03-26 00:29:34 +00:00
|
|
|
}, 10);
|
2014-03-30 01:44:51 +00:00
|
|
|
this.canUndo = function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
return undoStack.length;
|
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
this.canRedo = function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
return redoStack.length;
|
|
|
|
};
|
|
|
|
function restoreState(state, selectionStart, selectionEnd) {
|
2014-03-27 00:20:08 +00:00
|
|
|
// Update editor
|
2014-03-30 01:44:51 +00:00
|
|
|
watcher.noWatch(function() {
|
2014-04-05 00:54:06 +00:00
|
|
|
if(textContent != state.content) {
|
2014-03-30 01:44:51 +00:00
|
|
|
setValueNoWatch(state.content);
|
|
|
|
fileDesc.content = state.content;
|
|
|
|
eventMgr.onContentChanged(fileDesc, state.content);
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd(selectionStart, selectionEnd);
|
2014-03-27 00:20:08 +00:00
|
|
|
var discussionListJSON = fileDesc.discussionListJSON;
|
|
|
|
if(discussionListJSON != state.discussionListJSON) {
|
|
|
|
var oldDiscussionList = fileDesc.discussionList;
|
|
|
|
fileDesc.discussionListJSON = state.discussionListJSON;
|
|
|
|
var newDiscussionList = fileDesc.discussionList;
|
|
|
|
var diff = jsonDiffPatch.diff(oldDiscussionList, newDiscussionList);
|
|
|
|
var commentsChanged = false;
|
|
|
|
_.each(diff, function(discussionDiff, discussionIndex) {
|
|
|
|
if(!_.isArray(discussionDiff)) {
|
|
|
|
commentsChanged = true;
|
|
|
|
}
|
|
|
|
else if(discussionDiff.length === 1) {
|
|
|
|
eventMgr.onDiscussionCreated(fileDesc, newDiscussionList[discussionIndex]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eventMgr.onDiscussionRemoved(fileDesc, oldDiscussionList[discussionIndex]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
commentsChanged && eventMgr.onCommentsChanged(fileDesc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-26 00:29:34 +00:00
|
|
|
selectionStartBefore = selectionStart;
|
|
|
|
selectionEndBefore = selectionEnd;
|
|
|
|
currentState = state;
|
2014-04-08 23:20:48 +00:00
|
|
|
this.currentMode = undefined;
|
2014-03-26 00:29:34 +00:00
|
|
|
lastMode = undefined;
|
2014-04-08 23:20:48 +00:00
|
|
|
this.onButtonStateChange();
|
2014-03-26 00:29:34 +00:00
|
|
|
adjustCursorPosition();
|
2014-03-25 00:23:42 +00:00
|
|
|
}
|
2014-03-30 01:44:51 +00:00
|
|
|
this.undo = function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
var state = undoStack.pop();
|
|
|
|
if(!state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
redoStack.push(currentState);
|
2014-04-08 23:20:48 +00:00
|
|
|
restoreState.call(this, state, currentState.selectionStartBefore, currentState.selectionEndBefore);
|
2014-03-25 00:23:42 +00:00
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
this.redo = function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
var state = redoStack.pop();
|
|
|
|
if(!state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
undoStack.push(currentState);
|
2014-04-08 23:20:48 +00:00
|
|
|
restoreState.call(this, state, state.selectionStartAfter, state.selectionEndAfter);
|
2014-03-26 00:29:34 +00:00
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
this.init = function() {
|
2014-03-26 00:29:34 +00:00
|
|
|
var content = fileDesc.content;
|
|
|
|
undoStack = [];
|
|
|
|
redoStack = [];
|
|
|
|
lastTime = 0;
|
|
|
|
currentState = {
|
|
|
|
selectionStartAfter: fileDesc.selectionStart,
|
|
|
|
selectionEndAfter: fileDesc.selectionEnd,
|
|
|
|
content: content,
|
2014-03-27 00:20:08 +00:00
|
|
|
discussionListJSON: fileDesc.discussionListJSON
|
2014-03-26 00:29:34 +00:00
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
this.currentMode = undefined;
|
2014-03-26 00:29:34 +00:00
|
|
|
lastMode = undefined;
|
2014-03-31 00:10:28 +00:00
|
|
|
contentElt.textContent = content;
|
2014-03-26 00:29:34 +00:00
|
|
|
};
|
2014-03-30 01:44:51 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
var undoMgr = new UndoMgr();
|
|
|
|
editor.undoMgr = undoMgr;
|
2014-03-25 00:23:42 +00:00
|
|
|
|
2014-03-27 00:20:08 +00:00
|
|
|
function onComment() {
|
2014-03-30 01:44:51 +00:00
|
|
|
if(watcher.isWatching === true) {
|
2014-04-08 23:20:48 +00:00
|
|
|
undoMgr.currentMode = undoMgr.currentMode || 'comment';
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.saveState();
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
eventMgr.addListener('onDiscussionCreated', onComment);
|
|
|
|
eventMgr.addListener('onDiscussionRemoved', onComment);
|
|
|
|
eventMgr.addListener('onCommentsChanged', onComment);
|
|
|
|
|
2014-03-21 00:36:28 +00:00
|
|
|
function checkContentChange() {
|
2014-04-08 23:20:48 +00:00
|
|
|
var newTextContent = inputElt.textContent;
|
2014-03-21 00:36:28 +00:00
|
|
|
if(fileChanged === false) {
|
2014-04-08 23:20:48 +00:00
|
|
|
if(newTextContent == textContent) {
|
2014-03-18 01:10:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-08 23:20:48 +00:00
|
|
|
if(!/\n$/.test(newTextContent)) {
|
|
|
|
newTextContent += '\n';
|
2014-03-19 00:33:57 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.currentMode = undoMgr.currentMode || 'typing';
|
2014-03-27 00:20:08 +00:00
|
|
|
var discussionList = _.values(fileDesc.discussionList);
|
|
|
|
fileDesc.newDiscussion && discussionList.push(fileDesc.newDiscussion);
|
2014-04-08 23:20:48 +00:00
|
|
|
var updateDiscussionList = adjustCommentOffsets(textContent, newTextContent, discussionList);
|
|
|
|
textContent = newTextContent;
|
2014-03-27 00:20:08 +00:00
|
|
|
if(updateDiscussionList === true) {
|
|
|
|
fileDesc.discussionList = fileDesc.discussionList; // Write discussionList in localStorage
|
2014-03-24 00:22:46 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
fileDesc.content = textContent;
|
|
|
|
selectionMgr.saveSelectionState();
|
|
|
|
eventMgr.onContentChanged(fileDesc, textContent);
|
2014-03-27 00:20:08 +00:00
|
|
|
updateDiscussionList && eventMgr.onCommentsChanged(fileDesc);
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.saveState();
|
2014-03-18 01:10:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-04-08 23:20:48 +00:00
|
|
|
textContent = newTextContent;
|
|
|
|
if(!/\n$/.test(textContent)) {
|
|
|
|
textContent += '\n';
|
|
|
|
fileDesc.content = textContent;
|
2014-03-19 00:33:57 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd(fileDesc.editorStart, fileDesc.editorEnd);
|
|
|
|
eventMgr.onFileOpen(fileDesc, textContent);
|
2014-03-26 00:29:34 +00:00
|
|
|
previewElt.scrollTop = fileDesc.previewScrollTop;
|
2014-03-18 01:10:22 +00:00
|
|
|
scrollTop = fileDesc.editorScrollTop;
|
|
|
|
inputElt.scrollTop = scrollTop;
|
|
|
|
fileChanged = false;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
function adjustCommentOffsets(oldTextContent, newTextContent, discussionList) {
|
|
|
|
if(!discussionList.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var changes = diffMatchPatch.diff_main(oldTextContent, newTextContent);
|
|
|
|
var changed = false;
|
|
|
|
var startOffset = 0;
|
|
|
|
changes.forEach(function(change) {
|
|
|
|
var changeType = change[0];
|
|
|
|
var changeText = change[1];
|
|
|
|
if(changeType === 0) {
|
|
|
|
startOffset += changeText.length;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var endOffset = startOffset;
|
|
|
|
var diffOffset = changeText.length;
|
|
|
|
if(changeType === -1) {
|
|
|
|
endOffset += diffOffset;
|
|
|
|
diffOffset = -diffOffset;
|
|
|
|
}
|
|
|
|
discussionList.forEach(function(discussion) {
|
|
|
|
// selectionEnd
|
|
|
|
if(discussion.selectionEnd > endOffset) {
|
|
|
|
discussion.selectionEnd += diffOffset;
|
|
|
|
discussion.discussionIndex && (changed = true);
|
|
|
|
}
|
|
|
|
else if(discussion.selectionEnd > startOffset) {
|
|
|
|
discussion.selectionEnd = startOffset;
|
|
|
|
discussion.discussionIndex && (changed = true);
|
|
|
|
}
|
|
|
|
// selectionStart
|
|
|
|
if(discussion.selectionStart >= endOffset) {
|
|
|
|
discussion.selectionStart += diffOffset;
|
|
|
|
discussion.discussionIndex && (changed = true);
|
|
|
|
}
|
|
|
|
else if(discussion.selectionStart > startOffset) {
|
|
|
|
discussion.selectionStart = startOffset;
|
|
|
|
discussion.discussionIndex && (changed = true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(changeType === 1) {
|
|
|
|
startOffset += changeText.length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
editor.adjustCommentOffsets = adjustCommentOffsets;
|
|
|
|
|
2014-04-15 23:16:08 +00:00
|
|
|
editor.init = function() {
|
|
|
|
inputElt = document.getElementById('wmd-input');
|
2014-03-19 22:10:59 +00:00
|
|
|
$inputElt = $(inputElt);
|
2014-04-14 00:21:06 +00:00
|
|
|
contentElt = inputElt.querySelector('.editor-content');
|
2014-03-31 00:10:28 +00:00
|
|
|
$contentElt = $(contentElt);
|
2014-04-14 00:21:06 +00:00
|
|
|
marginElt = inputElt.querySelector('.editor-margin');
|
2014-03-31 00:10:28 +00:00
|
|
|
$marginElt = $(marginElt);
|
2014-04-15 23:16:08 +00:00
|
|
|
previewElt = document.querySelector('.preview-container');
|
|
|
|
|
|
|
|
$inputElt.addClass(settings.editorFontClass);
|
2014-03-21 00:36:28 +00:00
|
|
|
|
2014-03-30 01:44:51 +00:00
|
|
|
watcher.startWatching();
|
2014-03-21 00:36:28 +00:00
|
|
|
|
2014-03-24 00:22:46 +00:00
|
|
|
$(inputElt).scroll(function() {
|
|
|
|
scrollTop = inputElt.scrollTop;
|
|
|
|
if(fileChanged === false) {
|
|
|
|
fileDesc.editorScrollTop = scrollTop;
|
|
|
|
}
|
|
|
|
});
|
2014-03-18 01:10:22 +00:00
|
|
|
$(previewElt).scroll(function() {
|
|
|
|
if(fileChanged === false) {
|
|
|
|
fileDesc.previewScrollTop = previewElt.scrollTop;
|
|
|
|
}
|
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-04-06 23:39:24 +00:00
|
|
|
inputElt.focus = focus;
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
Object.defineProperty(inputElt, 'value', {
|
2014-03-16 02:13:42 +00:00
|
|
|
get: function () {
|
2014-04-05 00:54:06 +00:00
|
|
|
return textContent;
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
2014-03-30 01:44:51 +00:00
|
|
|
set: setValue
|
2014-03-16 02:13:42 +00:00
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
Object.defineProperty(inputElt, 'selectionStart', {
|
2014-03-16 02:13:42 +00:00
|
|
|
get: function () {
|
2014-04-06 00:59:32 +00:00
|
|
|
return Math.min(selectionMgr.selectionStart, selectionMgr.selectionEnd);
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd(value);
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
2014-03-18 01:10:22 +00:00
|
|
|
Object.defineProperty(inputElt, 'selectionEnd', {
|
2014-03-16 02:13:42 +00:00
|
|
|
get: function () {
|
2014-04-06 00:59:32 +00:00
|
|
|
return Math.max(selectionMgr.selectionStart, selectionMgr.selectionEnd);
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd(undefined, value);
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
2014-03-19 22:10:59 +00:00
|
|
|
var clearNewline = false;
|
2014-03-31 00:10:28 +00:00
|
|
|
$contentElt.on('keydown', function (evt) {
|
2014-03-21 00:36:28 +00:00
|
|
|
if(
|
|
|
|
evt.which === 17 || // Ctrl
|
|
|
|
evt.which === 91 || // Cmd
|
|
|
|
evt.which === 18 || // Alt
|
|
|
|
evt.which === 16 // Shift
|
|
|
|
) {
|
|
|
|
return;
|
2014-03-19 00:33:57 +00:00
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.saveSelectionState();
|
2014-03-21 00:36:28 +00:00
|
|
|
|
|
|
|
var cmdOrCtrl = evt.metaKey || evt.ctrlKey;
|
2014-03-30 01:44:51 +00:00
|
|
|
if(!cmdOrCtrl) {
|
|
|
|
adjustCursorPosition();
|
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-21 00:36:28 +00:00
|
|
|
switch (evt.which) {
|
2014-03-18 01:10:22 +00:00
|
|
|
case 9: // Tab
|
|
|
|
if (!cmdOrCtrl) {
|
|
|
|
action('indent', {
|
|
|
|
inverse: evt.shiftKey
|
|
|
|
});
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
action('newline');
|
|
|
|
evt.preventDefault();
|
|
|
|
break;
|
|
|
|
}
|
2014-03-21 00:36:28 +00:00
|
|
|
if(evt.which !== 13) {
|
2014-03-19 22:10:59 +00:00
|
|
|
clearNewline = false;
|
|
|
|
}
|
2014-03-21 00:36:28 +00:00
|
|
|
})
|
2014-04-08 23:20:48 +00:00
|
|
|
.on('mouseup', _.bind(selectionMgr.saveSelectionState, selectionMgr, true))
|
2014-03-21 00:36:28 +00:00
|
|
|
.on('paste', function () {
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.currentMode = 'paste';
|
2014-03-21 00:36:28 +00:00
|
|
|
adjustCursorPosition();
|
|
|
|
})
|
|
|
|
.on('cut', function () {
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.currentMode = 'cut';
|
2014-03-21 00:36:28 +00:00
|
|
|
adjustCursorPosition();
|
2014-03-18 01:10:22 +00:00
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-16 02:13:42 +00:00
|
|
|
var action = function (action, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
var textContent = getValue();
|
|
|
|
var selectionStart = options.start || selectionMgr.selectionStart;
|
|
|
|
var selectionEnd = options.end || selectionMgr.selectionEnd;
|
|
|
|
var state = {
|
|
|
|
selectionStart: selectionStart,
|
|
|
|
selectionEnd: selectionEnd,
|
|
|
|
before: textContent.slice(0, selectionStart),
|
|
|
|
after: textContent.slice(selectionEnd),
|
|
|
|
selection: textContent.slice(selectionStart, selectionEnd)
|
|
|
|
};
|
2014-03-16 02:13:42 +00:00
|
|
|
|
|
|
|
actions[action](state, options);
|
2014-04-06 00:59:32 +00:00
|
|
|
setValue(state.before + state.selection + state.after);
|
|
|
|
selectionMgr.setSelectionStartEnd(state.selectionStart, state.selectionEnd);
|
2014-03-19 22:10:59 +00:00
|
|
|
$inputElt.trigger('input');
|
2014-03-16 02:13:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var actions = {
|
|
|
|
indent: function (state, options) {
|
2014-04-06 00:59:32 +00:00
|
|
|
function strSplice(str, i, remove, add) {
|
|
|
|
remove = +remove || 0;
|
|
|
|
add = add || '';
|
|
|
|
return str.slice(0, i) + add + str.slice(i + remove);
|
|
|
|
}
|
2014-03-16 02:13:42 +00:00
|
|
|
var lf = state.before.lastIndexOf('\n') + 1;
|
|
|
|
if (options.inverse) {
|
|
|
|
if (/\s/.test(state.before.charAt(lf))) {
|
2014-03-23 02:33:41 +00:00
|
|
|
state.before = strSplice(state.before, lf, 1);
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionStart--;
|
|
|
|
state.selectionEnd--;
|
2014-03-16 02:13:42 +00:00
|
|
|
}
|
|
|
|
state.selection = state.selection.replace(/^[ \t]/gm, '');
|
|
|
|
} else if (state.selection) {
|
2014-03-23 02:33:41 +00:00
|
|
|
state.before = strSplice(state.before, lf, 0, '\t');
|
2014-03-16 02:13:42 +00:00
|
|
|
state.selection = state.selection.replace(/\r?\n(?=[\s\S])/g, '\n\t');
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionStart++;
|
|
|
|
state.selectionEnd++;
|
2014-03-16 02:13:42 +00:00
|
|
|
} else {
|
|
|
|
state.before += '\t';
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionStart++;
|
|
|
|
state.selectionEnd++;
|
2014-03-16 02:13:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionEnd = state.selectionStart + state.selection.length;
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
newline: function (state) {
|
|
|
|
var lf = state.before.lastIndexOf('\n') + 1;
|
2014-03-19 22:10:59 +00:00
|
|
|
if(clearNewline) {
|
|
|
|
state.before = state.before.substring(0, lf);
|
|
|
|
state.selection = '';
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionStart = lf;
|
|
|
|
state.selectionEnd = lf;
|
2014-03-19 22:10:59 +00:00
|
|
|
clearNewline = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearNewline = false;
|
|
|
|
var previousLine = state.before.slice(lf);
|
|
|
|
var indentMatch = previousLine.match(/^ {0,3}>[ ]*|^[ \t]*(?:[*+\-]|(\d+)\.)[ \t]|^\s+/);
|
|
|
|
var indent = (indentMatch || [''])[0];
|
|
|
|
if(indentMatch && indentMatch[1]) {
|
|
|
|
var number = parseInt(indentMatch[1], 10);
|
|
|
|
indent = indent.replace(/\d+/, number + 1);
|
|
|
|
}
|
|
|
|
if(indent.length) {
|
|
|
|
clearNewline = true;
|
|
|
|
}
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-04-05 00:54:06 +00:00
|
|
|
undoMgr.currentMode = 'newlines';
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-03-19 00:33:57 +00:00
|
|
|
state.before += '\n' + indent;
|
2014-03-16 02:13:42 +00:00
|
|
|
state.selection = '';
|
2014-04-06 00:59:32 +00:00
|
|
|
state.selectionStart += indent.length + 1;
|
|
|
|
state.selectionEnd = state.selectionStart;
|
2014-03-16 02:13:42 +00:00
|
|
|
},
|
|
|
|
};
|
2014-03-17 02:01:46 +00:00
|
|
|
};
|
2014-03-16 02:13:42 +00:00
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
var sectionList = [];
|
|
|
|
var sectionsToRemove = [];
|
|
|
|
var modifiedSections = [];
|
|
|
|
var insertBeforeSection;
|
|
|
|
function updateSectionList(newSectionList) {
|
|
|
|
|
|
|
|
modifiedSections = [];
|
|
|
|
sectionsToRemove = [];
|
|
|
|
insertBeforeSection = undefined;
|
|
|
|
|
|
|
|
// Render everything if file changed
|
|
|
|
if(fileChanged === true) {
|
|
|
|
sectionsToRemove = sectionList;
|
|
|
|
sectionList = newSectionList;
|
|
|
|
modifiedSections = newSectionList;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find modified section starting from top
|
|
|
|
var leftIndex = sectionList.length;
|
|
|
|
_.some(sectionList, function(section, index) {
|
2014-03-19 00:33:57 +00:00
|
|
|
var newSection = newSectionList[index];
|
|
|
|
if(index >= newSectionList.length ||
|
|
|
|
// Check modified
|
|
|
|
section.textWithFrontMatter != newSection.textWithFrontMatter ||
|
2014-03-25 00:23:42 +00:00
|
|
|
// Check that section has not been detached or moved
|
2014-03-31 00:10:28 +00:00
|
|
|
section.elt.parentNode !== contentElt ||
|
2014-03-21 00:36:28 +00:00
|
|
|
// Check also the content since nodes can be injected in sections via copy/paste
|
2014-03-25 00:23:42 +00:00
|
|
|
section.elt.textContent != newSection.textWithFrontMatter) {
|
2014-03-17 02:01:46 +00:00
|
|
|
leftIndex = index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
// Find modified section starting from bottom
|
|
|
|
var rightIndex = -sectionList.length;
|
|
|
|
_.some(sectionList.slice().reverse(), function(section, index) {
|
2014-03-19 00:33:57 +00:00
|
|
|
var newSection = newSectionList[newSectionList.length - index - 1];
|
|
|
|
if(index >= newSectionList.length ||
|
|
|
|
// Check modified
|
|
|
|
section.textWithFrontMatter != newSection.textWithFrontMatter ||
|
2014-03-25 00:23:42 +00:00
|
|
|
// Check that section has not been detached or moved
|
2014-03-31 00:10:28 +00:00
|
|
|
section.elt.parentNode !== contentElt ||
|
2014-03-21 00:36:28 +00:00
|
|
|
// Check also the content since nodes can be injected in sections via copy/paste
|
2014-03-25 00:23:42 +00:00
|
|
|
section.elt.textContent != newSection.textWithFrontMatter) {
|
2014-03-17 02:01:46 +00:00
|
|
|
rightIndex = -index;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
if(leftIndex - rightIndex > sectionList.length) {
|
|
|
|
// Prevent overlap
|
|
|
|
rightIndex = leftIndex - sectionList.length;
|
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
// Create an array composed of left unmodified, modified, right
|
|
|
|
// unmodified sections
|
|
|
|
var leftSections = sectionList.slice(0, leftIndex);
|
|
|
|
modifiedSections = newSectionList.slice(leftIndex, newSectionList.length + rightIndex);
|
|
|
|
var rightSections = sectionList.slice(sectionList.length + rightIndex, sectionList.length);
|
|
|
|
insertBeforeSection = _.first(rightSections);
|
|
|
|
sectionsToRemove = sectionList.slice(leftIndex, sectionList.length + rightIndex);
|
|
|
|
sectionList = leftSections.concat(modifiedSections).concat(rightSections);
|
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
|
|
|
|
function highlightSections() {
|
2014-03-18 01:10:22 +00:00
|
|
|
var newSectionEltList = document.createDocumentFragment();
|
|
|
|
modifiedSections.forEach(function(section) {
|
|
|
|
highlight(section);
|
2014-03-25 00:23:42 +00:00
|
|
|
newSectionEltList.appendChild(section.elt);
|
2014-03-18 01:10:22 +00:00
|
|
|
});
|
2014-03-30 01:44:51 +00:00
|
|
|
watcher.noWatch(function() {
|
2014-03-27 00:20:08 +00:00
|
|
|
if(fileChanged === true) {
|
2014-03-31 00:10:28 +00:00
|
|
|
contentElt.innerHTML = '';
|
|
|
|
contentElt.appendChild(newSectionEltList);
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd();
|
2014-03-18 01:10:22 +00:00
|
|
|
}
|
2014-03-27 00:20:08 +00:00
|
|
|
else {
|
|
|
|
// Remove outdated sections
|
|
|
|
sectionsToRemove.forEach(function(section) {
|
|
|
|
// section can be already removed
|
2014-03-31 00:10:28 +00:00
|
|
|
section.elt.parentNode === contentElt && contentElt.removeChild(section.elt);
|
2014-03-27 00:20:08 +00:00
|
|
|
});
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-03-27 00:20:08 +00:00
|
|
|
if(insertBeforeSection !== undefined) {
|
2014-03-31 00:10:28 +00:00
|
|
|
contentElt.insertBefore(newSectionEltList, insertBeforeSection.elt);
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-03-31 00:10:28 +00:00
|
|
|
contentElt.appendChild(newSectionEltList);
|
2014-03-18 01:10:22 +00:00
|
|
|
}
|
2014-03-21 00:36:28 +00:00
|
|
|
|
2014-03-27 00:20:08 +00:00
|
|
|
// Remove unauthorized nodes (text nodes outside of sections or duplicated sections via copy/paste)
|
2014-03-31 00:10:28 +00:00
|
|
|
var childNode = contentElt.firstChild;
|
2014-03-27 00:20:08 +00:00
|
|
|
while(childNode) {
|
|
|
|
var nextNode = childNode.nextSibling;
|
|
|
|
if(!childNode.generated) {
|
2014-03-31 00:10:28 +00:00
|
|
|
contentElt.removeChild(childNode);
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
|
|
|
childNode = nextNode;
|
|
|
|
}
|
2014-04-05 00:54:06 +00:00
|
|
|
selectionMgr.setSelectionStartEnd();
|
2014-03-27 00:20:08 +00:00
|
|
|
}
|
2014-03-25 00:23:42 +00:00
|
|
|
});
|
2014-03-17 02:01:46 +00:00
|
|
|
}
|
2014-03-19 00:33:57 +00:00
|
|
|
|
2014-04-06 00:59:32 +00:00
|
|
|
var escape = (function() {
|
|
|
|
var entityMap = {
|
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
"\u00a0": ' ',
|
|
|
|
};
|
|
|
|
return function(str) {
|
|
|
|
return str.replace(/[&<\u00a0]/g, function(s) {
|
|
|
|
return entityMap[s];
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})();
|
2014-04-02 23:35:07 +00:00
|
|
|
|
2014-03-17 02:01:46 +00:00
|
|
|
function highlight(section) {
|
2014-04-02 23:35:07 +00:00
|
|
|
var text = escape(section.text);
|
2014-03-19 22:10:59 +00:00
|
|
|
text = Prism.highlight(text, Prism.languages.md);
|
2014-03-23 02:33:41 +00:00
|
|
|
var frontMatter = section.textWithFrontMatter.substring(0, section.textWithFrontMatter.length - section.text.length);
|
2014-03-19 22:10:59 +00:00
|
|
|
if(frontMatter.length) {
|
2014-03-21 00:36:28 +00:00
|
|
|
// Front matter highlighting
|
2014-04-02 23:35:07 +00:00
|
|
|
frontMatter = escape(frontMatter);
|
2014-03-19 22:10:59 +00:00
|
|
|
frontMatter = frontMatter.replace(/\n/g, '<span class="token lf">\n</span>');
|
|
|
|
text = '<span class="token md">' + frontMatter + '</span>' + text;
|
|
|
|
}
|
2014-03-18 01:10:22 +00:00
|
|
|
var sectionElt = crel('span', {
|
2014-03-17 02:01:46 +00:00
|
|
|
id: 'wmd-input-section-' + section.id,
|
|
|
|
class: 'wmd-input-section'
|
|
|
|
});
|
2014-03-21 00:36:28 +00:00
|
|
|
sectionElt.generated = true;
|
2014-03-19 22:10:59 +00:00
|
|
|
sectionElt.innerHTML = text;
|
2014-03-25 00:23:42 +00:00
|
|
|
section.elt = sectionElt;
|
2014-03-16 02:13:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-05 00:54:06 +00:00
|
|
|
eventMgr.onEditorCreated(editor);
|
2014-03-18 01:10:22 +00:00
|
|
|
return editor;
|
2014-03-19 00:33:57 +00:00
|
|
|
});
|