-
Notifications
You must be signed in to change notification settings - Fork 90
History merging #932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zoltanhosszu
wants to merge
10
commits into
main
Choose a base branch
from
history-merging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
History merging #932
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
341d93e
Image upload history merging
zoltanhosszu cbbcfe5
Node selection doesn't update history
zoltanhosszu cbc9125
PR feedback
zoltanhosszu fdf4f12
Test updates
zoltanhosszu 879e92a
No history tracking for ProvisionalParagraph
zoltanhosszu d3f6375
Update action_text_attachment_upload_node.js
zoltanhosszu 1b3e2a8
Redo stack clearing
zoltanhosszu 230cb30
Fix flaky gallery test
zoltanhosszu 7d4c20d
Simplify code
zoltanhosszu 8142f44
Update attachments.test.js
zoltanhosszu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| import { $getSelection, $isRangeSelection, $isRootOrShadowRoot, SKIP_DOM_SELECTION_TAG } from "lexical" | ||
| import { | ||
| $createParagraphNode, $getNodeByKey, $getRoot, $getSelection, $isRangeSelection, $isRootOrShadowRoot, HISTORIC_TAG, HISTORY_PUSH_TAG, SKIP_DOM_SELECTION_TAG, | ||
| SKIP_SCROLL_INTO_VIEW_TAG | ||
| } from "lexical" | ||
| import Lexxy from "../config/lexxy" | ||
| import { SILENT_UPDATE_TAGS } from "../helpers/lexical_helper" | ||
| import { ActionTextAttachmentNode } from "./action_text_attachment_node" | ||
| import { $isProvisionalParagraphNode } from "./provisional_paragraph_node" | ||
| import { createElement, dispatch } from "../helpers/html_helper" | ||
| import { loadFileIntoImage } from "../helpers/upload_helper" | ||
| import { bytesToHumanSize } from "../helpers/storage_helper" | ||
|
|
||
| export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | ||
| static #activeUploads = new WeakSet() | ||
|
|
||
| static getType() { | ||
| return "action_text_attachment_upload" | ||
| } | ||
|
|
@@ -135,7 +139,7 @@ export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | |
| const writable = this.getWritable() | ||
| writable.width = width | ||
| writable.height = height | ||
| }, { tag: this.#backgroundUpdateTags }) | ||
| }, { tag: this.#transientUpdateTags }) | ||
| } | ||
|
|
||
| get #hasDimensions() { | ||
|
|
@@ -145,7 +149,10 @@ export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | |
| async #startUploadIfNeeded() { | ||
| if (this.#uploadStarted) return | ||
| if (!this.uploadUrl) return // Bridge-managed upload — skip DirectUpload | ||
| if (ActionTextAttachmentUploadNode.#activeUploads.has(this.file)) return | ||
|
|
||
| ActionTextAttachmentUploadNode.#activeUploads.add(this.file) | ||
| const uploadNodeKey = this.getKey() | ||
| this.#setUploadStarted() | ||
|
|
||
| const { DirectUpload } = await import("@rails/activestorage") | ||
|
|
@@ -162,12 +169,79 @@ export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | |
| } else { | ||
| this.#dispatchEvent("lexxy:upload-end", { file: this.file, error: null }) | ||
| this.editor.update(() => { | ||
| this.showUploadedAttachment(blob) | ||
| }, { tag: this.#backgroundUpdateTags }) | ||
| const uploadNode = $getNodeByKey(uploadNodeKey) | ||
| if (!(uploadNode instanceof ActionTextAttachmentUploadNode)) return | ||
|
|
||
| uploadNode.showUploadedAttachment(blob) | ||
| }, { | ||
| tag: this.#backgroundUpdateTags, | ||
| onUpdate: () => requestAnimationFrame(() => this.#collapseUploadHistory(uploadNodeKey)) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Upload completion uses HISTORY_PUSH_TAG so Lexical creates the undo boundary. | ||
| // Then we rewrite any upload-node snapshots to their clean (node-stripped) form | ||
| // and collapse duplicates to avoid no-op undo steps. | ||
| #collapseUploadHistory(uploadNodeKey) { | ||
| const historyState = this.#historyState | ||
| if (!historyState) return | ||
|
|
||
| const currentSignature = historyState.current && this.#contentSignatureFor(historyState.current.editorState) | ||
| const rewrittenStack = [] | ||
| let prevSignature = null | ||
|
|
||
| for (const entry of historyState.undoStack) { | ||
| const hasUploadNode = entry.editorState.read(() => | ||
| $getNodeByKey(uploadNodeKey) instanceof ActionTextAttachmentUploadNode | ||
| ) | ||
|
|
||
| const editorState = hasUploadNode | ||
| ? this.#editorStateStrippingUploadNodes(entry.editorState) | ||
| : entry.editorState | ||
|
|
||
| const signature = this.#contentSignatureFor(editorState) | ||
| if (signature !== prevSignature && signature !== currentSignature) { | ||
| rewrittenStack.push(hasUploadNode ? { editor: entry.editor, editorState } : entry) | ||
| } | ||
| prevSignature = signature | ||
| } | ||
|
|
||
| historyState.undoStack = rewrittenStack | ||
| historyState.redoStack = [] | ||
|
|
||
| // Force listeners (toolbar undo/redo button states) to observe the stack rewrite. | ||
| this.editor.update(() => {}, { tag: HISTORIC_TAG }) | ||
| } | ||
|
|
||
| // Upload nodes can't survive a JSON round-trip (File isn't serializable), | ||
| // so strip them from the serialized state before parseEditorState calls importJSON. | ||
| #editorStateStrippingUploadNodes(editorState) { | ||
| const json = editorState.toJSON() | ||
| this.#stripNodesFromJSON(json.root, n => n.type === "action_text_attachment_upload") | ||
| return this.editor.parseEditorState(json, () => { | ||
| if ($getRoot().getChildrenSize() === 0) $getRoot().append($createParagraphNode()) | ||
| }) | ||
| } | ||
|
|
||
| #stripNodesFromJSON(node, predicate) { | ||
| if (!node.children) return | ||
| node.children = node.children.filter(child => { | ||
| if (predicate(child)) return false | ||
| this.#stripNodesFromJSON(child, predicate) | ||
| return true | ||
| }) | ||
| } | ||
|
|
||
| #contentSignatureFor(editorState) { | ||
| return JSON.stringify(editorState.toJSON().root) | ||
| } | ||
|
|
||
| get #historyState() { | ||
| return this.editor.getRootElement()?.closest("lexxy-editor")?.historyState | ||
| } | ||
|
|
||
| #createUploadDelegate() { | ||
| const shouldAuthenticateUploads = Lexxy.global.get("authenticatedUploads") | ||
|
|
||
|
|
@@ -197,14 +271,14 @@ export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | |
| #setProgress(progress) { | ||
| this.editor.update(() => { | ||
| this.getWritable().progress = progress | ||
| }, { tag: this.#backgroundUpdateTags }) | ||
| }, { tag: this.#transientUpdateTags }) | ||
| } | ||
|
|
||
| #handleUploadError(error) { | ||
| console.warn(`Upload error for ${this.file?.name ?? "file"}: ${error}`) | ||
| this.editor.update(() => { | ||
| this.getWritable().uploadError = true | ||
| }, { tag: this.#backgroundUpdateTags }) | ||
| }, { tag: this.#transientUpdateTags }) | ||
| } | ||
|
|
||
| showUploadedAttachment(blob) { | ||
|
|
@@ -221,15 +295,24 @@ export class ActionTextAttachmentUploadNode extends ActionTextAttachmentNode { | |
| return replacementNode.getKey() | ||
| } | ||
|
|
||
| // Upload lifecycle methods (progress, completion, errors) run asynchronously and may | ||
| // fire while the user is focused on another element (e.g., a title field). Without | ||
| // SKIP_DOM_SELECTION_TAG, Lexical's reconciler would move the DOM selection back into | ||
| // the editor, stealing focus from wherever the user is currently typing. | ||
| // Transient updates (progress, dimensions, errors) are completely invisible to | ||
| // the undo history via HISTORIC_TAG. | ||
| get #transientUpdateTags() { | ||
| if (this.#editorHasFocus) { | ||
| return [ HISTORIC_TAG ] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else { | ||
| return [ HISTORIC_TAG, SKIP_DOM_SELECTION_TAG ] | ||
| } | ||
| } | ||
|
|
||
| // Use HISTORY_PUSH_TAG to force a stable undo boundary at upload completion. | ||
| // SKIP_SCROLL_INTO_VIEW_TAG avoids scroll jumps, and SKIP_DOM_SELECTION_TAG | ||
| // prevents focus theft when the editor is not active. | ||
| get #backgroundUpdateTags() { | ||
| if (this.#editorHasFocus) { | ||
| return SILENT_UPDATE_TAGS | ||
| return [ HISTORY_PUSH_TAG, SKIP_SCROLL_INTO_VIEW_TAG ] | ||
| } else { | ||
| return [ ...SILENT_UPDATE_TAGS, SKIP_DOM_SELECTION_TAG ] | ||
| return [ HISTORY_PUSH_TAG, SKIP_SCROLL_INTO_VIEW_TAG, SKIP_DOM_SELECTION_TAG ] | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would force a render as it's called within a command handler. The better move is unwrapping since the caller will need to call
$getSelectionand be in a read/update context