Skip to content

Commit

Permalink
Fix off by one issue with attachments and paste issues (#256)
Browse files Browse the repository at this point in the history
* fix off by one issue with attachments

* prettier

* prettier

* prettier

* prettier

* prettier

* prettier

* prettier

* add changesetS
  • Loading branch information
KonnorRogers authored Feb 5, 2025
1 parent ebf95cd commit 7e3e790
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-pears-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

Fix issues with paste extensions
5 changes: 5 additions & 0 deletions .changeset/new-donkeys-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rhino-editor": patch
---

Fix issues with attachment pasting
21 changes: 20 additions & 1 deletion src/exports/extensions/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,26 @@ function handleAttachment(
);
}

tr.replaceWith(currSelection.from - 1, currSelection.to, [
// The following checks fix some "off by 1" issues. I _think_ these are the only nodes we need to check. May need a more robust check if this continues to be an issue.
// https://github.com/KonnorRogers/rhino-editor/issues/254
let from = currSelection.from;
const prevNode = state.doc.resolve(from - 1);
const parentNode = prevNode.parent;

if (parentNode && parentNode.type.name === "doc") {
from -= 1;
} else {
const closestParagraph = findParentNodeOfTypeClosestToPos(
prevNode,
schema.nodes["paragraph"],
);

if (closestParagraph && closestParagraph.node.textContent === "") {
from -= 1;
}
}

tr.replaceWith(from, currSelection.to, [
...attachmentNodes,
schema.nodes.paragraph.create(),
]);
Expand Down
15 changes: 13 additions & 2 deletions src/exports/extensions/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@ export interface PasteOptions {}

// Super simple plugin that dispatches a paste event. This is convenient way to make this hard to override.
export function Paste() {
const handledEvents = new WeakMap();

return new Plugin({
key: new PluginKey("rhino-paste-event"),
props: {
handlePaste(view, event) {
const { clipboardData } = event;

// We always return false to allow other extensions to actually handle the paste via props.

if (event.defaultPrevented) {
return true;
return false;
}

if (handledEvents.has(event)) {
// This event has already processed. This prevents emitting the event twice.
return false;
}

handledEvents.set(event, null);

const rhinoPasteEvent = new RhinoPasteEvent(clipboardData);
view.dom.dispatchEvent(rhinoPasteEvent);

return true;
return false;

// @TODO: Future enhancements for pasting
// https://github.com/basecamp/trix/blob/fda14c5ae88a0821cf8999a53dcb3572b4172cf0/src/trix/controllers/level_0_input_controller.js#L39
Expand Down

0 comments on commit 7e3e790

Please sign in to comment.