yrby 0.7.1 → 0.8.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +88 -0
- data/Cargo.lock +3 -3
- data/README.md +593 -384
- data/ext/yrby/crates/html-core/Cargo.toml +1 -1
- data/ext/yrby/crates/html-core/src/lib.rs +13 -13
- data/ext/yrby/crates/lexical-html/Cargo.toml +2 -2
- data/ext/yrby/crates/lexical-html/src/lib.rs +90 -45
- data/ext/yrby/crates/prosemirror-html/Cargo.toml +2 -2
- data/ext/yrby/crates/prosemirror-html/src/lib.rs +44 -35
- data/ext/yrby/src/lib.rs +27 -12
- data/ext/yrby/src/protocol.rs +17 -17
- data/ext/yrby/src/read.rs +137 -26
- data/lib/generators/yrby/install/install_generator.rb +24 -13
- data/lib/generators/yrby/install/templates/document_channel.rb +4 -7
- data/lib/generators/yrby/tables/tables_generator.rb +7 -4
- data/lib/generators/yrby/tables/templates/create_y_tables.rb +1 -1
- data/lib/y/collaborative/attribute.rb +53 -0
- data/lib/y/collaborative/helper.rb +48 -0
- data/lib/y/collaborative.rb +98 -0
- data/lib/y/lexxy.rb +4 -4
- data/lib/y/rendering.rb +39 -15
- data/lib/y/tiptap.rb +3 -3
- data/lib/y/version.rb +1 -1
- metadata +4 -9
- data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/common.rs +0 -355
- data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/dynamic.rs +0 -276
- data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/macros.rs +0 -49
- data/ext/yrby/target/debug/build/rb-sys-4407948463231c4f/out/bindings-0.9.128-mri-arm64-darwin23-3.4.7.rs +0 -8934
- data/ext/yrby/target/debug/build/rb-sys-dbeea42737529c2d/out/bindings-0.9.128-mri-arm64-darwin23-3.4.7.rs +0 -8934
- data/ext/yrby/target/debug/build/serde-58ea0ee887cc2602/out/private.rs +0 -6
- data/ext/yrby/target/debug/build/serde_core-41f407c21c1f205e/out/private.rs +0 -5
- data/ext/yrby/target/debug/build/thiserror-0f1416a82ff26f22/out/private.rs +0 -5
data/ext/yrby/src/read.rs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! Pure content-reading helpers over yrs shared types
|
|
1
|
+
//! Pure content-reading helpers over yrs shared types, with no magnus/Ruby, so they
|
|
2
2
|
//! can be unit-tested directly in Rust (like `protocol.rs`). The binding layer in
|
|
3
3
|
//! `lib.rs` is a thin wrapper that opens a transaction and calls these.
|
|
4
4
|
|
|
@@ -6,8 +6,8 @@ use std::collections::HashMap;
|
|
|
6
6
|
use std::sync::Arc;
|
|
7
7
|
use yrs::types::text::YChange;
|
|
8
8
|
use yrs::{
|
|
9
|
-
Any, Array, GetString, Map, MapRef, Out, ReadTxn, Text, Xml, XmlFragment,
|
|
10
|
-
XmlOut, XmlTextRef,
|
|
9
|
+
Any, Array, ArrayRef, GetString, Map, MapRef, Out, ReadTxn, Text, Xml, XmlFragment,
|
|
10
|
+
XmlFragmentRef, XmlOut, XmlTextRef,
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
/// Read an XML-shaped root as text, one top-level block per line.
|
|
@@ -18,7 +18,7 @@ use yrs::{
|
|
|
18
18
|
/// (`<paragraph>…`). `get_string` already recurses these (tags included; the
|
|
19
19
|
/// caller strips them), so we keep that path.
|
|
20
20
|
/// - **Lexical** (Lexxy) stores every node as a `Y.XmlText`, and nests child
|
|
21
|
-
/// blocks (list items, table cells, nested lists) as *embedded* `Y.XmlText`s
|
|
21
|
+
/// blocks (list items, table cells, nested lists) as *embedded* `Y.XmlText`s,
|
|
22
22
|
/// which `get_string` silently omits, dropping all that content. So for a
|
|
23
23
|
/// Lexical block we walk its content (`Text::diff`) instead: text runs build a
|
|
24
24
|
/// line, inline children (links) join it, and nested block children flush the
|
|
@@ -34,7 +34,7 @@ pub fn xml_blocks_text<T: ReadTxn>(txn: &T, fragment: &XmlFragmentRef) -> String
|
|
|
34
34
|
// them (tags kept, caller strips). A Lexical decorator is an
|
|
35
35
|
// XmlElement *with* a `__type`: attachments carry readable text
|
|
36
36
|
// (a mention's plain text, an upload's caption); the rest
|
|
37
|
-
// (horizontal rule) have none
|
|
37
|
+
// (horizontal rule) have none: skip rather than emit their
|
|
38
38
|
// `<UNDEFINED …>` serialization.
|
|
39
39
|
if e.get_attribute(txn, "__type").is_none() {
|
|
40
40
|
out.push(e.get_string(txn));
|
|
@@ -158,35 +158,70 @@ fn walk_lexical_block<T: ReadTxn>(txn: &T, t: &XmlTextRef, out: &mut Vec<String>
|
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
/// Read a `Y.Map` root as a JSON object string (keys sorted
|
|
161
|
+
/// Read a `Y.Map` root as a JSON object string (object keys sorted at every
|
|
162
|
+
/// depth for stable, diffable output).
|
|
162
163
|
///
|
|
163
|
-
/// The complement to `read_text`/`read_xml` for structured state
|
|
164
|
+
/// The complement to `read_text`/`read_xml` for structured state, e.g. a shared
|
|
164
165
|
/// "view state" map. Values are converted recursively: primitives pass through;
|
|
165
166
|
/// nested `Y.Map`/`Y.Array` recurse; `Y.Text`/XML values stringify. The caller
|
|
166
167
|
/// parses the JSON (yrs's own `Out::to_json` is crate-private, so we walk the
|
|
167
168
|
/// `Out` variants ourselves here).
|
|
168
169
|
pub fn map_json<T: ReadTxn>(txn: &T, map: &MapRef) -> String {
|
|
169
|
-
let mut
|
|
170
|
-
|
|
171
|
-
.
|
|
172
|
-
.collect();
|
|
173
|
-
pairs.sort_by(|a, b| a.0.cmp(&b.0)); // deterministic key order
|
|
174
|
-
let mut out = String::from("{");
|
|
175
|
-
for (i, (k, v)) in pairs.iter().enumerate() {
|
|
176
|
-
if i > 0 {
|
|
177
|
-
out.push(',');
|
|
178
|
-
}
|
|
179
|
-
// Any::to_json serializes from the start of the buffer (it doesn't
|
|
180
|
-
// append), so each piece goes into its own String, then concatenated.
|
|
181
|
-
out.push_str(&any_to_json(&Any::String(Arc::from(k.as_str())))); // JSON-escaped key
|
|
182
|
-
out.push(':');
|
|
183
|
-
out.push_str(&any_to_json(v));
|
|
170
|
+
let mut hm: HashMap<String, Any> = HashMap::new();
|
|
171
|
+
for (k, v) in map.iter(txn) {
|
|
172
|
+
hm.insert(k.to_string(), out_to_any(txn, &v));
|
|
184
173
|
}
|
|
185
|
-
|
|
186
|
-
|
|
174
|
+
any_to_json(&Any::Map(Arc::new(hm)))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// A Y.Array's elements as a JSON array string, recursing through nested
|
|
178
|
+
/// shared types exactly as `map_json` does (a Y.Map root and a Y.Array root are
|
|
179
|
+
/// the two document shapes yrs can read from the top). Element order is the
|
|
180
|
+
/// array's own order; nested object keys are sorted like map_json.
|
|
181
|
+
pub fn array_json<T: ReadTxn>(txn: &T, array: &ArrayRef) -> String {
|
|
182
|
+
let items: Vec<Any> = array.iter(txn).map(|o| out_to_any(txn, &o)).collect();
|
|
183
|
+
any_to_json(&Any::Array(items.into()))
|
|
187
184
|
}
|
|
188
185
|
|
|
186
|
+
/// JSON-serialize an `Any` with object keys sorted at every depth. yrs's own
|
|
187
|
+
/// `Any::to_json` iterates `Any::Map` (a `HashMap`) in arbitrary order, so a
|
|
188
|
+
/// nested map would serialize unpredictably and differ run to run; sorting here
|
|
189
|
+
/// makes the whole tree deterministic. Scalars defer to `Any::to_json`, which
|
|
190
|
+
/// writes from the start of a buffer (it does not append), hence each scalar
|
|
191
|
+
/// gets its own fresh `String`.
|
|
189
192
|
fn any_to_json(a: &Any) -> String {
|
|
193
|
+
match a {
|
|
194
|
+
Any::Array(items) => {
|
|
195
|
+
let mut out = String::from("[");
|
|
196
|
+
for (i, item) in items.iter().enumerate() {
|
|
197
|
+
if i > 0 {
|
|
198
|
+
out.push(',');
|
|
199
|
+
}
|
|
200
|
+
out.push_str(&any_to_json(item));
|
|
201
|
+
}
|
|
202
|
+
out.push(']');
|
|
203
|
+
out
|
|
204
|
+
}
|
|
205
|
+
Any::Map(entries) => {
|
|
206
|
+
let mut pairs: Vec<(&String, &Any)> = entries.iter().collect();
|
|
207
|
+
pairs.sort_by(|a, b| a.0.cmp(b.0)); // deterministic key order
|
|
208
|
+
let mut out = String::from("{");
|
|
209
|
+
for (i, (key, value)) in pairs.into_iter().enumerate() {
|
|
210
|
+
if i > 0 {
|
|
211
|
+
out.push(',');
|
|
212
|
+
}
|
|
213
|
+
out.push_str(&scalar_json(&Any::String(Arc::from(key.as_str())))); // JSON-escaped key
|
|
214
|
+
out.push(':');
|
|
215
|
+
out.push_str(&any_to_json(value));
|
|
216
|
+
}
|
|
217
|
+
out.push('}');
|
|
218
|
+
out
|
|
219
|
+
}
|
|
220
|
+
scalar => scalar_json(scalar),
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
fn scalar_json(a: &Any) -> String {
|
|
190
225
|
let mut s = String::new();
|
|
191
226
|
a.to_json(&mut s);
|
|
192
227
|
s
|
|
@@ -307,6 +342,27 @@ mod tests {
|
|
|
307
342
|
assert_eq!(map_json(&txn, &map), r#"{"user":{"name":"Ada"}}"#);
|
|
308
343
|
}
|
|
309
344
|
|
|
345
|
+
#[test]
|
|
346
|
+
fn map_json_sorts_nested_object_keys() {
|
|
347
|
+
// Nested maps sort their keys too, not just the root: yrs stores map
|
|
348
|
+
// entries in a HashMap, so without this the inner object serializes in
|
|
349
|
+
// arbitrary, run-varying order.
|
|
350
|
+
let doc = Doc::new();
|
|
351
|
+
let map = doc.get_or_insert_map("state");
|
|
352
|
+
{
|
|
353
|
+
let mut txn = doc.transact_mut();
|
|
354
|
+
let inner = map.insert(&mut txn, "user", MapPrelim::default());
|
|
355
|
+
inner.insert(&mut txn, "name", "Ada");
|
|
356
|
+
inner.insert(&mut txn, "active", true);
|
|
357
|
+
inner.insert(&mut txn, "id", 7_i64);
|
|
358
|
+
}
|
|
359
|
+
let txn = doc.transact();
|
|
360
|
+
assert_eq!(
|
|
361
|
+
map_json(&txn, &map),
|
|
362
|
+
r#"{"user":{"active":true,"id":7,"name":"Ada"}}"#
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
310
366
|
#[test]
|
|
311
367
|
fn map_json_empty_is_object() {
|
|
312
368
|
let doc = Doc::new();
|
|
@@ -315,6 +371,61 @@ mod tests {
|
|
|
315
371
|
assert_eq!(map_json(&txn, &map), "{}");
|
|
316
372
|
}
|
|
317
373
|
|
|
374
|
+
#[test]
|
|
375
|
+
fn array_json_serializes_primitives_in_order() {
|
|
376
|
+
let doc = Doc::new();
|
|
377
|
+
let array = doc.get_or_insert_array("rows");
|
|
378
|
+
{
|
|
379
|
+
let mut txn = doc.transact_mut();
|
|
380
|
+
array.push_back(&mut txn, "a");
|
|
381
|
+
array.push_back(&mut txn, 2_i64);
|
|
382
|
+
array.push_back(&mut txn, true);
|
|
383
|
+
}
|
|
384
|
+
let txn = doc.transact();
|
|
385
|
+
assert_eq!(array_json(&txn, &array), r#"["a",2,true]"#);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
#[test]
|
|
389
|
+
fn array_json_recurses_into_nested_maps() {
|
|
390
|
+
// The kanban/spreadsheet shape: an array of Y.Maps. Each element must
|
|
391
|
+
// come through as its own object, keys sorted like map_json.
|
|
392
|
+
let doc = Doc::new();
|
|
393
|
+
let array = doc.get_or_insert_array("cards");
|
|
394
|
+
{
|
|
395
|
+
let mut txn = doc.transact_mut();
|
|
396
|
+
let card = array.push_back(&mut txn, MapPrelim::default());
|
|
397
|
+
card.insert(&mut txn, "text", "Ship it");
|
|
398
|
+
card.insert(&mut txn, "column", "done");
|
|
399
|
+
}
|
|
400
|
+
let txn = doc.transact();
|
|
401
|
+
assert_eq!(
|
|
402
|
+
array_json(&txn, &array),
|
|
403
|
+
r#"[{"column":"done","text":"Ship it"}]"#
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
#[test]
|
|
408
|
+
fn array_json_recurses_into_nested_arrays() {
|
|
409
|
+
let doc = Doc::new();
|
|
410
|
+
let array = doc.get_or_insert_array("grid");
|
|
411
|
+
{
|
|
412
|
+
let mut txn = doc.transact_mut();
|
|
413
|
+
let row = array.push_back(&mut txn, yrs::ArrayPrelim::default());
|
|
414
|
+
row.push_back(&mut txn, 1_i64);
|
|
415
|
+
row.push_back(&mut txn, 2_i64);
|
|
416
|
+
}
|
|
417
|
+
let txn = doc.transact();
|
|
418
|
+
assert_eq!(array_json(&txn, &array), r#"[[1,2]]"#);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#[test]
|
|
422
|
+
fn array_json_empty_is_array() {
|
|
423
|
+
let doc = Doc::new();
|
|
424
|
+
let array = doc.get_or_insert_array("rows");
|
|
425
|
+
let txn = doc.transact();
|
|
426
|
+
assert_eq!(array_json(&txn, &array), "[]");
|
|
427
|
+
}
|
|
428
|
+
|
|
318
429
|
#[test]
|
|
319
430
|
fn lexical_soft_line_break_and_tab_emit_their_characters() {
|
|
320
431
|
// A paragraph "foo⏎bar" (shift-enter): Lexical stores the LineBreakNode
|
|
@@ -365,8 +476,8 @@ mod tests {
|
|
|
365
476
|
#[test]
|
|
366
477
|
fn lexxy_full_schema_doc_extracts_attachment_text_too() {
|
|
367
478
|
// The full-schema capture (see lexical_html.rs): attachments must now
|
|
368
|
-
// contribute readable text
|
|
369
|
-
// upload's caption as its own line
|
|
479
|
+
// contribute readable text (a mention's plain text inline, an
|
|
480
|
+
// upload's caption as its own line) while the divider stays silent.
|
|
370
481
|
use yrs::updates::decoder::Decode;
|
|
371
482
|
use yrs::{Transact, Update};
|
|
372
483
|
let bytes = include_bytes!("../crates/lexical-html/src/fixtures/lexxy_full.bin");
|
|
@@ -5,15 +5,16 @@ require "generators/yrby/tables/tables_generator"
|
|
|
5
5
|
|
|
6
6
|
module Yrby
|
|
7
7
|
module Generators
|
|
8
|
-
# `bin/rails generate yrby:install`
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
8
|
+
# `bin/rails generate yrby:install` runs yrby:tables to create the storage
|
|
9
|
+
# migration. The models and Y::DocumentChannel ship in the gem, so that is
|
|
10
|
+
# the only file it generates by default. Pass --channel to also generate
|
|
11
|
+
# an application channel for custom authorization or room-keyed documents.
|
|
12
12
|
class InstallGenerator < ::Rails::Generators::Base
|
|
13
13
|
source_root File.expand_path("templates", __dir__)
|
|
14
|
+
class_option :channel, type: :boolean, default: false, desc: "Generate a custom DocumentChannel"
|
|
14
15
|
|
|
15
16
|
def create_channel
|
|
16
|
-
template "document_channel.rb", "app/channels/document_channel.rb"
|
|
17
|
+
template "document_channel.rb", "app/channels/document_channel.rb" if options[:channel]
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
def create_tables
|
|
@@ -25,15 +26,25 @@ module Yrby
|
|
|
25
26
|
|
|
26
27
|
Next steps:
|
|
27
28
|
|
|
28
|
-
1.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
3. Install the yrby-client npm package and connect an editor:
|
|
29
|
+
1. bin/rails db:migrate
|
|
30
|
+
2. Render a collaborative document on a page where the user may
|
|
31
|
+
edit the record:
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
<%= collaborative_document_tag @post, :body %>
|
|
34
|
+
|
|
35
|
+
3. Install the yrby-client npm package. The tag renders an
|
|
36
|
+
element that connects automatically. Once it syncs, your code
|
|
37
|
+
gets the document and can bind it to any Yjs editor:
|
|
38
|
+
|
|
39
|
+
import "yrby-client/element"
|
|
40
|
+
|
|
41
|
+
document.addEventListener("yrby:synced", ({ target, detail }) => {
|
|
42
|
+
const editor = bindYourEditor(target, detail.doc, detail.provider)
|
|
43
|
+
detail.signal.addEventListener("abort", () => editor.destroy(), { once: true })
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
Run with --channel to also generate app/channels/document_channel.rb,
|
|
47
|
+
and implement its authorized? method before you use it.
|
|
37
48
|
|
|
38
49
|
The README's Editors section links working integrations for
|
|
39
50
|
Tiptap, Lexxy, Rhino Editor, and CodeMirror.
|
|
@@ -16,11 +16,7 @@ class DocumentChannel < ApplicationCable::Channel
|
|
|
16
16
|
# and yrby-client retries it.
|
|
17
17
|
on_change { |key, update| Y::Document.append(key, update) }
|
|
18
18
|
|
|
19
|
-
def subscribed
|
|
20
|
-
return reject unless authorized?(params[:id])
|
|
21
|
-
|
|
22
|
-
sync_subscribed(params[:id])
|
|
23
|
-
end
|
|
19
|
+
def subscribed = sync_subscribed(params[:id])
|
|
24
20
|
|
|
25
21
|
def receive(data) = sync_receive(data, params[:id])
|
|
26
22
|
|
|
@@ -28,8 +24,9 @@ class DocumentChannel < ApplicationCable::Channel
|
|
|
28
24
|
|
|
29
25
|
# Everyone is denied until you fill this in. Wire it to your app's auth:
|
|
30
26
|
# identify current_user on the cable connection, then check they may read
|
|
31
|
-
# and write this document.
|
|
32
|
-
#
|
|
27
|
+
# and write this document. This runs once when the client subscribes, and
|
|
28
|
+
# the subscription stays authorized until it ends. Raising in on_change is
|
|
29
|
+
# for store failures, so don't use it for access control.
|
|
33
30
|
def authorized?(_document_key)
|
|
34
31
|
false
|
|
35
32
|
end
|
|
@@ -10,10 +10,13 @@ module Yrby
|
|
|
10
10
|
# yrby:install, and by other gems building on the same storage.
|
|
11
11
|
#
|
|
12
12
|
# Template notes (kept here, not in the emitted migration): state is
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
13
|
+
# 1.gigabyte - 1: the largest limit every adapter accepts. MySQL maps
|
|
14
|
+
# anything over 16 MB to longblob (a compacted snapshot is the whole
|
|
15
|
+
# document; a 16 MB cap would break compaction); Postgres raises above
|
|
16
|
+
# 1 GB - 1 and ignores the limit on bytea otherwise; SQLite ignores
|
|
17
|
+
# limits. Payload is 16.megabytes - 1 (one update can carry a big paste
|
|
18
|
+
# or a client's accumulated offline edits; the 64 KB default blob is
|
|
19
|
+
# too small).
|
|
17
20
|
# The partial unique index's WHERE only keeps
|
|
18
21
|
# key-only rows out of the index: uniqueness holds without it, since
|
|
19
22
|
# unique indexes treat NULLs as distinct on every supported database,
|
|
@@ -6,7 +6,7 @@ class CreateYTables < ActiveRecord::Migration<%= migration_version %>
|
|
|
6
6
|
t.string :key, null: false, index: { unique: true }
|
|
7
7
|
t.references :record, polymorphic: true, null: true, index: false
|
|
8
8
|
t.string :name
|
|
9
|
-
t.binary :state, limit:
|
|
9
|
+
t.binary :state, limit: 1.gigabyte - 1
|
|
10
10
|
t.timestamps
|
|
11
11
|
t.index %i[record_type record_id name], unique: true,
|
|
12
12
|
where: "record_type IS NOT NULL",
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Y
|
|
4
|
+
module Collaborative
|
|
5
|
+
# The collaborative document for one attribute of one record, such as a
|
|
6
|
+
# post's body. Ruby code and the shipped channel both use it, so they read
|
|
7
|
+
# and write the same rows.
|
|
8
|
+
class Attribute
|
|
9
|
+
attr_reader :record, :name
|
|
10
|
+
|
|
11
|
+
def initialize(record, name)
|
|
12
|
+
raise ArgumentError, "collaboration requires a persisted record" unless record.persisted?
|
|
13
|
+
|
|
14
|
+
@record = record
|
|
15
|
+
@name = name.to_s.dup.freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The document as update bytes, or nil before the first write.
|
|
19
|
+
def load_state = existing_row&.load_state
|
|
20
|
+
|
|
21
|
+
# Records one change and returns once it's saved. The channel waits for
|
|
22
|
+
# this to return before it acknowledges or broadcasts the change.
|
|
23
|
+
def append(update) = document_row.append(update)
|
|
24
|
+
|
|
25
|
+
# Builds a new Y::Doc from storage on every call.
|
|
26
|
+
def y_doc
|
|
27
|
+
Y::Doc.new.tap do |doc|
|
|
28
|
+
state = load_state
|
|
29
|
+
doc.apply_update(state) if state
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The key clients sync the document under, such as "post/1/body". If a
|
|
34
|
+
# key-only channel created the document before it was linked to a
|
|
35
|
+
# record, this returns that original key.
|
|
36
|
+
def key = existing_row&.key || Y::Document.key_for(record, name)
|
|
37
|
+
|
|
38
|
+
# Finds or creates the Y::Document or Y::EncryptedDocument row. Writes
|
|
39
|
+
# and maintenance work such as compaction go through it.
|
|
40
|
+
def document_row = document_class.for(record, name)
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
# Returns the row if it exists without creating one, so reading a
|
|
45
|
+
# document never adds a row. It selects only id and key because the
|
|
46
|
+
# row's load_state reads the snapshot itself, and loading the snapshot
|
|
47
|
+
# here too would fetch it twice.
|
|
48
|
+
def existing_row = document_class.select(:id, :key).find_by(record:, name:)
|
|
49
|
+
|
|
50
|
+
def document_class = record.class.collaborative_document_class(name)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Y
|
|
4
|
+
module Collaborative
|
|
5
|
+
# The view side of a collaborative document. The engine includes it into
|
|
6
|
+
# Action View.
|
|
7
|
+
#
|
|
8
|
+
# <%= collaborative_document_tag @post, :body %>
|
|
9
|
+
#
|
|
10
|
+
# renders the auto-connecting element:
|
|
11
|
+
#
|
|
12
|
+
# <yrby-document grant="<signed sgid>" name="body"></yrby-document>
|
|
13
|
+
#
|
|
14
|
+
# Importing "yrby-client/element" registers the element. It subscribes to
|
|
15
|
+
# Y::DocumentChannel by itself and exposes the synced Y.Doc through its
|
|
16
|
+
# `doc` property and `yrby:synced` event, much like the element behind
|
|
17
|
+
# turbo_stream_from.
|
|
18
|
+
#
|
|
19
|
+
# The grant is a signed GlobalID scoped to this record and attribute
|
|
20
|
+
# (record.collaborative_sgid(name)). Only render the tag on pages where
|
|
21
|
+
# the user may edit the record, because by default anyone holding the
|
|
22
|
+
# grant can open the document. To also check the user's current
|
|
23
|
+
# permissions when they subscribe, use Y::DocumentChannel.authorize_document.
|
|
24
|
+
#
|
|
25
|
+
# Extra options pass through to the element and a block becomes its
|
|
26
|
+
# content, so the element can wrap the mount point an editor binds to:
|
|
27
|
+
#
|
|
28
|
+
# <%= collaborative_document_tag @post, :body, id: "editor" %>
|
|
29
|
+
#
|
|
30
|
+
# expires_in: limits how long the grant lasts. refresh: is a URL the
|
|
31
|
+
# element fetches a new grant from when a subscription is rejected, so it
|
|
32
|
+
# can resubscribe without a page load. The app's action there re-runs its
|
|
33
|
+
# own authorization and renders { grant: record.collaborative_sgid(name) }:
|
|
34
|
+
#
|
|
35
|
+
# <%= collaborative_document_tag @post, :body, expires_in: 10.minutes,
|
|
36
|
+
# refresh: grant_post_path(@post) %>
|
|
37
|
+
module Helper
|
|
38
|
+
def collaborative_document_tag(record, name, expires_in: nil, refresh: nil, **, &)
|
|
39
|
+
grant = record.collaborative_sgid(name, **{ expires_in: expires_in }.compact)
|
|
40
|
+
attributes = { grant: grant, name: name }
|
|
41
|
+
attributes[:refresh] = refresh if refresh
|
|
42
|
+
# The helper's attributes go last because a later key wins in a keyword
|
|
43
|
+
# splat, so a template can't override the grant or the name.
|
|
44
|
+
tag.yrby_document(**, **attributes, &)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/concern"
|
|
4
|
+
require "global_id"
|
|
5
|
+
|
|
6
|
+
module Y
|
|
7
|
+
# The signed token that connects a page to a channel for record-backed
|
|
8
|
+
# collaborative documents.
|
|
9
|
+
#
|
|
10
|
+
# The client never picks its own document. The server signs a token for the
|
|
11
|
+
# record and attribute, the page renders it, and the channel looks the
|
|
12
|
+
# record up from it. The token is a signed GlobalID scoped to one attribute.
|
|
13
|
+
#
|
|
14
|
+
# # the view
|
|
15
|
+
# tag.div data: { grant: post.collaborative_sgid(:body) }
|
|
16
|
+
#
|
|
17
|
+
# # the channel
|
|
18
|
+
# def authorized?(_key)
|
|
19
|
+
# record.present? && record.editable_by?(current_user)
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# # Not memoized, because AnyCable gives each command a fresh channel
|
|
23
|
+
# # instance and a cached record would go stale. Y::DocumentChannel
|
|
24
|
+
# # does the same.
|
|
25
|
+
# def record
|
|
26
|
+
# Y::Collaborative.locate(params[:grant], :body)
|
|
27
|
+
# end
|
|
28
|
+
#
|
|
29
|
+
# The engine includes this into ActiveRecord::Base so any channel's
|
|
30
|
+
# authorized? can use it. lexxy-realtime uses the same token flow.
|
|
31
|
+
module Collaborative
|
|
32
|
+
extend ActiveSupport::Concern
|
|
33
|
+
|
|
34
|
+
class << self
|
|
35
|
+
# The signed-GlobalID purpose for one collaborative attribute. A token
|
|
36
|
+
# signed for one attribute verifies only against that attribute's
|
|
37
|
+
# purpose, so it can't locate the record through any other attribute.
|
|
38
|
+
# The purpose leaves out the channel name, so a custom channel and the
|
|
39
|
+
# shipped one can both resolve the same tokens for an attribute.
|
|
40
|
+
def sgid_purpose(name) = "yrby/#{name}"
|
|
41
|
+
|
|
42
|
+
# Looks up the record for a token from `collaborative_sgid(name)`.
|
|
43
|
+
# Returns nil for an invalid, tampered, expired, or wrong-attribute
|
|
44
|
+
# token, and for a record that no longer exists.
|
|
45
|
+
def locate(sgid, name)
|
|
46
|
+
GlobalID::Locator.locate_signed(sgid, for: sgid_purpose(name))
|
|
47
|
+
rescue ActiveRecord::RecordNotFound
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
included do
|
|
53
|
+
class_attribute :collaborative_document_options,
|
|
54
|
+
instance_accessor: false, default: {}.freeze
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class_methods do
|
|
58
|
+
# Declares how one attribute's document is stored. The shipped channel
|
|
59
|
+
# and Ruby reads both follow it, and undeclared attributes use plain
|
|
60
|
+
# Y::Document.
|
|
61
|
+
def has_collaborative_document(name, encrypted: false) # rubocop:disable Naming/PredicatePrefix
|
|
62
|
+
self.collaborative_document_options = collaborative_document_options.merge(
|
|
63
|
+
name.to_s => { encrypted: encrypted }.freeze
|
|
64
|
+
).freeze
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Returns the model that stores this attribute's document,
|
|
68
|
+
# Y::EncryptedDocument for an attribute declared encrypted and
|
|
69
|
+
# Y::Document for the rest. It resolves the constant on every call so
|
|
70
|
+
# that declaring an attribute doesn't load the engine's models while the
|
|
71
|
+
# app's models are still loading.
|
|
72
|
+
def collaborative_document_class(name)
|
|
73
|
+
encrypted = collaborative_document_options.dig(name.to_s, :encrypted)
|
|
74
|
+
encrypted ? Y::EncryptedDocument : Y::Document
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Returns the document for one attribute, stored the way the model
|
|
79
|
+
# declared. It responds to load_state, append, y_doc, and key.
|
|
80
|
+
def collaborative_document(name)
|
|
81
|
+
Attribute.new(self, name)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# A signed token that a channel can pass to Y::Collaborative.locate to get
|
|
85
|
+
# this record back, for this attribute only. Pass expires_in: to limit how
|
|
86
|
+
# long the grant lasts. Without it GlobalID's own default applies, which is
|
|
87
|
+
# one month under Rails. We pass the option through only when it's given,
|
|
88
|
+
# because an explicit nil would mean "never expire".
|
|
89
|
+
def collaborative_sgid(name, expires_in: nil)
|
|
90
|
+
options = { for: Y::Collaborative.sgid_purpose(name) }
|
|
91
|
+
options[:expires_in] = expires_in if expires_in
|
|
92
|
+
to_sgid(**options).to_s
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
require "y/collaborative/attribute"
|
|
98
|
+
require "y/collaborative/helper"
|
data/lib/y/lexxy.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Y
|
|
4
4
|
# The Lexxy renderer: Y::Lexical (core Lexical) plus the Lexxy-specific
|
|
5
|
-
# schema, applied beneath the app's rules
|
|
5
|
+
# schema, applied beneath the app's rules: an app rule for one of these
|
|
6
6
|
# types simply replaces it. This is the byte-parity class: the fixture
|
|
7
7
|
# tests hold `Y::Lexxy.new(doc).to_html` identical to a live editor's own
|
|
8
8
|
# serialized value.
|
|
@@ -36,14 +36,14 @@ module Y
|
|
|
36
36
|
%(<th class="lexxy-content__table-cell--header" #{style}>#{node.content}</th>)
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
# Attribute order follows Lexxy's export
|
|
39
|
+
# Attribute order follows Lexxy's export: checked items put aria-checked
|
|
40
40
|
# before value; items holding a nested list append the
|
|
41
41
|
# lexxy-nested-listitem class after value.
|
|
42
42
|
def self.list_item(node)
|
|
43
43
|
out = +"<li"
|
|
44
44
|
checked = node.attrs["__checked"]
|
|
45
|
-
out << %( aria-checked="#{checked}") unless checked.nil?
|
|
46
|
-
out << %( value="#{node.attrs["__value"] || 1}")
|
|
45
|
+
out << %( aria-checked="#{RenderRules.escape_attr(checked)}") unless checked.nil?
|
|
46
|
+
out << %( value="#{RenderRules.escape_attr(node.attrs["__value"] || 1)}")
|
|
47
47
|
out << %( class="lexxy-nested-listitem") if node.child_types.include?("list")
|
|
48
48
|
"#{out}>#{node.content}</li>"
|
|
49
49
|
end
|