braid-text 0.2.64 → 0.2.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/editor.html CHANGED
@@ -1,81 +1,23 @@
1
- <body style="background: auto; margin: 0px; padding: 0px">
2
- <textarea
3
- id="texty"
4
- style="width: 100%; height: 100%; box-sizing: border-box"
5
- ></textarea>
1
+ <body style="margin: 0px; padding: 0px; box-sizing: border-box">
2
+ <textarea id="the_editor" style="width: 100%; height: 100%;"></textarea>
6
3
  </body>
7
4
  <script src="https://braid.org/code/myers-diff1.js"></script>
8
5
  <script src="https://unpkg.com/braid-http@~1.3/braid-http-client.js"></script>
6
+ <script src="/web-utils.js"></script>
9
7
  <script src="/simpleton-client.js"></script>
10
8
  <script>
9
+
11
10
  let simpleton = simpleton_client(location.pathname, {
12
- on_patches: (patches) => apply_patches_and_update_selection(texty, patches),
13
- get_patches: (prev_state) => diff(prev_state, texty.value),
14
- get_state: () => texty.value,
15
- on_error: (e) => {
16
- texty.disabled = true
17
- texty.style.background = '#fee'
18
- texty.style.border = '4px solid red'
19
- },
20
- on_ack: () => texty.style.caretColor = ''
11
+ on_patches: (patches) => apply_patches_and_update_selection(the_editor, patches),
12
+ get_patches: (prev_state) => diff(prev_state, the_editor.value),
13
+ get_state: () => the_editor.value,
14
+ on_error: (e) => set_error_state(the_editor),
15
+ on_ack: () => set_acked_state(the_editor)
21
16
  });
22
17
 
23
- texty.value = "";
24
- texty.oninput = (e) => {
25
- texty.style.caretColor = 'red'
26
- simpleton.changed();
27
- }
28
-
29
- function diff(before, after) {
30
- let diff = diff_main(before, after);
31
- let patches = [];
32
- let offset = 0;
33
- for (let d of diff) {
34
- let p = null;
35
- if (d[0] == 1) p = { range: [offset, offset], content: d[1] };
36
- else if (d[0] == -1) {
37
- p = { range: [offset, offset + d[1].length], content: "" };
38
- offset += d[1].length;
39
- } else offset += d[1].length;
40
- if (p) {
41
- p.unit = "text";
42
- patches.push(p);
43
- }
44
- }
45
- return patches;
18
+ the_editor.oninput = (e) => {
19
+ set_acked_state(the_editor, false)
20
+ simpleton.changed()
46
21
  }
47
22
 
48
- function apply_patches_and_update_selection(textarea, patches) {
49
- let offset = 0;
50
- for (let p of patches) {
51
- p.range[0] += offset;
52
- p.range[1] += offset;
53
- offset -= p.range[1] - p.range[0];
54
- offset += p.content.length;
55
- }
56
-
57
- let original = textarea.value;
58
- let sel = [textarea.selectionStart, textarea.selectionEnd];
59
-
60
- for (var p of patches) {
61
- let range = p.range;
62
-
63
- for (let i = 0; i < sel.length; i++)
64
- if (sel[i] > range[0])
65
- if (sel[i] > range[1]) sel[i] -= range[1] - range[0];
66
- else sel[i] = range[0];
67
-
68
- for (let i = 0; i < sel.length; i++)
69
- if (sel[i] > range[0]) sel[i] += p.content.length;
70
-
71
- original =
72
- original.substring(0, range[0]) +
73
- p.content +
74
- original.substring(range[1]);
75
- }
76
-
77
- textarea.value = original;
78
- textarea.selectionStart = sel[0];
79
- textarea.selectionEnd = sel[1];
80
- }
81
23
  </script>