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
-7
View File
@@ -17,12 +17,5 @@ module Api
# a share link token, for visitors (with or without account) who got the link
def share_token = request.headers["X-Share-Token"].presence
# identifies the lock holder: one browser tab
def client_id = request.headers["X-Client-Id"].to_s[0, 64].presence || "anon"
def lock_holder = "#{Current.user&.id || 'anon'}:#{client_id}"
def holder_name = Current.user&.name || "Jemand mit dem Link"
end
end
@@ -1,29 +0,0 @@
module Api
# The edit lock of a server document: one browser tab edits at a time,
# everyone else sees the document read-only until the lock is released or
# expires (Document::LOCK_TTL without a heartbeat).
class DocumentLocksController < BaseController
before_action :load_document
# POST /api/documents/:id/lock -- take or refresh (heartbeat)
def create
return render(json: { lock: { mine: false, holder_name: nil } }) unless Document.role_at_least?(@role, :editor)
mine = @doc.acquire_lock!(lock_holder, holder_name)
render json: { lock: { mine:, holder_name: (@doc.lock_holder_name unless mine) } }
end
# DELETE /api/documents/:id/lock
def destroy
@doc.release_lock!(lock_holder)
head :no_content
end
private
def load_document
@doc = Document.find(params[:document_id])
@role = @doc.role_for(user: Current.user, token: share_token)
raise ActiveRecord::RecordNotFound unless @role
end
end
end
+11 -20
View File
@@ -23,32 +23,21 @@ module Api
render json: { document: doc.meta_json(role: :owner) }, status: :created
end
# GET /api/documents/:id[?known_version=n]
# With known_version: just the metadata when nothing changed since.
# GET /api/documents/:id[?content=0]
# The content is the JSON snapshot the editors keep current (the live
# content comes through DocumentChannel).
def show
touch_membership
meta = @doc.meta_json(role: @role).merge(lock: @doc.lock_info(lock_holder))
if params[:known_version].present? && params[:known_version].to_i == @doc.version
render json: { document: meta, unchanged: true }
else
render json: { document: meta.merge(content: @doc.content) }
end
meta = @doc.meta_json(role: @role)
meta[:content] = @doc.content unless params[:content] == "0"
render json: { document: meta }
end
# PATCH /api/documents/:id { title } and/or { content, base_version }
# PATCH /api/documents/:id { title }
def update
return forbidden unless Document.role_at_least?(@role, :editor)
if params.key?(:content)
return render(json: { error: "locked", holder_name: @doc.lock_holder_name }, status: :locked) if @doc.locked_by_other?(lock_holder)
@doc.with_lock do
if params.require(:base_version).to_i != @doc.version
return render(json: { error: "conflict", version: @doc.version }, status: :conflict)
end
@doc.update!(content: content_param, version: @doc.version + 1,
content_updated_at: Time.current, updated_by: Current.user)
end
end
@doc.update!(title: params[:title]) if params[:title].present?
@doc.update!(title: params.require(:title))
@doc.broadcast({ type: "meta", title: @doc.title })
render json: { document: @doc.meta_json(role: @role) }
end
@@ -56,8 +45,10 @@ module Api
def destroy
if @role == :owner
@doc.destroy!
@doc.broadcast({ type: "revoked" })
elsif Current.user
@doc.document_memberships.where(user: Current.user).delete_all
@doc.revalidate_access!
else
return forbidden
end
+4
View File
@@ -18,12 +18,14 @@ module Api
@doc.share_links.active.where(role:).find_each(&:revoke!)
@doc.share_links.create!(role:, created_by: Current.user)
end
@doc.revalidate_access!
render json: share_json
end
# DELETE /api/documents/:document_id/share/links/:id
def revoke_link
@doc.share_links.active.find(params[:id]).revoke!
@doc.revalidate_access!
render json: share_json
end
@@ -44,12 +46,14 @@ module Api
def update_member
m = @doc.document_memberships.find_by!(user_id: params[:user_id])
m.update!(role: params.require(:role))
@doc.revalidate_access!
render json: share_json
end
# DELETE /api/documents/:document_id/share/members/:user_id
def remove_member
@doc.document_memberships.where(user_id: params[:user_id]).delete_all
@doc.revalidate_access!
render json: share_json
end