Skip to content

Refactor to use prosemirror-adapter-react for attribute menu #1147

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kalilsn
Copy link
Member

@kalilsn kalilsn commented Apr 7, 2025

Not for merging, just putting this here in case it's useful for @allisonking

view.current?.dispatch(tr);
}

setTimeout(() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding this fixes a flushSync error. i don't really know why, and avoiding that kind of nonsense does seem like a strong pro of using the other view library

panelPosition: PanelProps;
viewRef: React.RefObject<EditorView | null>;
}
export function AttributePanel() {
Copy link
Member Author

@kalilsn kalilsn Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i made smaller changes to this component than were probably necessary:

  • got rid of its local state and used the parent's (maybe a mistake?). ideally it wouldn't depend on the node in the parent component state being up to date at all and could instead use prosemirror as the source of truth (perhaps just from the selection, or maybe directly in the plugin state). it should probably also submit all of its values at once instead of using these onchange handlers on individual inputs.
  • moved it inline in the doc (also maybe a mistake)
  • conditionally render the special link menu (not really functional, i wouldn't worry about it)
  • updated some styles/moved a few to tailwind classes

Comment on lines +27 to +34
view.dispatch(
view.state.tr.setSelection(
new TextSelection(
view.state.doc.resolve(pos)
// view.state.doc.resolve(getPos() + node.nodeSize)
)
)
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was from an attempt to make the attribute panel work based on the selection, rather than manually setting the node in the panelProps. it's not really working at the moment so you'll probably want to remove this

buttonText = `${node.type.name}${node.type.name === "heading" ? ` ${node.attrs.level}` : ""}`;
}

return (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where I would make the buttons for lists/any nested blocks more readable. Perhaps we could group them by depth? Or make them show on some kind of inline hover when they're not at the top level of the document. The way we handle this in legacy for suggested edits may be helpful to look at, if that's something you're tackling here.

},
props: {
decorations(state) {
// return this.getState(state)?.decorations;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uncommenting this should use our state logic to only rerender decorations in parts of the doc that change instead of crawling the whole doc each time, as recommended by marijn. But even without that, memoization makes this fast enough to work for now.

Comment on lines +177 to +179
stepMap.forEach((oldStart, oldEnd, newStart, newEnd) => {
mappedDecorations = mappedDecorations.remove(mappedDecorations.find(newStart, newEnd));
editorState.doc.nodesBetween(newStart, newEnd, (node, pos, parent, index) => {
Copy link
Member Author

@kalilsn kalilsn Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested this, so if you do use it, I'd make sure this is using the right combo of old/new positions and the respective editorstate vs previouseditorstate. Also there's a chance these are just the totally wrong functions to use 😓

Comment on lines +71 to +96
// handleClick: (view, pos, event) => {
// const $pos = view.state.doc.resolve(pos);

// const mark = $pos.marks()[0];

// const range = getMarkRange($pos, mark.type);

// const node = $pos.parent;

// const isLink = Boolean(
// baseSchema.marks.link.isInSet([
// ...(view.state.storedMarks || []),
// ...($pos.marks() || []),
// ])
// );
// console.log({ pos, node, event });
// setPanelPosition({
// ...panelPosition,
// isOpen: !panelPosition.isOpen,
// isLink,
// pos: range?.from || pos,
// node,
// });

// return true;
// },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using this in an attempt to make clicking into any mark open its menu, while also allowing the cursor to stay inside the editor, rather than having to click the tiny button and unfocusing the editor. But since the attribute panel depends on the node including the entire text in the mark and the pos pointing to the beginning of the mark, this needs some tweaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant