Skip to content

Commit

Permalink
testing saving original content via lStorage #26, #27, #27
Browse files Browse the repository at this point in the history
  • Loading branch information
w8tcha committed Sep 30, 2017
1 parent 02f0ff4 commit 28027f7
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions textselection/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
CKEDITOR.plugins.add('textselection',
{
version: 1.06,
version: "1.07",
init: function (editor) {

if (editor.config.fullPage) {
Expand Down Expand Up @@ -100,6 +100,7 @@
if (editor.mode === 'wysiwyg' && editor.getData()) {
if (CKEDITOR.env.gecko && !editor.focusManager.hasFocus) {
return;

}
var sel = editor.getSelection(), range;
if (sel && (range = sel.getRanges()[0])) {
Expand Down Expand Up @@ -332,6 +333,23 @@

this.startOffset = removeBookmarkText(bookmark.startNode);
this.endOffset = removeBookmarkText(bookmark.endNode);

var saveKey = 'textselection_' + window.location + "_" + document.getElementById(editor.name).name;

// compare length here...
var savedContent = localStorage.getItem(saveKey);

var diff = content.length - savedContent.length;

this.startOffset = this.startOffset - diff;

if (diff > 0) {
//this.endOffset = this.endOffset - diff;
}

content = localStorage.getItem(saveKey);
localStorage.removeItem(saveKey);

this.content = content;
this.updateElement();

Expand All @@ -346,6 +364,9 @@
enlarge: function() {
var htmlOpenTagRegexp = /<[a-zA-Z]+(>|.*?[^?]>)/g;
var htmlCloseTagRegexp = /<\/[^>]+>/g;



var content = this.content,
start = this.startOffset,
end = this.endOffset,
Expand Down Expand Up @@ -389,9 +410,13 @@
this.endOffset = end;
},

createBookmark: function(editor) {
createBookmark: function (editor) {
// Enlarge the range to avoid tag partial selection.
this.enlarge();

// save original content
localStorage.setItem('textselection_' + window.location + "_" + document.getElementById(editor.name).name, this.content);

var content = this.content,
start = this.startOffset,
end = this.endOffset,
Expand All @@ -406,6 +431,7 @@
editor.undoManager.lock();
}


this.content = content;
this.updateElement();
},
Expand Down

0 comments on commit 28027f7

Please sign in to comment.