-
-
Notifications
You must be signed in to change notification settings - Fork 560
refactor: support markviews #1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { Mark } from "prosemirror-model"; | ||
import type { | ||
EditorView, | ||
MarkView, | ||
ViewMutationRecord, | ||
} from "prosemirror-view"; | ||
|
||
import type { BlockNoteEditor } from "@blocknote/core"; | ||
import type { | ||
CoreMarkViewSpec, | ||
CoreMarkViewUserOptions, | ||
MarkViewDOMSpec, | ||
} from "./CoreMarkViewOptions.js"; | ||
|
||
/* eslint-disable curly */ | ||
|
||
export class CoreMarkView<ComponentType> implements MarkView { | ||
dom: HTMLElement; | ||
contentDOM: HTMLElement | undefined; | ||
mark: Mark; | ||
view: EditorView; | ||
inline: boolean; | ||
options: CoreMarkViewUserOptions<ComponentType>; | ||
editor: BlockNoteEditor<any, any, any>; | ||
|
||
#createElement(as?: MarkViewDOMSpec) { | ||
const { inline, mark } = this; | ||
return as == null | ||
? document.createElement(inline ? "span" : "div") | ||
: as instanceof HTMLElement | ||
? as | ||
: as instanceof Function | ||
? as(mark) | ||
: document.createElement(as); | ||
} | ||
|
||
createDOM(as?: MarkViewDOMSpec) { | ||
return this.#createElement(as); | ||
} | ||
|
||
createContentDOM(as?: MarkViewDOMSpec) { | ||
return this.#createElement(as); | ||
} | ||
|
||
constructor({ | ||
mark, | ||
view, | ||
inline, | ||
options, | ||
editor, // BlockNote specific | ||
}: CoreMarkViewSpec<ComponentType>) { | ||
this.mark = mark; | ||
this.view = view; | ||
this.inline = inline; | ||
this.options = options; | ||
this.editor = editor; | ||
|
||
this.dom = this.createDOM(options.as); | ||
this.contentDOM = options.contentAs | ||
? this.createContentDOM(options.contentAs) | ||
: undefined; | ||
this.dom.setAttribute("data-mark-view-root", "true"); | ||
if (this.contentDOM) { | ||
this.contentDOM.setAttribute("data-mark-view-content", "true"); | ||
this.contentDOM.style.whiteSpace = "inherit"; | ||
} | ||
} | ||
|
||
get component() { | ||
return this.options.component; | ||
} | ||
|
||
shouldIgnoreMutation: (mutation: ViewMutationRecord) => boolean = ( | ||
mutation | ||
) => { | ||
if (!this.dom || !this.contentDOM) return true; | ||
|
||
if (mutation.type === "selection") return false; | ||
|
||
if (this.contentDOM === mutation.target && mutation.type === "attributes") | ||
return true; | ||
|
||
if (this.contentDOM.contains(mutation.target)) return false; | ||
|
||
return true; | ||
}; | ||
|
||
ignoreMutation: (mutation: ViewMutationRecord) => boolean = (mutation) => { | ||
if (!this.dom || !this.contentDOM) return true; | ||
|
||
let result; | ||
|
||
const userIgnoreMutation = this.options.ignoreMutation; | ||
|
||
if (userIgnoreMutation) result = userIgnoreMutation(mutation); | ||
|
||
if (typeof result !== "boolean") | ||
result = this.shouldIgnoreMutation(mutation); | ||
|
||
return result; | ||
}; | ||
|
||
public destroy() { | ||
this.options.destroy?.(); | ||
this.dom.remove(); | ||
this.contentDOM?.remove(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/react/src/schema/markviews/CoreMarkViewOptions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { BlockNoteEditor } from "@blocknote/core"; | ||
import type { Mark } from "prosemirror-model"; | ||
import type { EditorView, ViewMutationRecord } from "prosemirror-view"; | ||
|
||
export type MarkViewDOMSpec = | ||
| string | ||
| HTMLElement | ||
| ((mark: Mark) => HTMLElement); | ||
|
||
export interface CoreMarkViewUserOptions<Component> { | ||
// DOM | ||
as?: MarkViewDOMSpec; | ||
contentAs?: MarkViewDOMSpec; | ||
|
||
// Component | ||
component: Component; | ||
|
||
// Overrides | ||
ignoreMutation?: (mutation: ViewMutationRecord) => boolean | void; | ||
destroy?: () => void; | ||
} | ||
|
||
export interface CoreMarkViewSpec<Component> { | ||
mark: Mark; | ||
view: EditorView; | ||
inline: boolean; | ||
|
||
options: CoreMarkViewUserOptions<Component>; | ||
|
||
// BlockNote specific | ||
editor: BlockNoteEditor<any, any, any>; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The implementation of MarkViews in this directory is based on `prosemirror-adapter`. We might want to migrate to the Tiptap implementation of MarkViews once this lands in Tiptap v3. |
16 changes: 16 additions & 0 deletions
16
packages/react/src/schema/markviews/ReactMarkViewOptions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { FC } from "react"; | ||
import type { | ||
CoreMarkViewSpec, | ||
CoreMarkViewUserOptions, | ||
} from "./CoreMarkViewOptions.js"; | ||
|
||
// export type ReactMarkViewComponent = ComponentType<Record<string, never>>; | ||
|
||
export type ReactMarkViewComponent = | ||
| FC<{ contentRef: (el: HTMLElement | null) => void }> | ||
| FC<{ contentRef: (el: HTMLElement | null) => void; value: string }>; | ||
|
||
export type ReactMarkViewSpec = CoreMarkViewSpec<ReactMarkViewComponent>; | ||
|
||
export type ReactMarkViewUserOptions = | ||
CoreMarkViewUserOptions<ReactMarkViewComponent>; |
70 changes: 70 additions & 0 deletions
70
packages/react/src/schema/markviews/ReactMarkViewRenderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { CoreMarkView } from "./CoreMarkView.js"; | ||
import type { MarkViewContext } from "./markViewContext.js"; | ||
import type { ReactMarkViewComponent } from "./ReactMarkViewOptions.js"; | ||
|
||
/* eslint-disable curly */ | ||
|
||
export class ReactMarkView extends CoreMarkView<ReactMarkViewComponent> { | ||
// implements ReactRenderer<MarkViewContext> | ||
// key: string = nanoid(); | ||
id = Math.floor(Math.random() * 0xffffffff).toString(); | ||
|
||
context: MarkViewContext = { | ||
contentRef: (element) => { | ||
if (element && this.contentDOM && element.firstChild !== this.contentDOM) | ||
element.appendChild(this.contentDOM); | ||
}, | ||
view: this.view, | ||
|
||
mark: this.mark, | ||
}; | ||
|
||
updateContext = () => { | ||
Object.assign(this.context, { | ||
mark: this.mark, | ||
}); | ||
}; | ||
|
||
// render = () => { | ||
// const UserComponent = this.component; | ||
|
||
// return createPortal( | ||
// <markViewContext.Provider value={this.context}> | ||
// <UserComponent /> | ||
// </markViewContext.Provider>, | ||
// this.dom, | ||
// this.key | ||
// ); | ||
// }; | ||
|
||
render = () => { | ||
this.editor._tiptapEditor.contentComponent.setRenderer( | ||
this.id, | ||
this.renderer() | ||
); | ||
}; | ||
|
||
destroy = () => { | ||
super.destroy(); | ||
this.editor._tiptapEditor.contentComponent.removeRenderer(this.id); | ||
}; | ||
|
||
renderer = () => { | ||
const UserComponent = this.component; | ||
|
||
const props: any = {}; | ||
|
||
if (this.mark.attrs.stringValue) { | ||
props.value = this.mark.attrs.stringValue; | ||
} | ||
|
||
return { | ||
reactElement: ( | ||
// <markViewContext.Provider value={this.context}> | ||
<UserComponent contentRef={this.context.contentRef} {...props} /> | ||
// </markViewContext.Provider> | ||
), | ||
element: this.dom, | ||
}; | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Mark } from "prosemirror-model"; | ||
import type { EditorView, MarkViewConstructor } from "prosemirror-view"; | ||
import { createContext, useContext } from "react"; | ||
import type { ReactMarkViewUserOptions } from "./ReactMarkViewOptions.js"; | ||
|
||
export type MarkViewContentRef = (node: HTMLElement | null) => void; | ||
|
||
export interface MarkViewContext { | ||
// won't change | ||
contentRef: MarkViewContentRef; | ||
view: EditorView; | ||
mark: Mark; | ||
} | ||
|
||
export const markViewContext = createContext<MarkViewContext>({ | ||
contentRef: () => { | ||
// nothing to do | ||
}, | ||
view: null as never, | ||
mark: null as never, | ||
}); | ||
|
||
export const useMarkViewContext = () => useContext(markViewContext); | ||
|
||
export const createMarkViewContext = createContext< | ||
(options: ReactMarkViewUserOptions) => MarkViewConstructor | ||
>((_options) => { | ||
throw new Error( | ||
"No ProsemirrorAdapterProvider detected, maybe you need to wrap the component with the Editor with ProsemirrorAdapterProvider?" | ||
); | ||
}); | ||
|
||
export const useMarkViewFactory = () => useContext(createMarkViewContext); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this is subtly incompatible with the implementation that I wrote for Tiptap V3: https://github.com/ueberdosis/tiptap/blob/next/packages/react/src/ReactMarkViewRenderer.tsx
May be worth reviewing the differences and seeing if we should upstream some of this or if we should follow the one I wrote for V3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any specific parts that seem incompatible? We don't expose markviews directly (only indirectly via React custom Styles), so I wonder if it's an issue. Of course, I could see if I can swap out the current implementation for the one you linked to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked into it more, specifically some of the CoreMarkViewUserOptions (
as
andcontentAs
), but it does not look like it is even used here so it should be fine.We can deal with this later when we either want to expose this integration or migrate to the Tiptap V3 implementation of it. So, let's put it off until later