Live collaboration with Yjs over ActionCable

All editors now work on a server document at the same time instead of
taking turns behind an edit lock.

- The document's content is a sequence of Yjs updates (document_updates);
  DocumentChannel stores and relays them immediately, checks access on
  subscribe and again whenever links or memberships change.
- app/frontend/collab/: the store stays position-based; a binding maps it
  to a Yjs form with stable row/column ids, so a row inserted by one
  person doesn't misplace what another is typing. Remote changes are
  applied only after pending local ones went into Yjs.
- Per-person undo (Y.UndoManager), presence via awareness: the others'
  selections as coloured boxes with names, avatars in the toolbar.
- Existing JSON documents convert on first open, deterministically, so
  two people opening one at once end up with the same Yjs state.
- Editors compact the stored updates after 200 and keep a JSON snapshot
  current for export and duplicating. Edit lock and polling are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
wkh
2026-09-28 20:41:02 +02:00
co-authored by Claude Opus 5.5
parent e14bada114
commit 7fce972fdc
27 changed files with 1685 additions and 413 deletions
+15 -26
View File
@@ -1,6 +1,5 @@
class Document < ApplicationRecord
ROLES = %w[viewer editor owner].freeze
LOCK_TTL = 90.seconds
MAX_CONTENT_BYTES = 20.megabytes
belongs_to :owner, class_name: "User"
@@ -8,6 +7,7 @@ class Document < ApplicationRecord
has_many :document_memberships, dependent: :delete_all
has_many :members, through: :document_memberships, source: :user
has_many :share_links, dependent: :delete_all
has_many :updates, class_name: "DocumentUpdate", dependent: :delete_all
validates :title, presence: true, length: { maximum: 200 }
validates :content, presence: true
@@ -36,39 +36,28 @@ class Document < ApplicationRecord
role.present? && ROLES.index(role.to_s) >= ROLES.index(minimum.to_s)
end
# ---- edit lock --------------------------------------------------------
def locked_by_other?(holder)
lock_holder.present? && lock_holder != holder && lock_expires_at&.future?
end
# Take or refresh the lock for `holder`. Returns true when `holder` has it.
def acquire_lock!(holder, name)
with_lock do
return false if locked_by_other?(holder)
update_columns(lock_holder: holder, lock_holder_name: name, lock_expires_at: LOCK_TTL.from_now)
true
end
end
def release_lock!(holder)
with_lock do
update_columns(lock_holder: nil, lock_holder_name: nil, lock_expires_at: nil) if lock_holder == holder
end
end
def lock_info(holder)
{ locked_by_other: locked_by_other?(holder), holder_name: (lock_holder_name if locked_by_other?(holder)) }
end
def meta_json(role:)
{
id:, title:, version:, role:,
id:, title:, role:,
owner_name: owner.name,
updated_at: content_updated_at.iso8601(3),
updated_by_name: updated_by&.name
}
end
# Tell everyone connected to this document (see DocumentChannel).
def broadcast(message) = DocumentChannel.broadcast_to(self, message)
# Access changed: every connection checks its role again.
def revalidate_access! = broadcast({ type: "revalidate" })
def self.valid_snapshot?(json)
parsed = JSON.parse(json)
parsed.is_a?(Hash) && parsed["sheets"].is_a?(Array)
rescue JSON::ParserError, TypeError
false
end
private
def content_is_document_json
+13
View File
@@ -0,0 +1,13 @@
# One Yjs update of a document -- an edit by someone, or a compacted state
# that replaces many earlier updates. Order doesn't matter to Yjs.
class DocumentUpdate < ApplicationRecord
MAX_BYTES = 10.megabytes
belongs_to :document
belongs_to :user, optional: true
validates :data, presence: true
validate { errors.add(:data, "ist zu groß") if data && data.bytesize > MAX_BYTES }
def as_message = { id:, data: Base64.strict_encode64(data) }
end