Files
outline/shared/editor/commands/comment.ts
T
Tom Moor fa8d82d82a fix: Restore uuid package on frontend (#10491)
* fix: Restore uuid package on frontend

* Remove legacy moduleNameMapper

* Add lint rule

* lint - getRandomValues can be used without SSL

* Update Comment.ts
2025-10-28 08:13:48 -04:00

67 lines
1.7 KiB
TypeScript

import { Attrs } from "prosemirror-model";
import { Command, NodeSelection, TextSelection } from "prosemirror-state";
import { v4 as uuidv4 } from "uuid";
import { isMarkActive } from "../queries/isMarkActive";
import { chainTransactions } from "../lib/chainTransactions";
import { addMark } from "./addMark";
import { collapseSelection } from "./collapseSelection";
import { chainCommands } from "prosemirror-commands";
export const addComment = (attrs: Attrs): Command =>
chainCommands(addCommentTextSelection(attrs), addCommentNodeSelection(attrs));
const addCommentNodeSelection =
(attrs: Attrs): Command =>
(state, dispatch) => {
if (!(state.selection instanceof NodeSelection)) {
return false;
}
const { selection } = state;
const existingMarks = selection.node.attrs.marks ?? [];
const newMark = {
type: "comment",
attrs: {
id: uuidv4(),
userId: attrs.userId,
draft: true,
},
};
const newAttrs = {
...selection.node.attrs,
marks: [...existingMarks, newMark],
};
dispatch?.(state.tr.setNodeMarkup(selection.from, undefined, newAttrs));
return true;
};
const addCommentTextSelection =
(attrs: Attrs): Command =>
(state, dispatch) => {
if (!(state.selection instanceof TextSelection)) {
return false;
}
if (
isMarkActive(
state.schema.marks.comment,
{
resolved: false,
},
{ exact: true }
)(state)
) {
return false;
}
chainTransactions(
addMark(state.schema.marks.comment, {
id: uuidv4(),
userId: attrs.userId,
draft: true,
}),
collapseSelection()
)(state, dispatch);
return true;
};