-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathOpBlockNoteEditor.tsx
More file actions
142 lines (130 loc) · 5.11 KB
/
OpBlockNoteEditor.tsx
File metadata and controls
142 lines (130 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* -- copyright
* OpenProject is an open source project management software.
* Copyright (C) the OpenProject GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 3.
*
* OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
* Copyright (C) 2006-2013 Jean-Philippe Lang
* Copyright (C) 2010-2013 the ChiliProject Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* See COPYRIGHT and LICENSE files for more details.
* ++
*/
import { BlockNoteEditorOptions, BlockNoteSchema } from '@blocknote/core';
import { ExternalLinkA11yExtension } from '../extensions/external-link-a11y';
import { ExternalLinkCaptureExtension } from '../extensions/external-link-capture';
import { User } from '@blocknote/core/comments';
import { filterSuggestionItems } from '@blocknote/core/extensions';
import { BlockNoteView } from '@blocknote/mantine';
import { getDefaultReactSlashMenuItems, SuggestionMenuController, useCreateBlockNote } from '@blocknote/react';
import { HocuspocusProvider } from '@hocuspocus/provider';
import { initializeOpBlockNoteExtensions, openProjectWorkPackageBlockSpec, openProjectWorkPackageSlashMenu } from 'op-blocknote-extensions';
import { useCallback, useEffect, useMemo } from 'react';
import * as Y from 'yjs';
import { useBlockNoteAttachments } from '../hooks/useBlockNoteAttachments';
import { useBlockNoteLocale } from '../hooks/useBlockNoteLocale';
import { useOpTheme } from '../hooks/useOpTheme';
interface CollaborativeUser {
name:string;
color:string;
}
export interface OpBlockNoteEditorProps {
activeUser:User;
readOnly:boolean;
openProjectUrl:string;
attachmentsUploadUrl:string;
attachmentsCollectionKey:string;
captureExternalLinks:boolean;
hocuspocusProvider?:HocuspocusProvider;
doc:Y.Doc;
}
const schema = BlockNoteSchema.create().extend({
blockSpecs: {
openProjectWorkPackage: openProjectWorkPackageBlockSpec(),
},
});
function generateRandomColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16).padStart(6, '0');
}
export function OpBlockNoteEditor({
activeUser,
readOnly,
openProjectUrl,
attachmentsUploadUrl,
attachmentsCollectionKey,
captureExternalLinks,
hocuspocusProvider,
doc,
}:OpBlockNoteEditorProps) {
const { localeString, localeDictionary } = useBlockNoteLocale(window.I18n.locale);
const { enabled: attachmentsEnabled, uploadFile } = useBlockNoteAttachments(attachmentsCollectionKey, attachmentsUploadUrl);
useEffect(() => {
initializeOpBlockNoteExtensions({ baseUrl: openProjectUrl, locale: localeString });
}, [openProjectUrl, localeString]);
const editorParams = useMemo<Partial<BlockNoteEditorOptions<typeof schema.blockSchema, typeof schema.inlineContentSchema, typeof schema.styleSchema>>>(() => {
const baseCollaboration = {
fragment: doc.getXmlFragment('document-store'),
user: {
name: activeUser.username,
color: hocuspocusProvider ? generateRandomColor() : '#333333',
...(hocuspocusProvider && { id: activeUser.id }),
} as unknown as CollaborativeUser,
};
return {
schema,
collaboration: {
...baseCollaboration,
provider: hocuspocusProvider ?? null,
...(hocuspocusProvider && { showCursorLabels: 'activity' as const }),
},
dictionary: localeDictionary,
...(attachmentsEnabled && { uploadFile }),
_tiptapOptions: {
extensions: [
ExternalLinkA11yExtension,
...(captureExternalLinks ? [ExternalLinkCaptureExtension] : []),
],
},
};
}, [hocuspocusProvider, doc, activeUser, localeDictionary, attachmentsEnabled, uploadFile, captureExternalLinks]);
const editor = useCreateBlockNote(editorParams, [activeUser]);
type EditorType = typeof editor;
const theme = useOpTheme();
const getCustomSlashMenuItems = useCallback((editorInstance:EditorType) => [
...getDefaultReactSlashMenuItems(editorInstance),
openProjectWorkPackageSlashMenu(editorInstance),
], []);
return (
<>
<BlockNoteView
editor={editor}
slashMenu={false}
theme={theme}
editable={!readOnly}
className={'block-note-editor-container'}
>
<SuggestionMenuController
triggerCharacter="/"
getItems={async (query:string) => Promise.resolve(filterSuggestionItems(getCustomSlashMenuItems(editor), query))}
/>
</BlockNoteView>
</>
);
}