@eightyfourthousand/lib-editing 2026.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +12 -0
- package/.eslintrc.json +18 -0
- package/README.md +7 -0
- package/jest.config.ts +10 -0
- package/package.json +35 -0
- package/postcss.config.mjs +9 -0
- package/project.json +29 -0
- package/src/fixtures/basic/json.ts +396 -0
- package/src/fixtures/toh251/json.ts +4913 -0
- package/src/fixtures/toh251/passages.ts +2814 -0
- package/src/fixtures/types.ts +8 -0
- package/src/index.ts +4 -0
- package/src/lib/block.ts +226 -0
- package/src/lib/components/editor/BlockEditor.tsx +38 -0
- package/src/lib/components/editor/EditorBackMatterPage.tsx +95 -0
- package/src/lib/components/editor/EditorBodyPage.tsx +87 -0
- package/src/lib/components/editor/EditorHeader.tsx +22 -0
- package/src/lib/components/editor/EditorLayout.tsx +62 -0
- package/src/lib/components/editor/EditorLeftPanelPage.tsx +27 -0
- package/src/lib/components/editor/EditorProvider.tsx +399 -0
- package/src/lib/components/editor/PaginationProvider.tsx +472 -0
- package/src/lib/components/editor/TitlesBuilder.tsx +40 -0
- package/src/lib/components/editor/TranslationBuilder.tsx +171 -0
- package/src/lib/components/editor/TranslationEditor.tsx +32 -0
- package/src/lib/components/editor/extensions/Abbreviation/Abbreviation.ts +133 -0
- package/src/lib/components/editor/extensions/Audio/Audio.ts +69 -0
- package/src/lib/components/editor/extensions/Bold.ts +43 -0
- package/src/lib/components/editor/extensions/Document.ts +8 -0
- package/src/lib/components/editor/extensions/DragHandle/DragHandle.ts +429 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLink.tsx +39 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkHoverContent.tsx +139 -0
- package/src/lib/components/editor/extensions/EndNoteLink/EndNoteLinkMark.ts +236 -0
- package/src/lib/components/editor/extensions/EndNoteLink/endnote-utils.ts +412 -0
- package/src/lib/components/editor/extensions/GlobalConfig.ts +52 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInput.tsx +54 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstance.tsx +129 -0
- package/src/lib/components/editor/extensions/GlossaryInstance/GlossaryInstanceNode.ts +148 -0
- package/src/lib/components/editor/extensions/Heading/Heading.ts +71 -0
- package/src/lib/components/editor/extensions/HoverInputField.tsx +54 -0
- package/src/lib/components/editor/extensions/Image.ts +18 -0
- package/src/lib/components/editor/extensions/Indent.ts +103 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLink.ts +173 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLinkHoverContent.tsx +137 -0
- package/src/lib/components/editor/extensions/InternalLink/InternalLinkInput.tsx +71 -0
- package/src/lib/components/editor/extensions/InternalLink/index.ts +1 -0
- package/src/lib/components/editor/extensions/Italic.ts +50 -0
- package/src/lib/components/editor/extensions/LeadingSpace.ts +106 -0
- package/src/lib/components/editor/extensions/Line/LineNode.ts +41 -0
- package/src/lib/components/editor/extensions/LineGroup/LineGroupNode.ts +124 -0
- package/src/lib/components/editor/extensions/Link/Link.ts +65 -0
- package/src/lib/components/editor/extensions/Link/LinkHoverContent.tsx +124 -0
- package/src/lib/components/editor/extensions/Link/index.ts +1 -0
- package/src/lib/components/editor/extensions/List.ts +74 -0
- package/src/lib/components/editor/extensions/Mantra/Mantra.ts +88 -0
- package/src/lib/components/editor/extensions/Mention/Mention.ts +184 -0
- package/src/lib/components/editor/extensions/Mention/MentionHoverContent.tsx +158 -0
- package/src/lib/components/editor/extensions/NodeWrapper.tsx +57 -0
- package/src/lib/components/editor/extensions/Paragraph/Paragraph.ts +25 -0
- package/src/lib/components/editor/extensions/ParagraphIndent.ts +87 -0
- package/src/lib/components/editor/extensions/Passage/EditLabel.tsx +57 -0
- package/src/lib/components/editor/extensions/Passage/EditorOptions.tsx +29 -0
- package/src/lib/components/editor/extensions/Passage/Passage.tsx +238 -0
- package/src/lib/components/editor/extensions/Passage/PassageNode.ts +223 -0
- package/src/lib/components/editor/extensions/Passage/ReaderOptions.tsx +55 -0
- package/src/lib/components/editor/extensions/Passage/ShowAnnotations.tsx +92 -0
- package/src/lib/components/editor/extensions/Passage/index.ts +1 -0
- package/src/lib/components/editor/extensions/Passage/label.spec.ts +118 -0
- package/src/lib/components/editor/extensions/Passage/label.ts +39 -0
- package/src/lib/components/editor/extensions/Placeholder.ts +9 -0
- package/src/lib/components/editor/extensions/SlashCommand/SlashCommand.ts +65 -0
- package/src/lib/components/editor/extensions/SlashCommand/SuggestionList.tsx +109 -0
- package/src/lib/components/editor/extensions/SlashCommand/Suggestions.ts +185 -0
- package/src/lib/components/editor/extensions/SmallCaps.ts +110 -0
- package/src/lib/components/editor/extensions/StarterKit.ts +36 -0
- package/src/lib/components/editor/extensions/Subscript.ts +43 -0
- package/src/lib/components/editor/extensions/Superscript.ts +43 -0
- package/src/lib/components/editor/extensions/Table.ts +32 -0
- package/src/lib/components/editor/extensions/TextAlign.ts +5 -0
- package/src/lib/components/editor/extensions/TitleMetadata.ts +40 -0
- package/src/lib/components/editor/extensions/TitleNode.ts +133 -0
- package/src/lib/components/editor/extensions/TitlesNode.ts +102 -0
- package/src/lib/components/editor/extensions/Trailer.ts +57 -0
- package/src/lib/components/editor/extensions/TranslationDocument.ts +7 -0
- package/src/lib/components/editor/extensions/TranslationMetadata.ts +58 -0
- package/src/lib/components/editor/extensions/Underline.ts +43 -0
- package/src/lib/components/editor/hooks/index.ts +4 -0
- package/src/lib/components/editor/hooks/useBlockEditor.ts +53 -0
- package/src/lib/components/editor/hooks/useDefaultExtensions.ts +39 -0
- package/src/lib/components/editor/hooks/useDirtyStore.ts +33 -0
- package/src/lib/components/editor/hooks/useTranslationExtensions.ts +148 -0
- package/src/lib/components/editor/index.ts +10 -0
- package/src/lib/components/editor/menus/EmptyBubbleMenu.tsx +42 -0
- package/src/lib/components/editor/menus/MainBubbleMenu.tsx +51 -0
- package/src/lib/components/editor/menus/TranslationBubbleMenu.tsx +57 -0
- package/src/lib/components/editor/menus/index.ts +3 -0
- package/src/lib/components/editor/menus/selectors/EndNoteSelector.tsx +388 -0
- package/src/lib/components/editor/menus/selectors/GlossarySelector.tsx +63 -0
- package/src/lib/components/editor/menus/selectors/LinkSelector.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/MantraSelector.tsx +119 -0
- package/src/lib/components/editor/menus/selectors/NodeSelector.tsx +144 -0
- package/src/lib/components/editor/menus/selectors/ParagraphButtons.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/SelectorInputField.tsx +68 -0
- package/src/lib/components/editor/menus/selectors/TextAlignSelector.tsx +89 -0
- package/src/lib/components/editor/menus/selectors/TextButtons.tsx +89 -0
- package/src/lib/components/editor/menus/selectors/TranslationNodeSelector.tsx +143 -0
- package/src/lib/components/editor/menus/selectors/TranslationTextButtons.tsx +125 -0
- package/src/lib/components/editor/menus/selectors/index.ts +5 -0
- package/src/lib/components/editor/save-filter.spec.ts +94 -0
- package/src/lib/components/editor/save-filter.ts +27 -0
- package/src/lib/components/editor/util.ts +304 -0
- package/src/lib/components/index.ts +3 -0
- package/src/lib/components/reader/ReaderBackMatterPage.tsx +62 -0
- package/src/lib/components/reader/ReaderBackMatterPanel.tsx +53 -0
- package/src/lib/components/reader/ReaderBodyPage.tsx +46 -0
- package/src/lib/components/reader/ReaderBodyPanel.tsx +68 -0
- package/src/lib/components/reader/ReaderLayout.tsx +39 -0
- package/src/lib/components/reader/ReaderLeftPanel.tsx +8 -0
- package/src/lib/components/reader/ReaderLeftPanelPage.tsx +31 -0
- package/src/lib/components/reader/TranslationReader.tsx +28 -0
- package/src/lib/components/reader/index.ts +2 -0
- package/src/lib/components/reader/ssr.ts +3 -0
- package/src/lib/components/shared/AiSummarizerPage.tsx +12 -0
- package/src/lib/components/shared/BackMatterPanel.tsx +143 -0
- package/src/lib/components/shared/BodyPanel.tsx +214 -0
- package/src/lib/components/shared/HoverCardProvider.tsx +407 -0
- package/src/lib/components/shared/Imprint.tsx +24 -0
- package/src/lib/components/shared/LabeledElement.tsx +133 -0
- package/src/lib/components/shared/LeftPanel.tsx +65 -0
- package/src/lib/components/shared/NavigationContext.ts +64 -0
- package/src/lib/components/shared/NavigationProvider.tsx +368 -0
- package/src/lib/components/shared/OpenGraphImage.tsx +75 -0
- package/src/lib/components/shared/PassageSkeleton.tsx +10 -0
- package/src/lib/components/shared/RestrictionWarning.tsx +177 -0
- package/src/lib/components/shared/SourceReader.tsx +83 -0
- package/src/lib/components/shared/SuggestRevisionForm.tsx +99 -0
- package/src/lib/components/shared/TableOfContents.tsx +280 -0
- package/src/lib/components/shared/ThreeColumnRenderer.tsx +54 -0
- package/src/lib/components/shared/TranslationHeader.tsx +86 -0
- package/src/lib/components/shared/TranslationHoverCard.tsx +84 -0
- package/src/lib/components/shared/TranslationSkeleton.tsx +16 -0
- package/src/lib/components/shared/TranslationTable.tsx +155 -0
- package/src/lib/components/shared/bibliography/BibliographyBody.tsx +28 -0
- package/src/lib/components/shared/bibliography/BibliographyList.tsx +63 -0
- package/src/lib/components/shared/bibliography/index.ts +1 -0
- package/src/lib/components/shared/generate-metadata.ts +44 -0
- package/src/lib/components/shared/glossary/GlossaryInstanceBody.tsx +144 -0
- package/src/lib/components/shared/glossary/GlossaryPaginationProvider.tsx +317 -0
- package/src/lib/components/shared/glossary/GlossarySkeleton.tsx +19 -0
- package/src/lib/components/shared/glossary/GlossaryTermList.tsx +58 -0
- package/src/lib/components/shared/glossary/index.ts +3 -0
- package/src/lib/components/shared/hooks/useGlossaryInstanceListener.tsx +42 -0
- package/src/lib/components/shared/hooks/useScrollInTab.tsx +43 -0
- package/src/lib/components/shared/hooks/useScrollPositionRestore.ts +274 -0
- package/src/lib/components/shared/hooks/useTohToggle.tsx +52 -0
- package/src/lib/components/shared/index.ts +11 -0
- package/src/lib/components/shared/ssr.ts +2 -0
- package/src/lib/components/shared/titles/FramedCard.tsx +132 -0
- package/src/lib/components/shared/titles/LongTitle.tsx +20 -0
- package/src/lib/components/shared/titles/LongTitles.tsx +28 -0
- package/src/lib/components/shared/titles/Title.tsx +54 -0
- package/src/lib/components/shared/titles/TitleDetails.tsx +47 -0
- package/src/lib/components/shared/titles/TitleForm.tsx +37 -0
- package/src/lib/components/shared/titles/Titles.tsx +114 -0
- package/src/lib/components/shared/titles/TitlesCard.tsx +113 -0
- package/src/lib/components/shared/titles/index.ts +8 -0
- package/src/lib/components/shared/types.ts +79 -0
- package/src/lib/components/ssr.ts +2 -0
- package/src/lib/exporters/abbreviation.spec.ts +31 -0
- package/src/lib/exporters/abbreviation.ts +22 -0
- package/src/lib/exporters/annotation.ts +193 -0
- package/src/lib/exporters/audio.spec.ts +77 -0
- package/src/lib/exporters/audio.ts +27 -0
- package/src/lib/exporters/blockquote.spec.ts +48 -0
- package/src/lib/exporters/blockquote.ts +24 -0
- package/src/lib/exporters/code.spec.ts +93 -0
- package/src/lib/exporters/code.ts +26 -0
- package/src/lib/exporters/end-note-link.spec.ts +104 -0
- package/src/lib/exporters/end-note-link.ts +35 -0
- package/src/lib/exporters/export.ts +12 -0
- package/src/lib/exporters/glossary-instance.spec.ts +85 -0
- package/src/lib/exporters/glossary-instance.ts +31 -0
- package/src/lib/exporters/has-abbreviation.spec.ts +31 -0
- package/src/lib/exporters/has-abbreviation.ts +21 -0
- package/src/lib/exporters/heading.spec.ts +80 -0
- package/src/lib/exporters/heading.ts +28 -0
- package/src/lib/exporters/image.spec.ts +48 -0
- package/src/lib/exporters/image.ts +25 -0
- package/src/lib/exporters/indent.spec.ts +58 -0
- package/src/lib/exporters/indent.ts +18 -0
- package/src/lib/exporters/index.ts +1 -0
- package/src/lib/exporters/internal-link.spec.ts +90 -0
- package/src/lib/exporters/internal-link.ts +35 -0
- package/src/lib/exporters/italic.spec.ts +84 -0
- package/src/lib/exporters/italic.ts +55 -0
- package/src/lib/exporters/leading-space.spec.ts +28 -0
- package/src/lib/exporters/leading-space.ts +16 -0
- package/src/lib/exporters/line-group.spec.ts +48 -0
- package/src/lib/exporters/line-group.ts +24 -0
- package/src/lib/exporters/line.spec.ts +48 -0
- package/src/lib/exporters/line.ts +24 -0
- package/src/lib/exporters/link.spec.ts +123 -0
- package/src/lib/exporters/link.ts +67 -0
- package/src/lib/exporters/list-item.spec.ts +48 -0
- package/src/lib/exporters/list-item.ts +24 -0
- package/src/lib/exporters/list.spec.ts +82 -0
- package/src/lib/exporters/list.ts +31 -0
- package/src/lib/exporters/mantra.spec.ts +51 -0
- package/src/lib/exporters/mantra.ts +25 -0
- package/src/lib/exporters/mention.ts +41 -0
- package/src/lib/exporters/paragraph.spec.ts +173 -0
- package/src/lib/exporters/paragraph.ts +32 -0
- package/src/lib/exporters/quote.spec.ts +56 -0
- package/src/lib/exporters/quote.ts +25 -0
- package/src/lib/exporters/span.spec.ts +118 -0
- package/src/lib/exporters/span.ts +44 -0
- package/src/lib/exporters/table-body-data.spec.ts +48 -0
- package/src/lib/exporters/table-body-data.ts +24 -0
- package/src/lib/exporters/table-body-header.spec.ts +48 -0
- package/src/lib/exporters/table-body-header.ts +24 -0
- package/src/lib/exporters/table-body-row.spec.ts +48 -0
- package/src/lib/exporters/table-body-row.ts +24 -0
- package/src/lib/exporters/table.spec.ts +48 -0
- package/src/lib/exporters/table.ts +24 -0
- package/src/lib/exporters/trailer.spec.ts +48 -0
- package/src/lib/exporters/trailer.ts +24 -0
- package/src/lib/exporters/util.ts +62 -0
- package/src/lib/passage.ts +182 -0
- package/src/lib/titles.ts +80 -0
- package/src/lib/transformers/abbreviation.spec.ts +87 -0
- package/src/lib/transformers/abbreviation.ts +30 -0
- package/src/lib/transformers/annotate.ts +146 -0
- package/src/lib/transformers/audio.spec.ts +55 -0
- package/src/lib/transformers/audio.ts +29 -0
- package/src/lib/transformers/blockquote.spec.ts +48 -0
- package/src/lib/transformers/blockquote.ts +41 -0
- package/src/lib/transformers/code.spec.ts +52 -0
- package/src/lib/transformers/code.ts +22 -0
- package/src/lib/transformers/deprecated.ts +7 -0
- package/src/lib/transformers/end-note-link.spec.ts +56 -0
- package/src/lib/transformers/end-note-link.ts +76 -0
- package/src/lib/transformers/glossary-instance.spec.ts +55 -0
- package/src/lib/transformers/glossary-instance.ts +40 -0
- package/src/lib/transformers/has-abbreviation.spec.ts +50 -0
- package/src/lib/transformers/has-abbreviation.ts +30 -0
- package/src/lib/transformers/heading.spec.ts +62 -0
- package/src/lib/transformers/heading.ts +30 -0
- package/src/lib/transformers/image.spec.ts +51 -0
- package/src/lib/transformers/image.ts +28 -0
- package/src/lib/transformers/indent.spec.ts +53 -0
- package/src/lib/transformers/indent.ts +17 -0
- package/src/lib/transformers/index.ts +33 -0
- package/src/lib/transformers/inline-title.spec.ts +59 -0
- package/src/lib/transformers/inline-title.ts +34 -0
- package/src/lib/transformers/internal-link.spec.ts +67 -0
- package/src/lib/transformers/internal-link.ts +65 -0
- package/src/lib/transformers/italic.ts +22 -0
- package/src/lib/transformers/leading-space.spec.ts +55 -0
- package/src/lib/transformers/leading-space.ts +17 -0
- package/src/lib/transformers/line-group.spec.ts +48 -0
- package/src/lib/transformers/line-group.ts +37 -0
- package/src/lib/transformers/line.spec.ts +54 -0
- package/src/lib/transformers/line.ts +27 -0
- package/src/lib/transformers/link.spec.ts +61 -0
- package/src/lib/transformers/link.ts +27 -0
- package/src/lib/transformers/list-item.spec.ts +48 -0
- package/src/lib/transformers/list-item.ts +37 -0
- package/src/lib/transformers/list.spec.ts +58 -0
- package/src/lib/transformers/list.ts +42 -0
- package/src/lib/transformers/mantra.spec.ts +58 -0
- package/src/lib/transformers/mantra.ts +28 -0
- package/src/lib/transformers/mention.ts +70 -0
- package/src/lib/transformers/paragraph.spec.ts +46 -0
- package/src/lib/transformers/paragraph.ts +26 -0
- package/src/lib/transformers/quote.spec.ts +42 -0
- package/src/lib/transformers/quote.ts +3 -0
- package/src/lib/transformers/quoted.ts +3 -0
- package/src/lib/transformers/recurse.ts +76 -0
- package/src/lib/transformers/reference.ts +3 -0
- package/src/lib/transformers/span.spec.ts +68 -0
- package/src/lib/transformers/span.ts +78 -0
- package/src/lib/transformers/split-at.ts +58 -0
- package/src/lib/transformers/split-block.ts +110 -0
- package/src/lib/transformers/split-content.ts +67 -0
- package/src/lib/transformers/split-insert.ts +76 -0
- package/src/lib/transformers/split-marks.ts +42 -0
- package/src/lib/transformers/split-node.ts +138 -0
- package/src/lib/transformers/table-body-data.spec.ts +44 -0
- package/src/lib/transformers/table-body-data.ts +29 -0
- package/src/lib/transformers/table-body-header.spec.ts +44 -0
- package/src/lib/transformers/table-body-header.ts +29 -0
- package/src/lib/transformers/table-body-row.spec.ts +44 -0
- package/src/lib/transformers/table-body-row.ts +29 -0
- package/src/lib/transformers/table.spec.ts +47 -0
- package/src/lib/transformers/table.ts +29 -0
- package/src/lib/transformers/trailer.spec.ts +43 -0
- package/src/lib/transformers/trailer.ts +26 -0
- package/src/lib/transformers/transformer.ts +25 -0
- package/src/lib/transformers/unknown.ts +8 -0
- package/src/lib/transformers/util.ts +20 -0
- package/src/lib/types.ts +10 -0
- package/src/ssr.ts +1 -0
- package/tsconfig.json +20 -0
- package/tsconfig.lib.json +29 -0
- package/tsconfig.spec.json +22 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { EditorContent } from '@tiptap/react';
|
|
4
|
+
import { TranslationBubbleMenu } from './menus';
|
|
5
|
+
import { cn } from '@eightyfourthousand/lib-utils';
|
|
6
|
+
import { usePagination } from './PaginationProvider';
|
|
7
|
+
import type {
|
|
8
|
+
TranslationEditorContentItem,
|
|
9
|
+
TranslationEditorContent,
|
|
10
|
+
} from '@eightyfourthousand/data-access';
|
|
11
|
+
|
|
12
|
+
// Re-export for backwards compatibility
|
|
13
|
+
export type { TranslationEditorContentItem, TranslationEditorContent };
|
|
14
|
+
|
|
15
|
+
export const TranslationEditor = ({ className }: { className?: string }) => {
|
|
16
|
+
const { editor } = usePagination();
|
|
17
|
+
|
|
18
|
+
if (!editor) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div className={cn('flex h-full', className)}>
|
|
24
|
+
<div className="relative flex flex-col flex-1 h-full">
|
|
25
|
+
<EditorContent className="flex-1" editor={editor} />
|
|
26
|
+
{editor.isEditable && <TranslationBubbleMenu editor={editor} />}
|
|
27
|
+
</div>
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default TranslationEditor;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Extension, Node } from '@tiptap/core';
|
|
2
|
+
import { mergeAttributes } from '@tiptap/react';
|
|
3
|
+
import { CommandSuggestionItem } from '../SlashCommand/SuggestionList';
|
|
4
|
+
import { TableIcon } from 'lucide-react';
|
|
5
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
6
|
+
|
|
7
|
+
export interface AbbreviationOptions {
|
|
8
|
+
HTMLAttributes: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare module '@tiptap/core' {
|
|
12
|
+
interface Commands<ReturnType> {
|
|
13
|
+
abbreviationCommand: {
|
|
14
|
+
insertAbbreviation: () => ReturnType;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const AbbreviationCommand = Extension.create({
|
|
20
|
+
name: 'abbreviationCommand',
|
|
21
|
+
|
|
22
|
+
addCommands() {
|
|
23
|
+
return {
|
|
24
|
+
insertAbbreviation:
|
|
25
|
+
() =>
|
|
26
|
+
({ commands }) => {
|
|
27
|
+
return commands.insertContent({
|
|
28
|
+
type: 'paragraph',
|
|
29
|
+
content: [
|
|
30
|
+
{
|
|
31
|
+
type: 'abbreviation',
|
|
32
|
+
content: [{ type: 'text', text: 'A' }],
|
|
33
|
+
attrs: { uuid: uuidv4() },
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: 'hasAbbreviation',
|
|
37
|
+
content: [{ type: 'text', text: 'Abbreviation' }],
|
|
38
|
+
attrs: { uuid: uuidv4() },
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const AbbreviationSuggestion: CommandSuggestionItem = {
|
|
48
|
+
title: 'Abbreviation',
|
|
49
|
+
description: 'Insert an abbreviation',
|
|
50
|
+
keywords: ['abbreviation'],
|
|
51
|
+
icon: TableIcon,
|
|
52
|
+
command: ({ editor, range }) => {
|
|
53
|
+
editor.chain().focus().deleteRange(range).insertAbbreviation().run();
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const Abbreviation = Node.create<AbbreviationOptions>({
|
|
58
|
+
name: 'abbreviation',
|
|
59
|
+
group: 'inline',
|
|
60
|
+
content: 'inline*',
|
|
61
|
+
inline: true,
|
|
62
|
+
|
|
63
|
+
addAttributes() {
|
|
64
|
+
return {
|
|
65
|
+
abbreviation: {
|
|
66
|
+
default: undefined,
|
|
67
|
+
parseHTML(element: HTMLElement) {
|
|
68
|
+
return element.getAttribute('abbreviation');
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
addOptions() {
|
|
75
|
+
return {
|
|
76
|
+
HTMLAttributes: {
|
|
77
|
+
class: 'italic min-w-8',
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
parseHTML() {
|
|
83
|
+
return [{ tag: 'span[type="abbreviation"]' }];
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
renderHTML({ HTMLAttributes }: { HTMLAttributes: Record<string, unknown> }) {
|
|
87
|
+
return [
|
|
88
|
+
'span',
|
|
89
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
90
|
+
type: 'abbreviation',
|
|
91
|
+
}),
|
|
92
|
+
0,
|
|
93
|
+
];
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
export const HasAbbreviation = Node.create<AbbreviationOptions>({
|
|
98
|
+
name: 'hasAbbreviation',
|
|
99
|
+
group: 'inline',
|
|
100
|
+
content: 'inline*',
|
|
101
|
+
inline: true,
|
|
102
|
+
|
|
103
|
+
addAttributes() {
|
|
104
|
+
return {
|
|
105
|
+
abbreviation: {
|
|
106
|
+
default: undefined,
|
|
107
|
+
parseHTML(element: HTMLElement) {
|
|
108
|
+
return element.getAttribute('abbreviation');
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
addOptions() {
|
|
115
|
+
return {
|
|
116
|
+
HTMLAttributes: {},
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
parseHTML() {
|
|
121
|
+
return [{ tag: 'span[type=hasAbbreviation]' }];
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
renderHTML({ HTMLAttributes }: { HTMLAttributes: Record<string, unknown> }) {
|
|
125
|
+
return [
|
|
126
|
+
'span',
|
|
127
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
128
|
+
type: 'hasAbbreviation',
|
|
129
|
+
}),
|
|
130
|
+
0,
|
|
131
|
+
];
|
|
132
|
+
},
|
|
133
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { mergeAttributes, Node } from '@tiptap/core';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
|
|
4
|
+
export interface AudioOptions {
|
|
5
|
+
/**
|
|
6
|
+
* HTML attributes to add to the image element.
|
|
7
|
+
* @default {}
|
|
8
|
+
* @example { class: 'foo' }
|
|
9
|
+
*/
|
|
10
|
+
HTMLAttributes: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare module '@tiptap/core' {
|
|
14
|
+
interface Commands<ReturnType> {
|
|
15
|
+
audio: {
|
|
16
|
+
/**
|
|
17
|
+
* Set a audio node
|
|
18
|
+
* @param options.updateSelection set to true will select the newly inserted content
|
|
19
|
+
*/
|
|
20
|
+
setAudio: (options: { src: string }) => ReturnType;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const Audio = Node.create<AudioOptions>({
|
|
26
|
+
name: 'audio',
|
|
27
|
+
|
|
28
|
+
group: 'block',
|
|
29
|
+
|
|
30
|
+
addAttributes() {
|
|
31
|
+
return {
|
|
32
|
+
src: {
|
|
33
|
+
default: null,
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
parseHTML() {
|
|
39
|
+
return [
|
|
40
|
+
{
|
|
41
|
+
tag: 'audio[src]',
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
renderHTML({ HTMLAttributes }) {
|
|
47
|
+
return [
|
|
48
|
+
'audio',
|
|
49
|
+
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
|
|
50
|
+
controls: true,
|
|
51
|
+
class: 'w-full max-w-xl my-4',
|
|
52
|
+
}),
|
|
53
|
+
];
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
addCommands() {
|
|
57
|
+
return {
|
|
58
|
+
setAudio:
|
|
59
|
+
(options) =>
|
|
60
|
+
({ commands }) => {
|
|
61
|
+
return commands.insertContent({
|
|
62
|
+
type: this.name,
|
|
63
|
+
attrs: options,
|
|
64
|
+
uuid: uuidv4(),
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { mergeAttributes } from '@tiptap/react';
|
|
2
|
+
import { Bold as TipTapBold } from '@tiptap/extension-bold';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
|
|
5
|
+
export const Bold = TipTapBold.extend({
|
|
6
|
+
addAttributes() {
|
|
7
|
+
return {
|
|
8
|
+
...this.parent?.(),
|
|
9
|
+
type: {
|
|
10
|
+
default: undefined,
|
|
11
|
+
parseHTML: (element) => element.getAttribute('data-type'),
|
|
12
|
+
renderHTML(attributes) {
|
|
13
|
+
return mergeAttributes(attributes, { 'data-type': attributes.type });
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
textStyle: {
|
|
17
|
+
default: undefined,
|
|
18
|
+
parseHTML: (element) => element.getAttribute('data-text-style'),
|
|
19
|
+
renderHTML(attributes) {
|
|
20
|
+
return mergeAttributes(attributes, {
|
|
21
|
+
'data-text-style': attributes.textStyle,
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
addCommands() {
|
|
28
|
+
const name = this.name;
|
|
29
|
+
return {
|
|
30
|
+
...this.parent?.(),
|
|
31
|
+
setBold() {
|
|
32
|
+
return ({ commands }) => {
|
|
33
|
+
return commands.setMark(name, { uuid: uuidv4() });
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
toggleBold() {
|
|
37
|
+
return ({ commands }) => {
|
|
38
|
+
return commands.toggleMark(name, { uuid: uuidv4() });
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
});
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core';
|
|
2
|
+
import {
|
|
3
|
+
NodeSelection,
|
|
4
|
+
Plugin,
|
|
5
|
+
PluginKey,
|
|
6
|
+
TextSelection,
|
|
7
|
+
} from '@tiptap/pm/state';
|
|
8
|
+
import { Fragment, Slice, Node } from '@tiptap/pm/model';
|
|
9
|
+
import { EditorView } from '@tiptap/pm/view';
|
|
10
|
+
|
|
11
|
+
export interface DragHandleOptions {
|
|
12
|
+
/**
|
|
13
|
+
* The width of the drag handle
|
|
14
|
+
*/
|
|
15
|
+
dragHandleWidth: number;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The treshold for scrolling
|
|
19
|
+
*/
|
|
20
|
+
scrollTreshold: number;
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* The css selector to query for the drag handle. (eg: '.custom-handle').
|
|
24
|
+
* If handle element is found, that element will be used as drag handle. If not, a default handle will be created
|
|
25
|
+
*/
|
|
26
|
+
dragHandleSelector?: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Tags to be excluded for drag handle
|
|
30
|
+
*/
|
|
31
|
+
excludedTags: string[];
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Custom nodes to be included for drag handle
|
|
35
|
+
*/
|
|
36
|
+
customNodes: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const absoluteRect = (node: Element) => {
|
|
40
|
+
const data = node.getBoundingClientRect();
|
|
41
|
+
const modal = node.closest('[role="dialog"]');
|
|
42
|
+
|
|
43
|
+
if (modal && window.getComputedStyle(modal).transform !== 'none') {
|
|
44
|
+
const modalRect = modal.getBoundingClientRect();
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
top: data.top - modalRect.top,
|
|
48
|
+
left: data.left - modalRect.left,
|
|
49
|
+
width: data.width,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
top: data.top,
|
|
54
|
+
left: data.left,
|
|
55
|
+
width: data.width,
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function nodeDOMAtCoords(
|
|
60
|
+
coords: { x: number; y: number },
|
|
61
|
+
options: DragHandleOptions,
|
|
62
|
+
) {
|
|
63
|
+
const selectors = [
|
|
64
|
+
'li',
|
|
65
|
+
'p:not(:first-child)',
|
|
66
|
+
'pre',
|
|
67
|
+
'blockquote',
|
|
68
|
+
'h1',
|
|
69
|
+
'h2',
|
|
70
|
+
'h3',
|
|
71
|
+
'h4',
|
|
72
|
+
'h5',
|
|
73
|
+
'h6',
|
|
74
|
+
...options.customNodes.map((node) => `[data-type=${node}]`),
|
|
75
|
+
].join(', ');
|
|
76
|
+
return document
|
|
77
|
+
.elementsFromPoint(coords.x, coords.y)
|
|
78
|
+
.find(
|
|
79
|
+
(elem: Element) =>
|
|
80
|
+
elem.parentElement?.matches?.('.ProseMirror') ||
|
|
81
|
+
elem.matches(selectors),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
const nodePosAtDOM = (
|
|
85
|
+
node: Element,
|
|
86
|
+
view: EditorView,
|
|
87
|
+
options: DragHandleOptions,
|
|
88
|
+
) => {
|
|
89
|
+
const boundingRect = node.getBoundingClientRect();
|
|
90
|
+
|
|
91
|
+
return view.posAtCoords({
|
|
92
|
+
left: boundingRect.left + 50 + options.dragHandleWidth,
|
|
93
|
+
top: boundingRect.top + 1,
|
|
94
|
+
})?.inside;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const calcNodePos = (pos: number, view: EditorView) => {
|
|
98
|
+
const $pos = view.state.doc.resolve(pos);
|
|
99
|
+
if ($pos.depth > 1) return $pos.before($pos.depth);
|
|
100
|
+
return pos;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const DragHandlePlugin = (
|
|
104
|
+
options: DragHandleOptions & { pluginKey: string },
|
|
105
|
+
) => {
|
|
106
|
+
let listType = '';
|
|
107
|
+
function handleDragStart(event: DragEvent, view: EditorView) {
|
|
108
|
+
view.focus();
|
|
109
|
+
|
|
110
|
+
if (!event.dataTransfer) return;
|
|
111
|
+
|
|
112
|
+
const node = nodeDOMAtCoords(
|
|
113
|
+
{
|
|
114
|
+
x: event.clientX + 50 + options.dragHandleWidth,
|
|
115
|
+
y: event.clientY,
|
|
116
|
+
},
|
|
117
|
+
options,
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
if (!(node instanceof Element)) return;
|
|
121
|
+
|
|
122
|
+
let draggedNodePos = nodePosAtDOM(node, view, options);
|
|
123
|
+
if (draggedNodePos == null || draggedNodePos < 0) return;
|
|
124
|
+
draggedNodePos = calcNodePos(draggedNodePos, view);
|
|
125
|
+
|
|
126
|
+
const { from, to } = view.state.selection;
|
|
127
|
+
const diff = from - to;
|
|
128
|
+
|
|
129
|
+
const fromSelectionPos = calcNodePos(from, view);
|
|
130
|
+
let differentNodeSelected = false;
|
|
131
|
+
|
|
132
|
+
const nodePos = view.state.doc.resolve(fromSelectionPos);
|
|
133
|
+
|
|
134
|
+
// Check if nodePos points to the top level node
|
|
135
|
+
if (nodePos.node().type.name === 'doc') differentNodeSelected = true;
|
|
136
|
+
else {
|
|
137
|
+
const nodeSelection = NodeSelection.create(
|
|
138
|
+
view.state.doc,
|
|
139
|
+
nodePos.before(),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// Check if the node where the drag event started is part of the current selection
|
|
143
|
+
differentNodeSelected = !(
|
|
144
|
+
draggedNodePos + 1 >= nodeSelection.$from.pos &&
|
|
145
|
+
draggedNodePos <= nodeSelection.$to.pos
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
let selection = view.state.selection;
|
|
149
|
+
if (
|
|
150
|
+
!differentNodeSelected &&
|
|
151
|
+
diff !== 0 &&
|
|
152
|
+
!(view.state.selection instanceof NodeSelection)
|
|
153
|
+
) {
|
|
154
|
+
const endSelection = NodeSelection.create(view.state.doc, to - 1);
|
|
155
|
+
selection = TextSelection.create(
|
|
156
|
+
view.state.doc,
|
|
157
|
+
draggedNodePos,
|
|
158
|
+
endSelection.$to.pos,
|
|
159
|
+
);
|
|
160
|
+
} else {
|
|
161
|
+
selection = NodeSelection.create(view.state.doc, draggedNodePos);
|
|
162
|
+
|
|
163
|
+
// if inline node is selected, e.g mention -> go to the parent node to select the whole node
|
|
164
|
+
// if table row is selected, go to the parent node to select the whole node
|
|
165
|
+
if (
|
|
166
|
+
(selection as NodeSelection).node.type.isInline ||
|
|
167
|
+
(selection as NodeSelection).node.type.name === 'tableRow'
|
|
168
|
+
) {
|
|
169
|
+
const pos = view.state.doc.resolve(selection.from);
|
|
170
|
+
selection = NodeSelection.create(view.state.doc, pos.before());
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
view.dispatch(view.state.tr.setSelection(selection));
|
|
174
|
+
|
|
175
|
+
// If the selected node is a list item, we need to save the type of the wrapping list e.g. OL or UL
|
|
176
|
+
if (
|
|
177
|
+
view.state.selection instanceof NodeSelection &&
|
|
178
|
+
view.state.selection.node.type.name === 'listItem' &&
|
|
179
|
+
node.parentElement?.tagName
|
|
180
|
+
) {
|
|
181
|
+
listType = node.parentElement.tagName;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const slice = view.state.selection.content();
|
|
185
|
+
const { dom, text } = view.serializeForClipboard(slice);
|
|
186
|
+
|
|
187
|
+
event.dataTransfer.clearData();
|
|
188
|
+
event.dataTransfer.setData('text/html', dom.innerHTML);
|
|
189
|
+
event.dataTransfer.setData('text/plain', text);
|
|
190
|
+
event.dataTransfer.effectAllowed = 'copyMove';
|
|
191
|
+
|
|
192
|
+
event.dataTransfer.setDragImage(node, 0, 0);
|
|
193
|
+
|
|
194
|
+
view.dragging = { slice, move: event.ctrlKey };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let dragHandleElement: HTMLElement | null = null;
|
|
198
|
+
|
|
199
|
+
function hideDragHandle() {
|
|
200
|
+
if (dragHandleElement) {
|
|
201
|
+
dragHandleElement.classList.add('hide');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function showDragHandle() {
|
|
206
|
+
if (dragHandleElement) {
|
|
207
|
+
dragHandleElement.classList.remove('hide');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function hideHandleOnEditorOut(event: MouseEvent) {
|
|
212
|
+
if (event.target instanceof Element) {
|
|
213
|
+
// Check if the relatedTarget class is still inside the editor
|
|
214
|
+
const relatedTarget = event.relatedTarget as HTMLElement;
|
|
215
|
+
const isInsideEditor =
|
|
216
|
+
relatedTarget?.classList.contains('tiptap') ||
|
|
217
|
+
relatedTarget?.classList.contains('drag-handle');
|
|
218
|
+
|
|
219
|
+
if (isInsideEditor) return;
|
|
220
|
+
}
|
|
221
|
+
hideDragHandle();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Throttling state for mousemove handler
|
|
225
|
+
let lastPos = { x: 0, y: 0 };
|
|
226
|
+
let rafId: number | null = null;
|
|
227
|
+
|
|
228
|
+
return new Plugin({
|
|
229
|
+
key: new PluginKey(options.pluginKey),
|
|
230
|
+
view: (view) => {
|
|
231
|
+
const handleBySelector = options.dragHandleSelector
|
|
232
|
+
? document.querySelector<HTMLElement>(options.dragHandleSelector)
|
|
233
|
+
: null;
|
|
234
|
+
dragHandleElement = handleBySelector ?? document.createElement('div');
|
|
235
|
+
dragHandleElement.draggable = true;
|
|
236
|
+
dragHandleElement.dataset.dragHandle = '';
|
|
237
|
+
dragHandleElement.classList.add('drag-handle');
|
|
238
|
+
|
|
239
|
+
function onDragHandleDragStart(e: DragEvent) {
|
|
240
|
+
handleDragStart(e, view);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
dragHandleElement.addEventListener('dragstart', onDragHandleDragStart);
|
|
244
|
+
|
|
245
|
+
function onDragHandleDrag(e: DragEvent) {
|
|
246
|
+
hideDragHandle();
|
|
247
|
+
const scrollY = window.scrollY;
|
|
248
|
+
if (e.clientY < options.scrollTreshold) {
|
|
249
|
+
window.scrollTo({ top: scrollY - 30, behavior: 'smooth' });
|
|
250
|
+
} else if (window.innerHeight - e.clientY < options.scrollTreshold) {
|
|
251
|
+
window.scrollTo({ top: scrollY + 30, behavior: 'smooth' });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
dragHandleElement.addEventListener('drag', onDragHandleDrag);
|
|
256
|
+
|
|
257
|
+
hideDragHandle();
|
|
258
|
+
|
|
259
|
+
if (!handleBySelector) {
|
|
260
|
+
view?.dom?.parentElement?.appendChild(dragHandleElement);
|
|
261
|
+
}
|
|
262
|
+
view?.dom?.parentElement?.addEventListener(
|
|
263
|
+
'mouseout',
|
|
264
|
+
hideHandleOnEditorOut,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
destroy: () => {
|
|
269
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
270
|
+
if (!handleBySelector) {
|
|
271
|
+
dragHandleElement?.remove?.();
|
|
272
|
+
}
|
|
273
|
+
dragHandleElement?.removeEventListener('drag', onDragHandleDrag);
|
|
274
|
+
dragHandleElement?.removeEventListener(
|
|
275
|
+
'dragstart',
|
|
276
|
+
onDragHandleDragStart,
|
|
277
|
+
);
|
|
278
|
+
dragHandleElement = null;
|
|
279
|
+
view?.dom?.parentElement?.removeEventListener(
|
|
280
|
+
'mouseout',
|
|
281
|
+
hideHandleOnEditorOut,
|
|
282
|
+
);
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
},
|
|
286
|
+
props: {
|
|
287
|
+
handleDOMEvents: {
|
|
288
|
+
mousemove: (view, event) => {
|
|
289
|
+
if (!view.editable) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Skip if position hasn't changed significantly (< 5px threshold)
|
|
294
|
+
const dx = Math.abs(event.clientX - lastPos.x);
|
|
295
|
+
const dy = Math.abs(event.clientY - lastPos.y);
|
|
296
|
+
if (dx < 5 && dy < 5) return;
|
|
297
|
+
|
|
298
|
+
lastPos = { x: event.clientX, y: event.clientY };
|
|
299
|
+
|
|
300
|
+
// Use requestAnimationFrame to batch updates
|
|
301
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
302
|
+
rafId = requestAnimationFrame(() => {
|
|
303
|
+
rafId = null;
|
|
304
|
+
|
|
305
|
+
const node = nodeDOMAtCoords(
|
|
306
|
+
{
|
|
307
|
+
x: event.clientX + 50 + options.dragHandleWidth,
|
|
308
|
+
y: event.clientY,
|
|
309
|
+
},
|
|
310
|
+
options,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
const notDragging = node?.closest('.not-draggable');
|
|
314
|
+
const excludedTagList = options.excludedTags
|
|
315
|
+
.concat(['ol', 'ul'])
|
|
316
|
+
.join(', ');
|
|
317
|
+
|
|
318
|
+
if (
|
|
319
|
+
!(node instanceof Element) ||
|
|
320
|
+
node.matches(excludedTagList) ||
|
|
321
|
+
notDragging
|
|
322
|
+
) {
|
|
323
|
+
hideDragHandle();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const compStyle = window.getComputedStyle(node);
|
|
328
|
+
const parsedLineHeight = parseInt(compStyle.lineHeight, 10);
|
|
329
|
+
const lineHeight = isNaN(parsedLineHeight)
|
|
330
|
+
? parseInt(compStyle.fontSize) * 1.2
|
|
331
|
+
: parsedLineHeight;
|
|
332
|
+
const paddingTop = parseInt(compStyle.paddingTop, 10);
|
|
333
|
+
|
|
334
|
+
const rect = absoluteRect(node);
|
|
335
|
+
|
|
336
|
+
rect.top += (lineHeight - 24) / 2;
|
|
337
|
+
rect.top += paddingTop;
|
|
338
|
+
// Li markers
|
|
339
|
+
if (node.matches('ul:not([data-type=taskList]) li, ol li')) {
|
|
340
|
+
rect.left -= options.dragHandleWidth;
|
|
341
|
+
}
|
|
342
|
+
rect.width = options.dragHandleWidth;
|
|
343
|
+
|
|
344
|
+
if (!dragHandleElement) return;
|
|
345
|
+
|
|
346
|
+
dragHandleElement.style.left = `${rect.left - rect.width}px`;
|
|
347
|
+
dragHandleElement.style.top = `${rect.top}px`;
|
|
348
|
+
showDragHandle();
|
|
349
|
+
});
|
|
350
|
+
},
|
|
351
|
+
keydown: () => {
|
|
352
|
+
hideDragHandle();
|
|
353
|
+
},
|
|
354
|
+
mousewheel: () => {
|
|
355
|
+
hideDragHandle();
|
|
356
|
+
},
|
|
357
|
+
// dragging class is used for CSS
|
|
358
|
+
dragstart: (view) => {
|
|
359
|
+
view.dom.classList.add('dragging');
|
|
360
|
+
},
|
|
361
|
+
drop: (view, event) => {
|
|
362
|
+
view.dom.classList.remove('dragging');
|
|
363
|
+
hideDragHandle();
|
|
364
|
+
let droppedNode: Node | null = null;
|
|
365
|
+
const dropPos = view.posAtCoords({
|
|
366
|
+
left: event.clientX,
|
|
367
|
+
top: event.clientY,
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
if (!dropPos) return;
|
|
371
|
+
|
|
372
|
+
if (view.state.selection instanceof NodeSelection) {
|
|
373
|
+
droppedNode = view.state.selection.node;
|
|
374
|
+
}
|
|
375
|
+
if (!droppedNode) return;
|
|
376
|
+
|
|
377
|
+
const resolvedPos = view.state.doc.resolve(dropPos.pos);
|
|
378
|
+
|
|
379
|
+
const isDroppedInsideList =
|
|
380
|
+
resolvedPos.parent.type.name === 'listItem';
|
|
381
|
+
|
|
382
|
+
// If the selected node is a list item and is not dropped inside a list, we need to wrap it inside <ol> tag otherwise ol list items will be transformed into ul list item when dropped
|
|
383
|
+
if (
|
|
384
|
+
view.state.selection instanceof NodeSelection &&
|
|
385
|
+
view.state.selection.node.type.name === 'listItem' &&
|
|
386
|
+
!isDroppedInsideList &&
|
|
387
|
+
listType === 'OL'
|
|
388
|
+
) {
|
|
389
|
+
const newList = view.state.schema.nodes.orderedList?.createAndFill(
|
|
390
|
+
null,
|
|
391
|
+
droppedNode,
|
|
392
|
+
);
|
|
393
|
+
const slice = new Slice(Fragment.from(newList), 0, 0);
|
|
394
|
+
view.dragging = { slice, move: event.ctrlKey };
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
dragend: (view) => {
|
|
398
|
+
view.dom.classList.remove('dragging');
|
|
399
|
+
},
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
export const DragHandle = Extension.create({
|
|
406
|
+
name: 'dragHandle',
|
|
407
|
+
|
|
408
|
+
addOptions() {
|
|
409
|
+
return {
|
|
410
|
+
dragHandleWidth: 25,
|
|
411
|
+
scrollTreshold: 100,
|
|
412
|
+
excludedTags: [],
|
|
413
|
+
customNodes: [],
|
|
414
|
+
};
|
|
415
|
+
},
|
|
416
|
+
|
|
417
|
+
addProseMirrorPlugins() {
|
|
418
|
+
return [
|
|
419
|
+
DragHandlePlugin({
|
|
420
|
+
pluginKey: 'dragHandle',
|
|
421
|
+
dragHandleWidth: this.options.dragHandleWidth,
|
|
422
|
+
scrollTreshold: this.options.scrollTreshold,
|
|
423
|
+
dragHandleSelector: this.options.dragHandleSelector,
|
|
424
|
+
excludedTags: this.options.excludedTags,
|
|
425
|
+
customNodes: this.options.customNodes,
|
|
426
|
+
}),
|
|
427
|
+
];
|
|
428
|
+
},
|
|
429
|
+
});
|