diff --git a/packages/client/src/components/document/editor/editor.tsx b/packages/client/src/components/document/editor/editor.tsx index 4320408d..c6c97190 100644 --- a/packages/client/src/components/document/editor/editor.tsx +++ b/packages/client/src/components/document/editor/editor.tsx @@ -1,7 +1,7 @@ import React, { useMemo, useEffect } from 'react'; import cls from 'classnames'; import { useEditor, EditorContent } from '@tiptap/react'; -import { Layout, Nav, BackTop, Toast } from '@douyinfe/semi-ui'; +import { BackTop } from '@douyinfe/semi-ui'; import { ILoginUser, IAuthority } from '@think/domains'; import { useToggle } from 'hooks/useToggle'; import { @@ -18,8 +18,6 @@ import { DataRender } from 'components/data-render'; import { joinUser } from 'components/document/collaboration'; import styles from './index.module.scss'; -const { Header, Content } = Layout; - interface IProps { user: ILoginUser; documentId: string; @@ -70,6 +68,8 @@ export const Editor: React.FC = ({ user, documentId, authority, classNam toggleLoading(false); }); + // provid + provider.on('status', async ({ status }) => { console.log('status', status); }); diff --git a/packages/client/src/components/document/reader/user.tsx b/packages/client/src/components/document/reader/user.tsx index 05057691..53432c25 100644 --- a/packages/client/src/components/document/reader/user.tsx +++ b/packages/client/src/components/document/reader/user.tsx @@ -43,8 +43,6 @@ export const CreateUser: React.FC<{ document: IDocument; container: () => HTMLEl const el = container && container(); - console.log(el); - if (!el) return content; return createPortal(content, el); }; diff --git a/packages/client/src/components/tiptap/basekit.tsx b/packages/client/src/components/tiptap/basekit.tsx index f8436940..78bae278 100644 --- a/packages/client/src/components/tiptap/basekit.tsx +++ b/packages/client/src/components/tiptap/basekit.tsx @@ -15,9 +15,6 @@ import { Emoji } from './extensions/emoji'; import { EvokeMenu } from './extensions/evokeMenu'; import { Focus } from './extensions/focus'; import { FontSize } from './extensions/fontSize'; -import { FootnoteDefinition } from './extensions/footnoteDefinition'; -import { FootnoteReference } from './extensions/footnoteReference'; -import { FootnotesSection } from './extensions/footnotesSection'; import { Gapcursor } from './extensions/gapCursor'; import { HardBreak } from './extensions/hardBreak'; import { Heading } from './extensions/heading'; @@ -34,7 +31,6 @@ import { Loading } from './extensions/loading'; import { Mind } from './extensions/mind'; import { OrderedList } from './extensions/orderedList'; import { Paragraph } from './extensions/paragraph'; -import { Paste } from './extensions/paste'; import { Placeholder } from './extensions/placeholder'; import { SearchNReplace } from './extensions/search'; import { Status } from './extensions/status'; @@ -51,6 +47,7 @@ import { TaskList } from './extensions/taskList'; import { Title } from './extensions/title'; import { TrailingNode } from './extensions/trailingNode'; import { Underline } from './extensions/underline'; +import { Paste } from './extensions/paste'; export const BaseKit = [ Attachment, @@ -70,9 +67,6 @@ export const BaseKit = [ EvokeMenu, Focus, FontSize, - FootnoteDefinition, - FootnoteReference, - FootnotesSection, Gapcursor, HardBreak, Heading, @@ -89,7 +83,6 @@ export const BaseKit = [ Mind, OrderedList, Paragraph, - Paste, Placeholder, SearchNReplace, Status, @@ -106,4 +99,5 @@ export const BaseKit = [ Title, TrailingNode, Underline, + Paste, ]; diff --git a/packages/client/src/components/tiptap/components/attachment/index.tsx b/packages/client/src/components/tiptap/components/attachment/index.tsx index 726a2d77..3f1de1d7 100644 --- a/packages/client/src/components/tiptap/components/attachment/index.tsx +++ b/packages/client/src/components/tiptap/components/attachment/index.tsx @@ -49,7 +49,7 @@ const getFileTypeIcon = (type: FileType) => { export const AttachmentWrapper = ({ editor, node, updateAttributes }) => { const $upload = useRef(); const isEditable = editor.isEditable; - const { autoTrigger, fileName, fileSize, fileExt, fileType, url, error } = node.attrs; + const { hasTrigger, fileName, fileSize, fileExt, fileType, url, error } = node.attrs; const [loading, toggleLoading] = useToggle(false); const [visible, toggleVisible] = useToggle(false); @@ -81,11 +81,11 @@ export const AttachmentWrapper = ({ editor, node, updateAttributes }) => { const type = normalizeFileType(fileType); useEffect(() => { - if (!url && !autoTrigger) { + if (!url && !hasTrigger) { selectFile(); - updateAttributes({ autoTrigger: true }); + updateAttributes({ hasTrigger: true }); } - }, [url, autoTrigger]); + }, [url, hasTrigger]); const content = (() => { if (error) { diff --git a/packages/client/src/components/tiptap/components/documentChildren/index.tsx b/packages/client/src/components/tiptap/components/documentChildren/index.tsx index e5258656..32b22498 100644 --- a/packages/client/src/components/tiptap/components/documentChildren/index.tsx +++ b/packages/client/src/components/tiptap/components/documentChildren/index.tsx @@ -8,17 +8,31 @@ import { DataRender } from 'components/data-render'; import { Empty } from 'components/empty'; import { IconDocument } from 'components/icons'; import styles from './index.module.scss'; +import { useEffect } from 'react'; const { Text } = Typography; -export const DocumentChildrenWrapper = ({ editor }) => { +export const DocumentChildrenWrapper = ({ editor, node, updateAttributes }) => { const isEditable = editor.isEditable; const { pathname, query } = useRouter(); - const wikiId = query?.wikiId; - const documentId = query?.documentId; + let { wikiId, documentId } = node.attrs; + if (!wikiId) { + query?.wikiId; + } + if (!documentId) { + documentId = query?.documentId; + } const isShare = pathname.includes('share'); const { data: documents, loading, error } = useChildrenDocument({ wikiId, documentId, isShare }); + useEffect(() => { + const attrs = node.attrs; + + if (attrs.wikiId !== wikiId || attrs.documentId !== documentId) { + updateAttributes({ wikiId, documentId }); + } + }, [node.attrs, wikiId, documentId]); + return (
diff --git a/packages/client/src/components/tiptap/components/iframe/index.tsx b/packages/client/src/components/tiptap/components/iframe/index.tsx index 061377f1..c90c1ea6 100644 --- a/packages/client/src/components/tiptap/components/iframe/index.tsx +++ b/packages/client/src/components/tiptap/components/iframe/index.tsx @@ -7,6 +7,8 @@ export const IframeWrapper = ({ editor, node, updateAttributes }) => { const isEditable = editor.isEditable; const { url, width, height } = node.attrs; + console.log('render iframe', node.attrs); + const onResize = (size) => { updateAttributes({ width: size.width, height: size.height }); }; diff --git a/packages/client/src/components/tiptap/components/image/index.tsx b/packages/client/src/components/tiptap/components/image/index.tsx index 39a6f66f..e519847e 100644 --- a/packages/client/src/components/tiptap/components/image/index.tsx +++ b/packages/client/src/components/tiptap/components/image/index.tsx @@ -11,7 +11,7 @@ const { Text } = Typography; export const ImageWrapper = ({ editor, node, updateAttributes }) => { const isEditable = editor.isEditable; - const { autoTrigger, error, src, alt, title, width, height, textAlign } = node.attrs; + const { hasTrigger, error, src, alt, title, width, height, textAlign } = node.attrs; const $upload = useRef(); const [loading, toggleLoading] = useToggle(false); @@ -45,11 +45,11 @@ export const ImageWrapper = ({ editor, node, updateAttributes }) => { }; useEffect(() => { - if (!src && !autoTrigger) { + if (!src && !hasTrigger) { selectFile(); - updateAttributes({ autoTrigger: true }); + updateAttributes({ hasTrigger: true }); } - }, [src, autoTrigger]); + }, [src, hasTrigger]); const content = (() => { if (error) { diff --git a/packages/client/src/components/tiptap/components/katex/index.tsx b/packages/client/src/components/tiptap/components/katex/index.tsx index 47d74585..76c41ae7 100644 --- a/packages/client/src/components/tiptap/components/katex/index.tsx +++ b/packages/client/src/components/tiptap/components/katex/index.tsx @@ -11,8 +11,6 @@ export const KatexWrapper = ({ editor, node, updateAttributes }) => { const isEditable = editor.isEditable; const { text } = node.attrs; - console.log(node.attrs); - const formatText = useMemo(() => { try { return katex.renderToString(`${text}`); @@ -27,10 +25,6 @@ export const KatexWrapper = ({ editor, node, updateAttributes }) => { 点击输入公式 ); - // useEffect(() => { - // updateAttributes(node.attrs); - // }, []); - return ( {isEditable ? ( diff --git a/packages/client/src/components/tiptap/extensions/attachment.ts b/packages/client/src/components/tiptap/extensions/attachment.ts index 2cd406e7..9cabf05c 100644 --- a/packages/client/src/components/tiptap/extensions/attachment.ts +++ b/packages/client/src/components/tiptap/extensions/attachment.ts @@ -1,6 +1,7 @@ import { Node, mergeAttributes } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { AttachmentWrapper } from '../components/attachment'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -38,27 +39,35 @@ export const Attachment = Node.create({ return { fileName: { default: null, + parseHTML: getDatasetAttribute('filename'), }, fileSize: { default: null, + parseHTML: getDatasetAttribute('filesize'), }, fileType: { default: null, + parseHTML: getDatasetAttribute('filetype'), }, fileExt: { default: null, + parseHTML: getDatasetAttribute('fileext'), }, url: { default: null, + parseHTML: getDatasetAttribute('url'), }, - autoTrigger: { + hasTrigger: { default: false, + parseHTML: (element) => getDatasetAttribute('hastrigger')(element) === 'true', }, error: { default: null, + parseHTML: getDatasetAttribute('error'), }, }; }, + // @ts-ignore addCommands() { return { diff --git a/packages/client/src/components/tiptap/extensions/banner.ts b/packages/client/src/components/tiptap/extensions/banner.ts index 57bef047..c0e80480 100644 --- a/packages/client/src/components/tiptap/extensions/banner.ts +++ b/packages/client/src/components/tiptap/extensions/banner.ts @@ -2,6 +2,7 @@ import { Node, Command, mergeAttributes, wrappingInputRule } from '@tiptap/core' import { ReactNodeViewRenderer } from '@tiptap/react'; import { BannerWrapper } from '../components/banner'; import { typesAvailable } from '../services/markdown/markdownToHTML/markdownBanner'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -17,24 +18,15 @@ export const Banner = Node.create({ group: 'block', defining: true, - addOptions() { - return { - types: typesAvailable, - HTMLAttributes: { - class: 'banner', - }, - }; - }, - addAttributes() { return { type: { default: 'info', rendered: false, - parseHTML: (element) => element.getAttribute('data-banner'), + parseHTML: getDatasetAttribute('info'), renderHTML: (attributes) => { return { - 'data-banner': attributes.type, + 'data-type': attributes.type, 'class': `banner banner-${attributes.type}`, }; }, @@ -42,6 +34,14 @@ export const Banner = Node.create({ }; }, + addOptions() { + return { + HTMLAttributes: { + class: 'banner', + }, + }; + }, + parseHTML() { return [ { @@ -50,16 +50,8 @@ export const Banner = Node.create({ ]; }, - renderHTML({ node, HTMLAttributes }) { - const { class: classy } = this.options.HTMLAttributes; - - const attributes = { - ...this.options.HTMLAttributes, - 'data-callout': node.attrs.type, - 'class': `${classy} ${classy}-${node.attrs.type}`, - }; - - return ['div', mergeAttributes(attributes, HTMLAttributes), 0]; + renderHTML({ HTMLAttributes }) { + return ['div', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]; }, // @ts-ignore diff --git a/packages/client/src/components/tiptap/extensions/blockquote.ts b/packages/client/src/components/tiptap/extensions/blockquote.ts index 4e8628e2..71d6c9bc 100644 --- a/packages/client/src/components/tiptap/extensions/blockquote.ts +++ b/packages/client/src/components/tiptap/extensions/blockquote.ts @@ -1,7 +1,7 @@ import { Blockquote as BuiltInBlockquote } from '@tiptap/extension-blockquote'; import { wrappingInputRule } from '@tiptap/core'; import { getParents } from '../services/dom'; -import { getMarkdownSource } from '../services/markdown/markdownSourceMap'; +import { getMarkdownSource } from '../services/markdown'; export const Blockquote = BuiltInBlockquote.extend({ addAttributes() { diff --git a/packages/client/src/components/tiptap/extensions/bulletList.ts b/packages/client/src/components/tiptap/extensions/bulletList.ts index 4fa3bc29..306d6e15 100644 --- a/packages/client/src/components/tiptap/extensions/bulletList.ts +++ b/packages/client/src/components/tiptap/extensions/bulletList.ts @@ -1,5 +1,5 @@ import { BulletList as BuiltInBulletList } from '@tiptap/extension-bullet-list'; -import { getMarkdownSource } from '../services/markdown/markdownSourceMap'; +import { getMarkdownSource } from '../services/markdown'; import { listInputRule } from '../services/listInputRule'; export const BulletList = BuiltInBulletList.extend({ diff --git a/packages/client/src/components/tiptap/extensions/codeBlock.ts b/packages/client/src/components/tiptap/extensions/codeBlock.ts index 93c9a8bb..962a0547 100644 --- a/packages/client/src/components/tiptap/extensions/codeBlock.ts +++ b/packages/client/src/components/tiptap/extensions/codeBlock.ts @@ -1,37 +1,324 @@ -import { CodeBlockLowlight } from '@tiptap/extension-code-block-lowlight'; -import { ReactNodeViewRenderer } from '@tiptap/react'; import { lowlight } from 'lowlight/lib/all'; +import { Node, textblockTypeInputRule, mergeAttributes } from '@tiptap/core'; +import { Plugin, PluginKey, TextSelection } from 'prosemirror-state'; +import { ReactNodeViewRenderer } from '@tiptap/react'; +import { LowlightPlugin } from '../services/lowlightPlugin'; import { CodeBlockWrapper } from '../components/codeBlock'; -const extractLanguage = (element) => element.getAttribute('lang'); +export interface CodeBlockOptions { + /** + * Adds a prefix to language classes that are applied to code tags. + * Defaults to `'language-'`. + */ + languageClassPrefix: string; + /** + * Define whether the node should be exited on triple enter. + * Defaults to `true`. + */ + exitOnTripleEnter: boolean; + /** + * Define whether the node should be exited on arrow down if there is no node after it. + * Defaults to `true`. + */ + exitOnArrowDown: boolean; + /** + * Custom HTML attributes that should be added to the rendered HTML tag. + */ + HTMLAttributes: Record; +} -export const CodeBlock = CodeBlockLowlight.extend({ - isolating: true, +declare module '@tiptap/core' { + interface Commands { + codeBlock: { + /** + * Set a code block + */ + setCodeBlock: (attributes?: { language: string }) => ReturnType; + /** + * Toggle a code block + */ + toggleCodeBlock: (attributes?: { language: string }) => ReturnType; + }; + } +} + +export const backtickInputRegex = /^```([a-z]+)?[\s\n]$/; +export const tildeInputRegex = /^~~~([a-z]+)?[\s\n]$/; + +export const BuiltInCodeBlock = Node.create({ + name: 'codeBlock', + + addOptions() { + return { + languageClassPrefix: 'language-', + exitOnTripleEnter: true, + exitOnArrowDown: true, + HTMLAttributes: {}, + }; + }, + + content: 'text*', + + marks: '', + + group: 'block', + + code: true, + + defining: true, addAttributes() { return { language: { default: null, - parseHTML: (element) => extractLanguage(element), - }, - class: { - default: 'code highlight', + parseHTML: (element) => { + const { languageClassPrefix } = this.options; + const classNames = Array.from(element.firstElementChild?.classList || element.classList || []); + const languages = classNames + .filter((className) => className.startsWith(languageClassPrefix)) + .map((className) => className.replace(languageClassPrefix, '')); + const language = languages[0]; + + if (!language) { + return null; + } + + return language; + }, + rendered: false, }, }; }, - renderHTML({ HTMLAttributes }) { + + parseHTML() { return [ - 'pre', { - ...HTMLAttributes, - class: `content-editor-code-block ${HTMLAttributes.class}`, + tag: 'pre', + preserveWhitespace: 'full', }, - ['code', {}, 0], ]; }, + + renderHTML({ node, HTMLAttributes }) { + return [ + 'pre', + mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), + [ + 'code', + { + class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null, + }, + 0, + ], + ]; + }, + + addCommands() { + return { + setCodeBlock: + (attributes) => + ({ commands }) => { + return commands.setNode(this.name, attributes); + }, + toggleCodeBlock: + (attributes) => + ({ commands }) => { + return commands.toggleNode(this.name, 'paragraph', attributes); + }, + }; + }, + + addKeyboardShortcuts() { + return { + 'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(), + + // remove code block when at start of document or code block is empty + 'Backspace': () => { + const { empty, $anchor } = this.editor.state.selection; + const isAtStart = $anchor.pos === 1; + + if (!empty || $anchor.parent.type.name !== this.name) { + return false; + } + + if (isAtStart || !$anchor.parent.textContent.length) { + return this.editor.commands.clearNodes(); + } + + return false; + }, + + // exit node on triple enter + 'Enter': ({ editor }) => { + if (!this.options.exitOnTripleEnter) { + return false; + } + + const { state } = editor; + const { selection } = state; + const { $from, empty } = selection; + + if (!empty || $from.parent.type !== this.type) { + return false; + } + + const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2; + const endsWithDoubleNewline = $from.parent.textContent.endsWith('\n\n'); + + if (!isAtEnd || !endsWithDoubleNewline) { + return false; + } + + return editor + .chain() + .command(({ tr }) => { + tr.delete($from.pos - 2, $from.pos); + + return true; + }) + .exitCode() + .run(); + }, + + // exit node on arrow down + 'ArrowDown': ({ editor }) => { + if (!this.options.exitOnArrowDown) { + return false; + } + + const { state } = editor; + const { selection, doc } = state; + const { $from, empty } = selection; + + if (!empty || $from.parent.type !== this.type) { + return false; + } + + const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2; + + if (!isAtEnd) { + return false; + } + + const after = $from.after(); + + if (after === undefined) { + return false; + } + + const nodeAfter = doc.nodeAt(after); + + if (nodeAfter) { + return false; + } + + return editor.commands.exitCode(); + }, + }; + }, + + addInputRules() { + return [ + textblockTypeInputRule({ + find: backtickInputRegex, + type: this.type, + getAttributes: (match) => ({ + language: match[1], + }), + }), + textblockTypeInputRule({ + find: tildeInputRegex, + type: this.type, + getAttributes: (match) => ({ + language: match[1], + }), + }), + ]; + }, + + addProseMirrorPlugins() { + return [ + // this plugin creates a code block for pasted content from VS Code + // we can also detect the copied code language + new Plugin({ + key: new PluginKey('codeBlockVSCodeHandler'), + props: { + handlePaste: (view, event) => { + if (!event.clipboardData) { + return false; + } + + // don’t create a new code block within code blocks + if (this.editor.isActive(this.type.name)) { + return false; + } + + const text = event.clipboardData.getData('text/plain'); + const vscode = event.clipboardData.getData('vscode-editor-data'); + const vscodeData = vscode ? JSON.parse(vscode) : undefined; + const language = vscodeData?.mode; + + if (!text || !language) { + return false; + } + + const { tr } = view.state; + + // create an empty code block + tr.replaceSelectionWith(this.type.create({ language })); + + // put cursor inside the newly created code block + tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2)))); + + // add text to code block + // strip carriage return chars from text pasted as code + // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd + tr.insertText(text.replace(/\r\n?/g, '\n')); + + // store meta information + // this is useful for other plugins that depends on the paste event + // like the paste rule plugin + tr.setMeta('paste', true); + + view.dispatch(tr); + + return true; + }, + }, + }), + ]; + }, +}); + +export interface CodeBlockLowlightOptions extends CodeBlockOptions { + lowlight: any; + defaultLanguage: string | null | undefined; +} + +export const CodeBlock = BuiltInCodeBlock.extend({ + addOptions() { + return { + ...this.parent?.(), + lowlight, + defaultLanguage: null, + }; + }, + + addProseMirrorPlugins() { + return [ + ...(this.parent?.() || []), + LowlightPlugin({ + name: this.name, + lowlight: this.options.lowlight, + defaultLanguage: this.options.defaultLanguage, + }), + ]; + }, + addNodeView() { return ReactNodeViewRenderer(CodeBlockWrapper); }, }).configure({ lowlight, + defaultLanguage: 'auto', }); diff --git a/packages/client/src/components/tiptap/extensions/documentChildren.ts b/packages/client/src/components/tiptap/extensions/documentChildren.ts index 5609c2f8..cfae46de 100644 --- a/packages/client/src/components/tiptap/extensions/documentChildren.ts +++ b/packages/client/src/components/tiptap/extensions/documentChildren.ts @@ -1,6 +1,7 @@ import { Node, mergeAttributes, wrappingInputRule } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { DocumentChildrenWrapper } from '../components/documentChildren'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -21,23 +22,35 @@ export const DocumentChildren = Node.create({ addAttributes() { return { - color: { - default: 'grey', - }, - text: { + wikiId: { default: '', + parseHTML: getDatasetAttribute('wikiId'), + }, + documentId: { + default: '', + parseHTML: getDatasetAttribute('documentId'), + }, + }; + }, + addOptions() { + return { + HTMLAttributes: { + class: 'documentChildren', }, }; }, parseHTML() { - return [{ tag: 'div[data-type=documentChildren]' }]; + return [ + { + tag: 'div', + }, + ]; }, renderHTML({ HTMLAttributes }) { - return ['div', mergeAttributes((this.options && this.options.HTMLAttributes) || {}, HTMLAttributes)]; + return ['div', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]; }, - // @ts-ignore addCommands() { return { diff --git a/packages/client/src/components/tiptap/extensions/documentReference.ts b/packages/client/src/components/tiptap/extensions/documentReference.ts index 08d09a33..179afb47 100644 --- a/packages/client/src/components/tiptap/extensions/documentReference.ts +++ b/packages/client/src/components/tiptap/extensions/documentReference.ts @@ -1,6 +1,7 @@ import { Node, mergeAttributes, wrappingInputRule } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { DocumentReferenceWrapper } from '../components/documentReference'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -23,22 +24,37 @@ export const DocumentReference = Node.create({ return { wikiId: { default: '', + parseHTML: getDatasetAttribute('wikiId'), }, documentId: { default: '', + parseHTML: getDatasetAttribute('documentId'), }, title: { default: '', + parseHTML: getDatasetAttribute('title'), + }, + }; + }, + + addOptions() { + return { + HTMLAttributes: { + class: 'documentReference', }, }; }, parseHTML() { - return [{ tag: 'div[data-type=documentReference]' }]; + return [ + { + tag: 'div', + }, + ]; }, renderHTML({ HTMLAttributes }) { - return ['div', mergeAttributes((this.options && this.options.HTMLAttributes) || {}, HTMLAttributes)]; + return ['div', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]; }, // @ts-ignore diff --git a/packages/client/src/components/tiptap/extensions/footnoteDefinition.ts b/packages/client/src/components/tiptap/extensions/footnoteDefinition.ts deleted file mode 100644 index 4c8d96a4..00000000 --- a/packages/client/src/components/tiptap/extensions/footnoteDefinition.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { mergeAttributes, Node } from '@tiptap/core'; -import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants'; - -export const FootnoteDefinition = Node.create({ - name: 'footnoteDefinition', - - content: 'paragraph', - - group: 'block', - - parseHTML() { - return [ - { tag: 'section.footnotes li' }, - { tag: '.footnote-backref', priority: PARSE_HTML_PRIORITY_HIGHEST, ignore: true }, - ]; - }, - - renderHTML({ HTMLAttributes }) { - return ['li', mergeAttributes(HTMLAttributes), 0]; - }, -}); diff --git a/packages/client/src/components/tiptap/extensions/footnoteReference.ts b/packages/client/src/components/tiptap/extensions/footnoteReference.ts deleted file mode 100644 index 91b30568..00000000 --- a/packages/client/src/components/tiptap/extensions/footnoteReference.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Node, mergeAttributes } from '@tiptap/core'; -import { PARSE_HTML_PRIORITY_HIGHEST } from '../constants'; - -export const FootnoteReference = Node.create({ - name: 'footnoteReference', - - inline: true, - - group: 'inline', - - atom: true, - - draggable: true, - - selectable: true, - - addAttributes() { - return { - footnoteId: { - default: null, - parseHTML: (element) => element.querySelector('a').getAttribute('id'), - }, - footnoteNumber: { - default: null, - parseHTML: (element) => element.textContent, - }, - }; - }, - - parseHTML() { - return [{ tag: 'sup.footnote-ref', priority: PARSE_HTML_PRIORITY_HIGHEST }]; - }, - - renderHTML({ HTMLAttributes: { footnoteNumber, footnoteId, ...HTMLAttributes } }) { - return ['sup', mergeAttributes(HTMLAttributes), footnoteNumber]; - }, -}); diff --git a/packages/client/src/components/tiptap/extensions/footnotesSection.ts b/packages/client/src/components/tiptap/extensions/footnotesSection.ts deleted file mode 100644 index 78f388ab..00000000 --- a/packages/client/src/components/tiptap/extensions/footnotesSection.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { mergeAttributes, Node } from '@tiptap/core'; - -export const FootnotesSection = Node.create({ - name: 'footnotesSection', - - content: 'footnoteDefinition+', - - group: 'block', - - isolating: true, - - parseHTML() { - return [{ tag: 'section.footnotes > ol' }]; - }, - - renderHTML({ HTMLAttributes }) { - return ['ol', mergeAttributes(HTMLAttributes, { class: 'footnotes gl-font-sm' }), 0]; - }, -}); diff --git a/packages/client/src/components/tiptap/extensions/htmlMarks.ts b/packages/client/src/components/tiptap/extensions/htmlMarks.ts index 0dfd2e36..889c1b8b 100644 --- a/packages/client/src/components/tiptap/extensions/htmlMarks.ts +++ b/packages/client/src/components/tiptap/extensions/htmlMarks.ts @@ -2,24 +2,7 @@ import { Mark, mergeAttributes, markInputRule } from '@tiptap/core'; import { PARSE_HTML_PRIORITY_LOWEST } from '../constants'; import { markInputRegex, extractMarkAttributesFromMatch } from '../services/markUtils'; -const marks = [ - 'ins', - 'abbr', - 'bdo', - 'cite', - 'dfn', - 'mark', - 'small', - 'span', - 'time', - 'kbd', - 'q', - 'samp', - 'var', - 'ruby', - 'rp', - 'rt', -]; +export const marks = [{ name: 'underline', tag: 'u' }]; const attrs = { time: ['datetime'], @@ -28,9 +11,10 @@ const attrs = { bdo: ['dir'], }; -export const HTMLMarks = marks.map((name) => +export const HTMLMarks = marks.map(({ name, tag }) => Mark.create({ name, + tag, inclusive: false, addOptions() { return { @@ -51,17 +35,17 @@ export const HTMLMarks = marks.map((name) => }, parseHTML() { - return [{ tag: name, priority: PARSE_HTML_PRIORITY_LOWEST }]; + return [{ tag: tag, priority: PARSE_HTML_PRIORITY_LOWEST }]; }, renderHTML({ HTMLAttributes }) { - return [name, mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; + return [tag, mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; }, addInputRules() { return [ markInputRule({ - find: markInputRegex(name), + find: markInputRegex(tag), type: this.type, getAttributes: extractMarkAttributesFromMatch, }), diff --git a/packages/client/src/components/tiptap/extensions/iframe.ts b/packages/client/src/components/tiptap/extensions/iframe.ts index bdd337e8..109d400d 100644 --- a/packages/client/src/components/tiptap/extensions/iframe.ts +++ b/packages/client/src/components/tiptap/extensions/iframe.ts @@ -1,6 +1,7 @@ import { Node, mergeAttributes } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { IframeWrapper } from '../components/iframe'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -11,7 +12,7 @@ declare module '@tiptap/core' { } export const Iframe = Node.create({ - name: 'external-iframe', + name: 'iframe', content: '', marks: '', group: 'block', @@ -21,7 +22,7 @@ export const Iframe = Node.create({ addOptions() { return { HTMLAttributes: { - 'data-type': 'external-iframe', + class: 'iframe', }, }; }, @@ -30,12 +31,15 @@ export const Iframe = Node.create({ return { width: { default: '100%', + parseHTML: getDatasetAttribute('width'), }, height: { - default: 54, + default: 200, + parseHTML: getDatasetAttribute('height'), }, url: { default: null, + parseHTML: getDatasetAttribute('url'), }, }; }, @@ -43,7 +47,7 @@ export const Iframe = Node.create({ parseHTML() { return [ { - tag: 'iframe[data-type="external-iframe"]', + tag: 'iframe', }, ]; }, diff --git a/packages/client/src/components/tiptap/extensions/image.ts b/packages/client/src/components/tiptap/extensions/image.ts index 00325d7b..4a308859 100644 --- a/packages/client/src/components/tiptap/extensions/image.ts +++ b/packages/client/src/components/tiptap/extensions/image.ts @@ -51,7 +51,7 @@ export const Image = BuiltInImage.extend({ height: { default: 'auto', }, - autoTrigger: { + hasTrigger: { default: false, }, error: { diff --git a/packages/client/src/components/tiptap/extensions/mind.ts b/packages/client/src/components/tiptap/extensions/mind.ts index f55d47fc..4831927b 100644 --- a/packages/client/src/components/tiptap/extensions/mind.ts +++ b/packages/client/src/components/tiptap/extensions/mind.ts @@ -1,6 +1,8 @@ import { Node, mergeAttributes, nodeInputRule } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; +import { safeJSONParse } from 'helpers/json'; import { MindWrapper } from '../components/mind'; +import { getDatasetAttribute } from '../services/dataset'; const DEFAULT_MIND_DATA = { meta: { @@ -21,31 +23,34 @@ declare module '@tiptap/core' { } export const Mind = Node.create({ - name: 'jsmind', + name: 'mind', content: '', marks: '', group: 'block', draggable: true, atom: true, - addOptions() { - return { - HTMLAttributes: { - 'data-type': 'jsmind', - }, - }; - }, - addAttributes() { return { width: { default: '100%', + parseHTML: getDatasetAttribute('width'), }, height: { default: 240, + parseHTML: getDatasetAttribute('height'), }, data: { default: DEFAULT_MIND_DATA, + parseHTML: getDatasetAttribute('data', true), + }, + }; + }, + + addOptions() { + return { + HTMLAttributes: { + class: 'mind', }, }; }, @@ -53,7 +58,7 @@ export const Mind = Node.create({ parseHTML() { return [ { - tag: 'div[data-type="jsmind"]', + tag: 'div', }, ]; }, diff --git a/packages/client/src/components/tiptap/extensions/orderedList.ts b/packages/client/src/components/tiptap/extensions/orderedList.ts index c0746f85..c2b478e2 100644 --- a/packages/client/src/components/tiptap/extensions/orderedList.ts +++ b/packages/client/src/components/tiptap/extensions/orderedList.ts @@ -1,5 +1,5 @@ import { OrderedList as BuiltInOrderedList } from '@tiptap/extension-ordered-list'; -import { getMarkdownSource } from '../services/markdown/markdownSourceMap'; +import { getMarkdownSource } from '../services/markdown'; export const OrderedList = BuiltInOrderedList.extend({ addAttributes() { diff --git a/packages/client/src/components/tiptap/extensions/paste.ts b/packages/client/src/components/tiptap/extensions/paste.ts index e10401ac..183d8efc 100644 --- a/packages/client/src/components/tiptap/extensions/paste.ts +++ b/packages/client/src/components/tiptap/extensions/paste.ts @@ -1,10 +1,14 @@ import { Extension } from '@tiptap/core'; import { Plugin, PluginKey } from 'prosemirror-state'; -import { markdownSerializer } from '../services/markdown/serializer'; import { EXTENSION_PRIORITY_HIGHEST } from '../constants'; import { handleFileEvent } from '../services/upload'; import { isInCode, LANGUAGES } from '../services/code'; -import { isMarkdown, normalizePastedMarkdown } from '../services/markdown/helpers'; +import { + isMarkdown, + normalizePastedMarkdown, + markdownToProsemirror, + prosemirrorToMarkdown, +} from '../services/markdown'; import { isTitleNode } from '../services/node'; export const Paste = Extension.create({ @@ -40,7 +44,7 @@ export const Paste = Extension.create({ // 粘贴代码 if (isInCode(view.state)) { event.preventDefault(); - view.dispatch(view.state.tr.insertText(text)); + view.dispatch(view.state.tr.insertText(text).scrollIntoView()); return true; } @@ -56,7 +60,7 @@ export const Paste = Extension.create({ }) ) ); - view.dispatch(view.state.tr.insertText(text)); + view.dispatch(view.state.tr.insertText(text).scrollIntoView()); return true; } @@ -66,14 +70,19 @@ export const Paste = Extension.create({ const firstNode = view.props.state.doc.content.firstChild; const hasTitle = isTitleNode(firstNode) && firstNode.content.size > 0; const schema = view.props.state.schema; - const doc = markdownSerializer.markdownToProsemirror({ + const doc = markdownToProsemirror({ schema, content: normalizePastedMarkdown(text), hasTitle, }); - // @ts-ignore - const transaction = view.state.tr.insert(view.state.selection.head, view.state.schema.nodeFromJSON(doc)); - view.dispatch(transaction); + let tr = view.state.tr; + const selection = tr.selection; + view.state.doc.nodesBetween(selection.from, selection.to, (node, position) => { + const startPosition = hasTitle ? Math.min(position, selection.from) : 0; + const endPosition = Math.min(position + node.nodeSize, selection.to); + tr = tr.replaceWith(startPosition, endPosition, view.state.schema.nodeFromJSON(doc)); + }); + view.dispatch(tr.scrollIntoView()); return true; } @@ -111,8 +120,8 @@ export const Paste = Extension.create({ if (!doc) { return ''; } - const content = markdownSerializer.proseMirrorToMarkdown({ - schema: this.editor.schema, + + const content = prosemirrorToMarkdown({ content: doc, }); diff --git a/packages/client/src/components/tiptap/extensions/status.ts b/packages/client/src/components/tiptap/extensions/status.ts index 50676708..00107d97 100644 --- a/packages/client/src/components/tiptap/extensions/status.ts +++ b/packages/client/src/components/tiptap/extensions/status.ts @@ -1,6 +1,7 @@ import { Node, mergeAttributes } from '@tiptap/core'; import { ReactNodeViewRenderer } from '@tiptap/react'; import { StatusWrapper } from '../components/status'; +import { getDatasetAttribute } from '../services/dataset'; declare module '@tiptap/core' { interface Commands { @@ -20,19 +21,33 @@ export const Status = Node.create({ return { color: { default: 'grey', + parseHTML: getDatasetAttribute('color'), }, text: { default: '', + parseHTML: getDatasetAttribute('text'), + }, + }; + }, + + addOptions() { + return { + HTMLAttributes: { + class: 'status', }, }; }, parseHTML() { - return [{ tag: 'span[data-type=status]' }]; + return [ + { + tag: 'div', + }, + ]; }, renderHTML({ HTMLAttributes }) { - return ['span', mergeAttributes((this.options && this.options.HTMLAttributes) || {}, HTMLAttributes)]; + return ['div', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]; }, // @ts-ignore diff --git a/packages/client/src/components/tiptap/extensions/table.ts b/packages/client/src/components/tiptap/extensions/table.ts index 097b4dec..7c2b052c 100644 --- a/packages/client/src/components/tiptap/extensions/table.ts +++ b/packages/client/src/components/tiptap/extensions/table.ts @@ -1,5 +1,56 @@ +import { mergeAttributes } from '@tiptap/core'; import { Table as BuiltInTable } from '@tiptap/extension-table'; -export const Table = BuiltInTable.configure({ - resizable: false, +export const Table = BuiltInTable.extend({ + addAttributes() { + return { + style: { + default: null, + }, + }; + }, + renderHTML({ node, HTMLAttributes }) { + let totalWidth = 0; + let fixedWidth = true; + + try { + // use first row to determine width of table; + // @ts-ignore + const tr = node.content.content[0]; + tr.content.content.forEach((td) => { + if (td.attrs.colwidth) { + td.attrs.colwidth.forEach((col) => { + if (!col) { + fixedWidth = false; + totalWidth += this.options.cellMinWidth; + } else { + totalWidth += col; + } + }); + } else { + fixedWidth = false; + const colspan = td.attrs.colspan ? td.attrs.colspan : 1; + totalWidth += this.options.cellMinWidth * colspan; + } + }); + } catch (error) { + fixedWidth = false; + } + + if (fixedWidth && totalWidth > 0) { + HTMLAttributes.style = `width: ${totalWidth}px;`; + } else if (totalWidth && totalWidth > 0) { + HTMLAttributes.style = `min-width: ${totalWidth}px`; + } else { + HTMLAttributes.style = null; + } + + return [ + 'div', + { class: 'tableWrapper' }, + ['table', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), ['tbody', 0]], + ]; + }, +}).configure({ + resizable: true, }); diff --git a/packages/client/src/components/tiptap/extensions/tableCell.tsx b/packages/client/src/components/tiptap/extensions/tableCell.tsx index 03ba72f0..f3f80654 100644 --- a/packages/client/src/components/tiptap/extensions/tableCell.tsx +++ b/packages/client/src/components/tiptap/extensions/tableCell.tsx @@ -1,6 +1,7 @@ import ReactDOM from 'react-dom'; import { Button } from '@douyinfe/semi-ui'; import { IconDelete, IconPlus } from '@douyinfe/semi-icons'; +import { mergeAttributes } from '@tiptap/core'; import { TableCell as BuiltInTableCell } from '@tiptap/extension-table-cell'; import { Tooltip } from 'components/tooltip'; import { Plugin, PluginKey } from 'prosemirror-state'; @@ -16,6 +17,56 @@ import { import { FloatMenuView } from '../views/floatMenuView'; export const TableCell = BuiltInTableCell.extend({ + addAttributes() { + return { + colspan: { + default: 1, + }, + rowspan: { + default: 1, + }, + colwidth: { + default: null, + parseHTML: (element) => { + const colwidth = element.getAttribute('colwidth'); + const value = colwidth ? colwidth.split(',').map((item) => parseInt(item, 10)) : null; + + return value; + }, + }, + style: { + default: null, + }, + }; + }, + + renderHTML({ HTMLAttributes }) { + let totalWidth = 0; + let fixedWidth = true; + + if (HTMLAttributes.colwidth) { + HTMLAttributes.colwidth.forEach((col) => { + if (!col) { + fixedWidth = false; + } else { + totalWidth += col; + } + }); + } else { + fixedWidth = false; + } + + if (fixedWidth && totalWidth > 0) { + HTMLAttributes.style = `width: ${totalWidth}px;`; + } else if (totalWidth && totalWidth > 0) { + HTMLAttributes.style = `min-width: ${totalWidth}px`; + } else { + HTMLAttributes.style = null; + } + + return ['td', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; + }, + addProseMirrorPlugins() { const extensionThis = this; let selectedRowIndex = -1; diff --git a/packages/client/src/components/tiptap/extensions/tableHeader.tsx b/packages/client/src/components/tiptap/extensions/tableHeader.tsx index c891b2be..acd512c5 100644 --- a/packages/client/src/components/tiptap/extensions/tableHeader.tsx +++ b/packages/client/src/components/tiptap/extensions/tableHeader.tsx @@ -8,7 +8,62 @@ import { Decoration, DecorationSet } from 'prosemirror-view'; import { getCellsInRow, isColumnSelected, isTableSelected, selectColumn } from '../services/table'; import { FloatMenuView } from '../views/floatMenuView'; +// @flow +/* eslint-disable no-unused-vars */ +import { mergeAttributes } from '@tiptap/core'; +// import TableHeader from "@tiptap/extension-table-header"; + export const TableHeader = BuiltInTableHeader.extend({ + addAttributes() { + return { + colspan: { + default: 1, + }, + rowspan: { + default: 1, + }, + colwidth: { + default: null, + parseHTML: (element) => { + const colwidth = element.getAttribute('colwidth'); + const value = colwidth ? colwidth.split(',').map((item) => parseInt(item, 10)) : null; + + return value; + }, + }, + style: { + default: null, + }, + }; + }, + + renderHTML({ HTMLAttributes }) { + let totalWidth = 0; + let fixedWidth = true; + + if (HTMLAttributes.colwidth) { + HTMLAttributes.colwidth.forEach((col) => { + if (!col) { + fixedWidth = false; + } else { + totalWidth += col; + } + }); + } else { + fixedWidth = false; + } + + if (fixedWidth && totalWidth > 0) { + HTMLAttributes.style = `width: ${totalWidth}px;`; + } else if (totalWidth && totalWidth > 0) { + HTMLAttributes.style = `min-width: ${totalWidth}px`; + } else { + HTMLAttributes.style = null; + } + + return ['th', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; + }, + addProseMirrorPlugins() { const extensionThis = this; @@ -21,7 +76,7 @@ export const TableHeader = BuiltInTableHeader.extend({ tippyOptions: { zIndex: 100, }, - shouldShow: ({ editor }) => { + shouldShow: ({ editor }, floatMenuView) => { if (!editor.isEditable) { return false; } @@ -30,6 +85,12 @@ export const TableHeader = BuiltInTableHeader.extend({ return false; } const cells = getCellsInRow(0)(selection); + + if (cells && cells[0]) { + const node = editor.view.nodeDOM(cells[0].pos) as HTMLElement; + floatMenuView.setConatiner(node.parentElement.parentElement.parentElement.parentElement); + } + return !!cells?.some((cell, index) => isColumnSelected(index)(selection)); }, init: (dom, editor) => { diff --git a/packages/client/src/components/tiptap/extensions/title.tsx b/packages/client/src/components/tiptap/extensions/title.tsx index 319c1724..452ec751 100644 --- a/packages/client/src/components/tiptap/extensions/title.tsx +++ b/packages/client/src/components/tiptap/extensions/title.tsx @@ -1,13 +1,23 @@ import { Node, mergeAttributes } from '@tiptap/core'; -export const Title = Node.create({ +export interface TitleOptions { + HTMLAttributes: Record; +} + +declare module '@tiptap/core' { + interface Commands { + title: { + setTitle: (attributes) => ReturnType; + toggleTitle: (attributes) => ReturnType; + }; + } +} + +export const Title = Node.create({ name: 'title', - content: 'text*', - selectable: true, + content: 'inline*', + group: 'block', defining: true, - inline: false, - group: 'basic', - allowGapCursor: true, addOptions() { return { @@ -16,16 +26,15 @@ export const Title = Node.create({ }, }; }, - parseHTML() { return [ { - tag: 'h1[class=title]', + tag: 'p[class=title]', }, ]; }, renderHTML({ HTMLAttributes }) { - return ['h1', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; + return ['p', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]; }, }); diff --git a/packages/client/src/components/tiptap/menus/baseBubbleMenu.tsx b/packages/client/src/components/tiptap/menus/baseBubbleMenu.tsx index 45a35fb8..d7aa316f 100644 --- a/packages/client/src/components/tiptap/menus/baseBubbleMenu.tsx +++ b/packages/client/src/components/tiptap/menus/baseBubbleMenu.tsx @@ -6,6 +6,7 @@ import { Link } from '../extensions/link'; import { Attachment } from '../extensions/attachment'; import { Image } from '../extensions/image'; import { Banner } from '../extensions/banner'; +import { Status } from '../extensions/status'; import { HorizontalRule } from '../extensions/horizontalRule'; import { Iframe } from '../extensions/iframe'; import { Mind } from '../extensions/mind'; @@ -21,6 +22,7 @@ const OTHER_BUBBLE_MENU_TYPES = [ Attachment.name, Image.name, Banner.name, + Status.name, Iframe.name, Mind.name, Table.name, diff --git a/packages/client/src/components/tiptap/menus/components/paragraph.tsx b/packages/client/src/components/tiptap/menus/components/paragraph.tsx index c42f5564..037c06af 100644 --- a/packages/client/src/components/tiptap/menus/components/paragraph.tsx +++ b/packages/client/src/components/tiptap/menus/components/paragraph.tsx @@ -21,7 +21,7 @@ export const Paragraph = ({ editor }) => { } }, []); - console.log(getCurrentCaretTitle(editor)); + // console.log(getCurrentCaretTitle(editor)); return (