Stackedit/public/res/editor.js

638 lines
22 KiB
JavaScript
Raw Normal View History

2014-03-16 02:13:42 +00:00
/* jshint -W084, -W099 */
define([
'jquery',
2014-03-17 02:01:46 +00:00
'underscore',
2014-03-18 01:10:22 +00:00
'settings',
2014-03-16 02:13:42 +00:00
'eventMgr',
'prism-core',
2014-03-17 02:01:46 +00:00
'crel',
2014-03-21 00:36:28 +00:00
'MutationObservers',
2014-03-16 02:13:42 +00:00
'libs/prism-markdown'
2014-03-18 01:10:22 +00:00
], function ($, _, settings, eventMgr, Prism, crel) {
2014-03-19 00:33:57 +00:00
2014-03-17 02:01:46 +00:00
String.prototype.splice = function (i, remove, add) {
remove = +remove || 0;
add = add || '';
return this.slice(0, i) + add + this.slice(i + remove);
};
2014-03-18 01:10:22 +00:00
var editor = {};
var selectionStart = 0;
var selectionEnd = 0;
var scrollTop = 0;
var inputElt;
2014-03-19 22:10:59 +00:00
var $inputElt;
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);
};
})();
eventMgr.addListener('onPagedownConfigure', function(editor) {
pagedownEditor = editor;
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-21 00:36:28 +00:00
function saveEditorState() {
setTimeout
2014-03-18 01:10:22 +00:00
selectionStart = inputElt.selectionStart;
selectionEnd = inputElt.selectionEnd;
2014-03-21 00:36:28 +00:00
scrollTop = inputElt.scrollTop;
2014-03-18 01:10:22 +00:00
if(fileChanged === false) {
fileDesc.editorStart = selectionStart;
fileDesc.editorEnd = selectionEnd;
2014-03-21 00:36:28 +00:00
fileDesc.editorScrollTop = scrollTop;
}
}
var previousTextContent;
function checkContentChange() {
saveEditorState();
var currentTextContent = inputElt.textContent;
if(fileChanged === false) {
2014-03-18 01:10:22 +00:00
if(currentTextContent == previousTextContent) {
return;
}
2014-03-19 00:33:57 +00:00
if(!/\n$/.test(currentTextContent)) {
currentTextContent += '\n';
}
2014-03-18 01:10:22 +00:00
fileDesc.content = currentTextContent;
eventMgr.onContentChanged(fileDesc);
}
else {
2014-03-19 00:33:57 +00:00
if(!/\n$/.test(currentTextContent)) {
currentTextContent += '\n';
fileDesc.content = currentTextContent;
}
2014-03-18 01:10:22 +00:00
eventMgr.onFileOpen(fileDesc);
previewElt.scrollTop = fileDesc.previewScrollTop;
selectionStart = fileDesc.editorStart;
selectionEnd = fileDesc.editorEnd;
scrollTop = fileDesc.editorScrollTop;
inputElt.scrollTop = scrollTop;
fileChanged = false;
}
previousTextContent = currentTextContent;
}
2014-03-19 00:33:57 +00:00
2014-03-21 00:36:28 +00:00
var cursorY = 0;
function saveCursorCoordinates() {
saveEditorState();
2014-03-19 00:33:57 +00:00
2014-03-21 00:36:28 +00:00
var backwards = false;
var selection = window.getSelection();
if(!selection.rangeCount) {
return;
}
if (!selection.isCollapsed) {
var range = document.createRange();
range.setStart(selection.anchorNode, selection.anchorOffset);
range.setEnd(selection.focusNode, selection.focusOffset);
backwards = range.collapsed;
range.detach();
}
2014-03-19 00:33:57 +00:00
2014-03-21 00:36:28 +00:00
var selectionRange = selection.getRangeAt(0);
var container = backwards ? selectionRange.startContainer : selectionRange.endContainer;
if(container.textContent == '\n') {
cursorY = container.parentNode.offsetTop + container.parentNode.offsetHeight / 2;
}
else {
var cursorOffset = backwards ? selectionStart : selectionEnd;
var selectedChar = inputElt.textContent[cursorOffset];
if(selectedChar === undefined || selectedChar == '\n') {
selectionRange = createRange(cursorOffset - 1, cursorOffset);
2014-03-19 00:33:57 +00:00
}
else {
2014-03-21 00:36:28 +00:00
selectionRange = createRange(cursorOffset, cursorOffset + 1);
2014-03-19 00:33:57 +00:00
}
2014-03-21 00:36:28 +00:00
var selectionRect = selectionRange.getBoundingClientRect();
cursorY = selectionRect.top + selectionRect.height / 2 - inputElt.offsetTop + inputElt.scrollTop;
selectionRange.detach();
}
eventMgr.onCursorCoordinates(0, cursorY);
}
function adjustCursorPosition() {
inputElt && setTimeout(function() {
saveCursorCoordinates();
2014-03-19 00:33:57 +00:00
var adjust = inputElt.offsetHeight / 2;
if(adjust > 130) {
adjust = 130;
}
2014-03-21 00:36:28 +00:00
var cursorMinY = inputElt.scrollTop + adjust;
var cursorMaxY = inputElt.scrollTop + inputElt.offsetHeight - adjust;
2014-03-19 00:33:57 +00:00
if(cursorY < cursorMinY) {
inputElt.scrollTop += cursorY - cursorMinY;
}
else if(cursorY > cursorMaxY) {
inputElt.scrollTop += cursorY - cursorMaxY;
}
}, 0);
}
eventMgr.addListener('onLayoutResize', adjustCursorPosition);
2014-03-18 01:10:22 +00:00
editor.init = function(elt1, elt2) {
inputElt = elt1;
2014-03-19 22:10:59 +00:00
$inputElt = $(inputElt);
2014-03-18 01:10:22 +00:00
previewElt = elt2;
2014-03-21 00:36:28 +00:00
2014-03-18 01:10:22 +00:00
editor.contentElt = crel('div', {
2014-03-19 22:10:59 +00:00
class: 'editor-content',
2014-03-18 01:10:22 +00:00
contenteditable: true
});
inputElt.appendChild(editor.contentElt);
2014-03-21 00:36:28 +00:00
editor.$contentElt = $(editor.contentElt);
2014-03-19 00:33:57 +00:00
2014-03-21 00:36:28 +00:00
editor.marginElt = crel('div', {
class: 'editor-margin'
});
inputElt.appendChild(editor.marginElt);
editor.$marginElt = $(editor.marginElt);
var observer = new MutationObserver(checkContentChange);
observer.observe(editor.contentElt, {
childList: true,
subtree: true,
characterData: true
});
$(inputElt).scroll(saveEditorState);
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-03-18 01:10:22 +00:00
inputElt.focus = function() {
editor.$contentElt.focus();
this.setSelectionRange(selectionStart, selectionEnd);
inputElt.scrollTop = scrollTop;
2014-03-16 02:13:42 +00:00
};
2014-03-18 01:10:22 +00:00
editor.$contentElt.focus(function() {
inputElt.focused = true;
2014-03-16 02:13:42 +00:00
});
2014-03-18 01:10:22 +00:00
editor.$contentElt.blur(function() {
inputElt.focused = false;
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, 'value', {
2014-03-16 02:13:42 +00:00
get: function () {
return this.textContent;
},
set: function (value) {
var currentValue = this.textContent;
// Find the first modified char
var startIndex = 0;
var startIndexMax = Math.min(currentValue.length, value.length);
while (startIndex < startIndexMax) {
if (currentValue.charCodeAt(startIndex) !== value.charCodeAt(startIndex)) {
break;
}
startIndex++;
}
// Find the last modified char
var endIndex = 1;
var endIndexMax = Math.min(currentValue.length - startIndex, value.length - startIndex);
while (endIndex <= endIndexMax) {
if (currentValue.charCodeAt(currentValue.length - endIndex) !== value.charCodeAt(value.length - endIndex)) {
break;
}
endIndex++;
}
var replacementText = value.substring(startIndex, value.length - endIndex + 1);
endIndex = currentValue.length - endIndex + 1;
2014-03-19 00:33:57 +00:00
var range = createRange(startIndex, endIndex);
2014-03-16 02:13:42 +00:00
range.deleteContents();
range.insertNode(document.createTextNode(replacementText));
}
});
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 () {
var selection = window.getSelection();
if (selection.rangeCount) {
var range = selection.getRangeAt(0),
element = range.startContainer,
container = element,
offset = range.startOffset;
if (!(this.compareDocumentPosition(element) & 0x10)) {
return 0;
}
do {
while (element = element.previousSibling) {
if (element.textContent) {
offset += element.textContent.length;
}
}
element = container = container.parentNode;
} while (element && element != this);
return offset;
} else {
return 0;
}
},
set: function (value) {
2014-03-18 01:10:22 +00:00
inputElt.setSelectionRange(value, selectionEnd);
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 () {
var selection = window.getSelection();
if (selection.rangeCount) {
return this.selectionStart + (selection.getRangeAt(0) + '').length;
} else {
return 0;
}
},
set: function (value) {
2014-03-18 01:10:22 +00:00
inputElt.setSelectionRange(selectionStart, value);
2014-03-16 02:13:42 +00:00
},
enumerable: true,
configurable: true
});
2014-03-18 01:10:22 +00:00
inputElt.setSelectionRange = function (ss, se) {
selectionStart = ss;
selectionEnd = se;
2014-03-19 00:33:57 +00:00
var range = createRange(ss, se);
2014-03-16 02:13:42 +00:00
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
};
2014-03-19 22:10:59 +00:00
var clearNewline = false;
2014-03-18 01:10:22 +00:00
editor.$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-03-21 00:36:28 +00:00
saveEditorState();
adjustCursorPosition();
var cmdOrCtrl = evt.metaKey || evt.ctrlKey;
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
})
.on('mouseup', saveCursorCoordinates)
.on('paste', function () {
2014-03-18 01:10:22 +00:00
pagedownEditor.undoManager.setMode("paste");
2014-03-21 00:36:28 +00:00
adjustCursorPosition();
})
.on('cut', function () {
2014-03-18 01:10:22 +00:00
pagedownEditor.undoManager.setMode("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-03-18 01:10:22 +00:00
var text = inputElt.value,
ss = options.start || selectionStart,
se = options.end || selectionEnd,
2014-03-16 02:13:42 +00:00
state = {
ss: ss,
se: se,
before: text.slice(0, ss),
after: text.slice(se),
selection: text.slice(ss, se)
};
actions[action](state, options);
2014-03-18 01:10:22 +00:00
inputElt.value = state.before + state.selection + state.after;
inputElt.setSelectionRange(state.ss, state.se);
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) {
var lf = state.before.lastIndexOf('\n') + 1;
2014-03-18 01:10:22 +00:00
pagedownEditor.undoManager.setMode("typing");
2014-03-16 02:13:42 +00:00
if (options.inverse) {
if (/\s/.test(state.before.charAt(lf))) {
state.before = state.before.splice(lf, 1);
state.ss--;
state.se--;
}
state.selection = state.selection.replace(/^[ \t]/gm, '');
} else if (state.selection) {
state.before = state.before.splice(lf, 0, '\t');
state.selection = state.selection.replace(/\r?\n(?=[\s\S])/g, '\n\t');
state.ss++;
state.se++;
} else {
state.before += '\t';
state.ss++;
state.se++;
return;
}
state.se = state.ss + state.selection.length;
},
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 = '';
state.ss = lf;
state.se = lf;
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-03-18 01:10:22 +00:00
pagedownEditor.undoManager.setMode("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 = '';
state.ss += indent.length + 1;
state.se = state.ss;
},
};
2014-03-17 02:01:46 +00:00
};
2014-03-16 02:13:42 +00:00
2014-03-19 00:33:57 +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 ||
// Check that section has not been detached from the DOM with backspace
2014-03-21 00:36:28 +00:00
!section.highlightedContent.parentNode ||
// Check also the content since nodes can be injected in sections via copy/paste
section.highlightedContent.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 ||
// Check that section has not been detached from the DOM with backspace
!section.highlightedContent.parentNode ||
2014-03-21 00:36:28 +00:00
// Check also the content since nodes can be injected in sections via copy/paste
2014-03-19 00:33:57 +00:00
section.highlightedContent.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);
newSectionEltList.appendChild(section.highlightedContent);
});
2014-03-17 02:01:46 +00:00
if(fileChanged === true) {
2014-03-18 01:10:22 +00:00
editor.contentElt.innerHTML = '';
editor.contentElt.appendChild(newSectionEltList);
inputElt.setSelectionRange(selectionStart, selectionEnd);
}
else {
// Remove outdated sections
sectionsToRemove.forEach(function(section) {
var sectionElt = document.getElementById("wmd-input-section-" + section.id);
// section can be already removed
sectionElt && editor.contentElt.removeChild(sectionElt);
2014-03-17 02:01:46 +00:00
});
2014-03-19 00:33:57 +00:00
2014-03-18 01:10:22 +00:00
if(insertBeforeSection !== undefined) {
var insertBeforeElt = document.getElementById("wmd-input-section-" + insertBeforeSection.id);
editor.contentElt.insertBefore(newSectionEltList, insertBeforeElt);
}
else {
editor.contentElt.appendChild(newSectionEltList);
}
2014-03-19 00:33:57 +00:00
2014-03-21 00:36:28 +00:00
// Remove unauthorized nodes (text nodes outside of sections or duplicated sections via copy/paste)
2014-03-18 01:10:22 +00:00
var childNode = editor.contentElt.firstChild;
while(childNode) {
var nextNode = childNode.nextSibling;
2014-03-21 00:36:28 +00:00
if(!childNode.generated) {
2014-03-18 01:10:22 +00:00
editor.contentElt.removeChild(childNode);
}
childNode = nextNode;
}
2014-03-21 00:36:28 +00:00
inputElt.setSelectionRange(selectionStart, selectionEnd);
2014-03-17 02:01:46 +00:00
}
}
2014-03-19 00:33:57 +00:00
2014-03-17 02:01:46 +00:00
function highlight(section) {
2014-03-19 22:10:59 +00:00
var text = section.text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
text = Prism.highlight(text, Prism.languages.md);
var frontMatter = section.textWithFrontMatter.substring(0, section.textWithFrontMatter.length-section.text.length);
if(frontMatter.length) {
2014-03-21 00:36:28 +00:00
// Front matter highlighting
2014-03-19 22:10:59 +00:00
frontMatter = frontMatter.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
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-17 02:01:46 +00:00
section.highlightedContent = sectionElt;
2014-03-16 02:13:42 +00:00
}
2014-03-19 00:33:57 +00:00
function createRange(ss, se) {
function findOffset(ss) {
var offset = 0,
element = editor.contentElt,
container;
do {
container = element;
element = element.firstChild;
if (element) {
do {
var len = element.textContent.length;
if (offset <= ss && offset + len > ss) {
break;
}
offset += len;
} while (element = element.nextSibling);
}
if (!element) {
// It's the container's lastChild
break;
}
} while (element && element.hasChildNodes() && element.nodeType != 3);
if (element) {
return {
element: element,
offset: ss - offset
};
} else if (container) {
element = container;
while (element && element.lastChild) {
element = element.lastChild;
}
if (element.nodeType === 3) {
return {
element: element,
offset: element.textContent.length
};
} else {
return {
element: element,
offset: 0
};
}
}
return {
element: editor.contentElt,
offset: 0,
error: true
};
}
var range = document.createRange(),
offset = findOffset(ss);
range.setStart(offset.element, offset.offset);
if (se && se != ss) {
offset = findOffset(se);
}
range.setEnd(offset.element, offset.offset);
return range;
}
2014-03-18 01:10:22 +00:00
return editor;
2014-03-19 00:33:57 +00:00
});