This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
/
shared-undo-manager.html
92 lines (84 loc) · 2.6 KB
/
shared-undo-manager.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Shared UndoManager</title>
<style>
#wrapper {
width: 90%;
margin: 0 auto;
}
#editor1, #editor2, #output, #stack {
display: block;
border: 1px solid gray;
height: 100px;
width: 100%;
padding: 5px;
overflow: auto;
resize: none;
}
p {
margin: 8px 0;
}
</style>
</head>
<body>
<div id="wrapper">
<h2>Editor 1</h2>
<div id="editor1"></div>
<h2>Editor 2</h2>
<div id="editor2"></div>
<h2>Output</h2>
<textarea id="output" readonly></textarea>
<h2>UndoManager Stack</h2>
<textarea id="stack" readonly></textarea>
</div>
<script src="../bower_components/requirejs/require.js"></script>
<script>
require({
paths: {
'lodash-amd': '../bower_components/lodash-amd',
'immutable': '../bower_components/immutable'
}
}, [
'../src/scribe',
'../src/undo-manager'
], function (
Scribe,
UndoManager
) {
var editor1 = document.querySelector('#editor1');
var editor2 = document.querySelector('#editor2');
var output = document.querySelector('#output');
var stack = document.querySelector('#stack');
var undoManager = new UndoManager(40, document);
document.addEventListener('DOMTransaction', update);
document.addEventListener('undo', update);
document.addEventListener('redo', update);
var scribe1 = new Scribe(editor1, {undo: {enabled: true, manager: undoManager, interval: 1000}});
scribe1.on('content-changed', update);
var scribe2 = new Scribe(editor2, {undo: {enabled: true, manager: undoManager, interval: 1000}});
scribe2.on('content-changed', update);
function update() {
output.value = 'Editor 1:\n' + scribe1.getHTML() + '\n\nEditor 2:\n' + scribe2.getHTML();
// for debugging
stack.value = '';
for (var i = 0; i <= undoManager.length; i++) {
stack.value += (undoManager.position == i ? '> ' : ' ');
if (i < undoManager.length) {
var item = undoManager.item(i)[0];
stack.value += '[' + (item.scribe == scribe1 ? 'editor1' : 'editor2');
stack.value += ': ' + item.content.replace(/<em class="scribe-marker">[^<]*?<\/em>/g, '|') + ']\n';
}
else if (undoManager.length > 0 && i == undoManager.length) {
var item = undoManager.item(i - 1)[0];
stack.value += '[' + (item.scribe == scribe1 ? 'editor1' : 'editor2');
stack.value += ': ' + item.previousItem.content.replace(/<em class="scribe-marker">[^<]*?<\/em>/g, '|') + ']\n';
}
}
}
update();
});
</script>
</body>
</html>