Skip to content

Commit

Permalink
feat: history more accurate
Browse files Browse the repository at this point in the history
history plugin should use before all plugins
  • Loading branch information
petersolopov committed Jun 18, 2020
1 parent 5181bb9 commit 910e397
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/plugins.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import Yace from "../src/index.js";
import tab from "../src/plugins/tab.js";
import cutLine from "../src/plugins/cutLine.js";
import preserveIndent from "../src/plugins/preserveIndent.js";
import history from "../src/plugins/history.js";

const editor = new Yace("#editor", {
value:
Expand All @@ -29,7 +31,7 @@
fontSize: "20px",
},
highlighter: highlight,
plugins: [tab(), cutLine()],
plugins: [history(), tab(), cutLine(), preserveIndent()],
lineNumbers: true,
});

Expand Down
20 changes: 15 additions & 5 deletions src/plugins/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ function history() {
activeIndex = stack.length - 1;
};

const shouldRecord = (record) => {
return (
stack[activeIndex].value !== record.value ||
stack[activeIndex].selectionStart !== record.selectionStart ||
stack[activeIndex].selectionEnd !== record.selectionEnd
);
};

return (textareaProps, event) => {
if (event.type === "keydown") {
if (isKey("ctrl/cmd+z", event)) {
event.preventDefault();

if (activeIndex !== null) {
// after applying all plugins it can be new props
if (shouldRecord(textareaProps)) {
stack.push(textareaProps);
activeIndex++;
}

const newActiveIndex = Math.max(0, activeIndex - 1);
activeIndex = newActiveIndex;
return stack[newActiveIndex];
Expand All @@ -42,11 +56,7 @@ function history() {
return;
}

if (
stack[activeIndex].value !== textareaProps.value ||
stack[activeIndex].selectionStart !== textareaProps.selectionStart ||
stack[activeIndex].selectionEnd !== textareaProps.selectionEnd
) {
if (shouldRecord(textareaProps)) {
rewriteHistory(textareaProps);
return;
}
Expand Down

0 comments on commit 910e397

Please sign in to comment.