-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97e7506
commit 2d8fc2b
Showing
6 changed files
with
209 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,17 @@ | ||
import { Mappable } from "prosemirror-transform"; | ||
import { Decoration } from "prosemirror-view"; | ||
|
||
import { DOMNode } from "../dom.js"; | ||
|
||
export interface DecorationType { | ||
spec: any; | ||
map( | ||
mapping: Mappable, | ||
span: Decoration, | ||
offset: number, | ||
oldOffset: number | ||
): Decoration | null; | ||
valid(node: Node, span: Decoration): boolean; | ||
eq(other: DecorationType): boolean; | ||
destroy(dom: DOMNode): void; | ||
} |
This file contains 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,79 @@ | ||
import { Mark } from "prosemirror-model"; | ||
import { Mappable } from "prosemirror-transform"; | ||
import { Decoration } from "prosemirror-view"; | ||
import { ComponentType } from "react"; | ||
|
||
import { DecorationType } from "./DecorationType.js"; | ||
|
||
function compareObjs( | ||
a: { [prop: string]: unknown }, | ||
b: { [prop: string]: unknown } | ||
) { | ||
if (a == b) return true; | ||
for (const p in a) if (a[p] !== b[p]) return false; | ||
for (const p in b) if (!(p in a)) return false; | ||
return true; | ||
} | ||
|
||
type ReactWidgetSpec = { | ||
side: number; | ||
marks?: readonly Mark[]; | ||
stopEvent?: (event: Event) => boolean; | ||
ignoreSelection?: boolean; | ||
key?: string; | ||
}; | ||
|
||
const noSpec = {}; | ||
|
||
export class ReactWidgetType implements DecorationType { | ||
// TODO: implement side affinity? | ||
side: number; | ||
spec: ReactWidgetSpec; | ||
|
||
constructor(public Component: ComponentType, spec: ReactWidgetSpec) { | ||
this.spec = spec ?? noSpec; | ||
this.side = this.spec.side ?? 0; | ||
} | ||
|
||
map( | ||
mapping: Mappable, | ||
span: Decoration, | ||
offset: number, | ||
oldOffset: number | ||
): Decoration | null { | ||
const { pos, deleted } = mapping.mapResult( | ||
span.from + oldOffset, | ||
this.side < 0 ? -1 : 1 | ||
); | ||
return deleted | ||
? null | ||
: // @ts-expect-error Decoration constructor args are internal | ||
new Decoration(pos - offset, pos - offset, this); | ||
} | ||
|
||
valid(): boolean { | ||
return true; | ||
} | ||
|
||
eq(other: DecorationType): boolean { | ||
return ( | ||
this == other || | ||
(other instanceof ReactWidgetType && | ||
((this.spec.key && this.spec.key == other.spec.key) || | ||
(this.Component == other.Component && | ||
compareObjs(this.spec, other.spec)))) | ||
); | ||
} | ||
destroy(): void { | ||
// Can be implemented with React effect hooks | ||
} | ||
} | ||
|
||
export function widget( | ||
pos: number, | ||
component: ComponentType, | ||
spec: ReactWidgetSpec | ||
) { | ||
// @ts-expect-error Decoration constructor args are internal | ||
return new Decoration(pos, pos, new ReactWidgetType(component, spec)); | ||
} |
This file contains 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