@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
56 lines (53 loc) • 1.5 kB
JavaScript
import { Editor, Range } from 'slate';
import { getCharacterBefore, getCharacterAfter } from './get-character.js';
const defaultMarks = {
bold: false,
italic: false,
strikethrough: false,
code: false
};
function isMarkActive(editor, mark) {
const marks = Editor.marks(editor);
return marks ? marks[mark] === true : false;
}
function getMarks(editor) {
if (!editor) {
return { ...defaultMarks };
}
const marks = Editor.marks(editor);
return { ...defaultMarks, ...marks };
}
function filterActiveMarks(value) {
return Object.keys(value ?? {}).filter(
(key) => key !== "text"
);
}
function toggleMark(editor, mark) {
const isActive = isMarkActive(editor, mark);
if (isActive) {
Editor.removeMark(editor, mark);
} else {
Editor.addMark(editor, mark, true);
}
}
function removeMarks(editor) {
const marks = Editor.marks(editor);
if (marks) {
for (const mark in marks) {
Editor.removeMark(editor, mark);
}
}
}
function leaveMarkEdge(editor, edge) {
if (editor.selection && Range.isCollapsed(editor.selection)) {
const marks = Object.keys(Editor.marks(editor) ?? {});
if (marks.length > 0) {
const sibling = edge === "start" ? getCharacterBefore(editor, editor.selection) : getCharacterAfter(editor, editor.selection);
if (!sibling) {
removeMarks(editor);
}
}
}
}
export { filterActiveMarks, getMarks, isMarkActive, leaveMarkEdge, removeMarks, toggleMark };
//# sourceMappingURL=marks.js.map