braid-text 0.0.1 → 0.0.4
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/README.md +87 -44
- package/editor.html +4 -4
- package/markdown-editor.html +5 -7
- package/package.json +1 -1
- package/{client.js → simpleton-client.js} +5 -5
package/README.md
CHANGED
|
@@ -12,7 +12,25 @@ This library provides a simple http route handler, along with client code, enabl
|
|
|
12
12
|
- Fast / Robust / Extensively fuzz-tested
|
|
13
13
|
- Developed in [braid.org](https://braid.org)
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
### Demo: a Wiki!
|
|
16
|
+
|
|
17
|
+
This will run a collaboratively-editable wiki:
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
npm install
|
|
21
|
+
node server-demo.js
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Now open these URLs in your browser:
|
|
25
|
+
- http://localhost:8888/demo (to see the demo text)
|
|
26
|
+
- http://localhost:8888/demo?editor (to edit the text)
|
|
27
|
+
- http://localhost:8888/demo?markdown-editor (to edit it as markdown)
|
|
28
|
+
|
|
29
|
+
Or try opening the URL in [Braid-Chrome](https://github.com/braid-org/braid-chrome), or another Braid client, to edit it directly!
|
|
30
|
+
|
|
31
|
+
Check out the `server-demo.js` file to see examples for how to add access control, and a `/pages` endpoint to show all the edited pages.
|
|
32
|
+
|
|
33
|
+
## General Use on Server
|
|
16
34
|
|
|
17
35
|
Install it in your project:
|
|
18
36
|
```shell
|
|
@@ -32,25 +50,7 @@ server.on("request", (req, res) => {
|
|
|
32
50
|
})
|
|
33
51
|
```
|
|
34
52
|
|
|
35
|
-
##
|
|
36
|
-
|
|
37
|
-
This will run a collaboratively-editable wiki:
|
|
38
|
-
|
|
39
|
-
```shell
|
|
40
|
-
npm install
|
|
41
|
-
node server-demo.js
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Now open these URLs in your browser:
|
|
45
|
-
- http://localhost:8888/demo (to see the demo text)
|
|
46
|
-
- http://localhost:8888/demo?editor (to edit the text)
|
|
47
|
-
- http://localhost:8888/demo?markdown-editor (to edit it as markdown)
|
|
48
|
-
|
|
49
|
-
Or try opening the URL in [Braid-Chrome](https://github.com/braid-org/braid-chrome), or another Braid client, to edit it directly!
|
|
50
|
-
|
|
51
|
-
Check out the `server-demo.js` file to see examples for how to add access control, and a `/pages` endpoint to show all the edited pages.
|
|
52
|
-
|
|
53
|
-
## Full Server Library API
|
|
53
|
+
## Server API
|
|
54
54
|
|
|
55
55
|
`braid_text.db_folder = './braid-text-db' // <-- this is the default`
|
|
56
56
|
- This is where the Diamond-Types history files will be stored for each resource.
|
|
@@ -88,35 +88,78 @@ Check out the `server-demo.js` file to see examples for how to add access contro
|
|
|
88
88
|
- `patches`: <small style="color:lightgrey">[optional]</small> Array of patches, each of the form `{unit: 'text', range: '[1:3]', content: 'hi'}`, which would replace the second and third unicode code-points in the text with `hi`.
|
|
89
89
|
- `peer`: <small style="color:lightgrey">[optional]</small> Identifies this peer. This mutation will not be echoed back to `get` subscriptions that use this same `peer` header.
|
|
90
90
|
|
|
91
|
-
## Use
|
|
91
|
+
## General Use on Client
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
```html
|
|
94
|
+
<script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
|
|
95
|
+
<script>
|
|
96
|
+
|
|
97
|
+
// connect to the server
|
|
98
|
+
let simpleton = simpleton_client('https://example.org/some-resource', {
|
|
99
|
+
apply_remote_update: ({ state, patches }) => {
|
|
100
|
+
|
|
101
|
+
// Apply the incoming state or patches to local text here.
|
|
102
|
+
|
|
103
|
+
// Then return the new state of textarea as a string:
|
|
104
|
+
return new_state
|
|
105
|
+
},
|
|
106
|
+
generate_local_diff_update: (prev_state) => {
|
|
107
|
+
|
|
108
|
+
// Compute diff between prev_state ^ and the current textarea string...
|
|
109
|
+
|
|
110
|
+
// Then return the new state (as a string) and the diff (as `patches`)
|
|
111
|
+
return {new_state, patches}
|
|
112
|
+
},
|
|
113
|
+
})
|
|
105
114
|
|
|
106
|
-
|
|
115
|
+
...
|
|
107
116
|
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
// When changes occur in client's textarea, let simpleton know,
|
|
118
|
+
// so that it can call generate_local_diff_update() to ask for them.
|
|
119
|
+
simpleton.changed()
|
|
120
|
+
|
|
121
|
+
</script>
|
|
122
|
+
```
|
|
110
123
|
|
|
111
124
|
See [editor.html](https://raw.githubusercontent.com/braid-org/braid-text/master/editor.html) for a simple working example.
|
|
112
125
|
|
|
113
|
-
##
|
|
126
|
+
## Client API
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
simpleton = simpleton_client(url, options)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
- `url`: The URL of the resource to synchronize with.
|
|
133
|
+
- `options`: An object containing the following properties:
|
|
134
|
+
- `apply_remote_update`: A function that will be called whenever an update is received from the server. It should have the following signature:
|
|
135
|
+
|
|
136
|
+
```javascript
|
|
137
|
+
({state, patches}) => {...}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
- `state`: If present, represents the new value of the text.
|
|
141
|
+
- `patches`: If present, an array of patch objects, each representing a string-replace operation. Each patch object has the following properties:
|
|
142
|
+
- `range`: An array of two numbers, `[start, end]`, specifying the start and end positions of the characters to be deleted.
|
|
143
|
+
- `content`: The text to be inserted in place of the deleted characters.
|
|
144
|
+
|
|
145
|
+
Note that patches will always be in order, but the range positions of each patch reference the original string, i.e., the second patch's range values do not take into account the application of the first patch.
|
|
146
|
+
|
|
147
|
+
The function should apply the `state` or `patches` to the local text and return the new state.
|
|
148
|
+
|
|
149
|
+
- `generate_local_diff_update`: A function that will be called whenever a local update occurs, but may be delayed if the network is congested. It should have the following signature:
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
(prev_state) => {...}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The function should calculate the difference between `prev_state` and the current state, and express this difference as an array of patches (similar to the ones described in `apply_remote_update`).
|
|
156
|
+
|
|
157
|
+
If a difference is detected, the function should return an object with the following properties:
|
|
158
|
+
- `new_state`: The current state of the text.
|
|
159
|
+
- `patches`: An array of patch objects representing the changes.
|
|
160
|
+
|
|
161
|
+
If no difference is detected, the function should return `undefined` or `null`.
|
|
114
162
|
|
|
115
|
-
|
|
116
|
-
apply_remote_update,
|
|
117
|
-
generate_local_diff_update}
|
|
163
|
+
- `content_type`: <small style="color:lightgrey">[optional]</small> If set, this value will be sent in the `Accept` and `Content-Type` headers to the server.
|
|
118
164
|
|
|
119
|
-
|
|
120
|
-
- `apply_remote_update`: This function will be called whenever an update is received from the server. The function should look like `({state, patches}) => {...}`. Only one of `state` or `patches` will be set. If it is `state`, then this is the new value of the text. If it is `patches`, then patches is an array of values like `{range: [1, 3], content: "Hi"}`. Each such value represents a string-replace operation; the `range` specifies a start and end position — these characters will be deleted — and `content` says what text to put in its place. Note that these patches will always be in order, but that the range positions of each patch always reference the original string, e.g., the second patch's range values do not take into account applying the first patch. Finally, this function returns the new state, after the application of the `state` or `patches`.
|
|
121
|
-
- `generate_local_diff_update`: This function will often be called whenever an update happens locally, but the system may delay calling it if the network is congested. The function should look like `(prev_state) => {...}`. The function should basically do a diff between `prev_state` and the current state, and express this diff as an array of patches similar to the ones discussed above. Finally, if there is an actual difference detected, this function should return an object `{state, patches}`, otherwise it should return nothing.
|
|
122
|
-
- `braid_text.changed()`: Call this to report local updates whenever they occur, e.g., in the `oninput` handler of a textarea being synchronized.
|
|
165
|
+
- `simpleton.changed()`: Call this function to report local updates whenever they occur, e.g., in the `oninput` event handler of a textarea being synchronized.
|
package/editor.html
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
</body>
|
|
7
7
|
<script src="https://braid.org/code/myers-diff1.js"></script>
|
|
8
8
|
<script src="https://unpkg.com/braid-http@~0.3/braid-http-client.js"></script>
|
|
9
|
-
<script src="https://unpkg.com/braid-text/client.js"></script>
|
|
9
|
+
<script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
|
|
10
10
|
<script>
|
|
11
|
-
let
|
|
11
|
+
let simpleton = simpleton_client(location.pathname, {
|
|
12
12
|
apply_remote_update: ({ state, patches }) => {
|
|
13
13
|
if (state !== undefined) texty.value = state;
|
|
14
14
|
else apply_patches_and_update_selection(texty, patches);
|
|
@@ -17,12 +17,12 @@
|
|
|
17
17
|
generate_local_diff_update: (prev_state) => {
|
|
18
18
|
var patches = diff(prev_state, texty.value);
|
|
19
19
|
if (patches.length === 0) return null;
|
|
20
|
-
return { patches,
|
|
20
|
+
return { patches, new_state: texty.value };
|
|
21
21
|
},
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
texty.value = "";
|
|
25
|
-
texty.oninput = (e) =>
|
|
25
|
+
texty.oninput = (e) => simpleton.changed();
|
|
26
26
|
|
|
27
27
|
function diff(before, after) {
|
|
28
28
|
let diff = diff_main(before, after);
|
package/markdown-editor.html
CHANGED
|
@@ -16,19 +16,17 @@ dom.BODY = -> DIV(WIKI())
|
|
|
16
16
|
<script>
|
|
17
17
|
window.fetch = window.statebus_fetch
|
|
18
18
|
</script>
|
|
19
|
-
<script src="https://unpkg.com/braid-text/client.js"></script>
|
|
19
|
+
<script src="https://unpkg.com/braid-text/simpleton-client.js"></script>
|
|
20
20
|
|
|
21
21
|
<script>
|
|
22
22
|
|
|
23
|
-
var apply_patches_and_update_selection, diff, first_time, i, j, render_delay, scroll,
|
|
24
|
-
|
|
25
|
-
braid_text_key = location.pathname;
|
|
23
|
+
var apply_patches_and_update_selection, diff, first_time, i, j, render_delay, scroll, t, timer, ting, toggle_editor, update_markdown, update_markdown_later;
|
|
26
24
|
|
|
27
25
|
t = function() {
|
|
28
26
|
return document.getElementById('the editor');
|
|
29
27
|
};
|
|
30
28
|
|
|
31
|
-
var
|
|
29
|
+
var simpleton = simpleton_client(location.pathname, {
|
|
32
30
|
apply_remote_update: function(x) {
|
|
33
31
|
if (x.state !== void 0) {
|
|
34
32
|
t().value = x.state;
|
|
@@ -47,7 +45,7 @@ var braid_text = braid_text_client(braid_text_key, {
|
|
|
47
45
|
}
|
|
48
46
|
return {
|
|
49
47
|
patches: patches,
|
|
50
|
-
|
|
48
|
+
new_state: t().value
|
|
51
49
|
};
|
|
52
50
|
}
|
|
53
51
|
});
|
|
@@ -188,7 +186,7 @@ dom.WIKI = function() {
|
|
|
188
186
|
}
|
|
189
187
|
// Bail on edits that try to wipe us out
|
|
190
188
|
state.source = e.target.value;
|
|
191
|
-
|
|
189
|
+
simpleton.changed();
|
|
192
190
|
return update_markdown_later();
|
|
193
191
|
},
|
|
194
192
|
defaultValue: state.source
|
package/package.json
CHANGED
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
//
|
|
10
10
|
// generate_local_diff_update: (prev_state) => {...}
|
|
11
11
|
// this is to generate outgoing changes,
|
|
12
|
-
// and if there are changes, returns { patches,
|
|
12
|
+
// and if there are changes, returns { patches, new_state }
|
|
13
13
|
//
|
|
14
|
-
// content_type:
|
|
14
|
+
// content_type: used for Accept and Content-Type headers
|
|
15
15
|
//
|
|
16
16
|
// returns { changed(): (diff_function) => {...} }
|
|
17
17
|
// this is for outgoing changes;
|
|
18
18
|
// diff_function = () => ({patches, new_version}).
|
|
19
19
|
//
|
|
20
|
-
function
|
|
20
|
+
function simpleton_client(url, { apply_remote_update, generate_local_diff_update, content_type }) {
|
|
21
21
|
var peer = Math.random().toString(36).substr(2)
|
|
22
22
|
var current_version = []
|
|
23
23
|
var prev_state = ""
|
|
@@ -74,7 +74,7 @@ function braid_text_client(url, { apply_remote_update, generate_local_diff_updat
|
|
|
74
74
|
while (true) {
|
|
75
75
|
var update = generate_local_diff_update(prev_state)
|
|
76
76
|
if (!update) return // Stop if there wasn't a change!
|
|
77
|
-
var {patches,
|
|
77
|
+
var {patches, new_state} = update
|
|
78
78
|
|
|
79
79
|
// convert from js-indicies to code-points
|
|
80
80
|
let c = 0
|
|
@@ -103,7 +103,7 @@ function braid_text_client(url, { apply_remote_update, generate_local_diff_updat
|
|
|
103
103
|
|
|
104
104
|
var parents = current_version
|
|
105
105
|
current_version = version
|
|
106
|
-
prev_state =
|
|
106
|
+
prev_state = new_state
|
|
107
107
|
|
|
108
108
|
outstanding_changes++
|
|
109
109
|
await braid_fetch_wrapper(url, {
|