index.d.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. import { EditorState, Transaction, Selection, Plugin } from 'prosemirror-state';
  2. import { Mark, Node, ParseRule, Slice, ResolvedPos, DOMParser, DOMSerializer } from 'prosemirror-model';
  3. import { Mapping } from 'prosemirror-transform';
  4. type DOMNode = InstanceType<typeof window.Node>;
  5. type WidgetConstructor = ((view: EditorView, getPos: () => number | undefined) => DOMNode) | DOMNode;
  6. /**
  7. Decoration objects can be provided to the view through the
  8. [`decorations` prop](https://prosemirror.net/docs/ref/#view.EditorProps.decorations). They come in
  9. several variants—see the static members of this class for details.
  10. */
  11. declare class Decoration {
  12. /**
  13. The start position of the decoration.
  14. */
  15. readonly from: number;
  16. /**
  17. The end position. Will be the same as `from` for [widget
  18. decorations](https://prosemirror.net/docs/ref/#view.Decoration^widget).
  19. */
  20. readonly to: number;
  21. /**
  22. Creates a widget decoration, which is a DOM node that's shown in
  23. the document at the given position. It is recommended that you
  24. delay rendering the widget by passing a function that will be
  25. called when the widget is actually drawn in a view, but you can
  26. also directly pass a DOM node. `getPos` can be used to find the
  27. widget's current document position.
  28. */
  29. static widget(pos: number, toDOM: WidgetConstructor, spec?: {
  30. /**
  31. Controls which side of the document position this widget is
  32. associated with. When negative, it is drawn before a cursor
  33. at its position, and content inserted at that position ends
  34. up after the widget. When zero (the default) or positive, the
  35. widget is drawn after the cursor and content inserted there
  36. ends up before the widget.
  37. When there are multiple widgets at a given position, their
  38. `side` values determine the order in which they appear. Those
  39. with lower values appear first. The ordering of widgets with
  40. the same `side` value is unspecified.
  41. When `marks` is null, `side` also determines the marks that
  42. the widget is wrapped in—those of the node before when
  43. negative, those of the node after when positive.
  44. */
  45. side?: number;
  46. /**
  47. The precise set of marks to draw around the widget.
  48. */
  49. marks?: readonly Mark[];
  50. /**
  51. Can be used to control which DOM events, when they bubble out
  52. of this widget, the editor view should ignore.
  53. */
  54. stopEvent?: (event: Event) => boolean;
  55. /**
  56. When set (defaults to false), selection changes inside the
  57. widget are ignored, and don't cause ProseMirror to try and
  58. re-sync the selection with its selection state.
  59. */
  60. ignoreSelection?: boolean;
  61. /**
  62. When comparing decorations of this type (in order to decide
  63. whether it needs to be redrawn), ProseMirror will by default
  64. compare the widget DOM node by identity. If you pass a key,
  65. that key will be compared instead, which can be useful when
  66. you generate decorations on the fly and don't want to store
  67. and reuse DOM nodes. Make sure that any widgets with the same
  68. key are interchangeable—if widgets differ in, for example,
  69. the behavior of some event handler, they should get
  70. different keys.
  71. */
  72. key?: string;
  73. /**
  74. Called when the widget decoration is removed or the editor is
  75. destroyed.
  76. */
  77. destroy?: (node: DOMNode) => void;
  78. /**
  79. Specs allow arbitrary additional properties.
  80. */
  81. [key: string]: any;
  82. }): Decoration;
  83. /**
  84. Creates an inline decoration, which adds the given attributes to
  85. each inline node between `from` and `to`.
  86. */
  87. static inline(from: number, to: number, attrs: DecorationAttrs, spec?: {
  88. /**
  89. Determines how the left side of the decoration is
  90. [mapped](https://prosemirror.net/docs/ref/#transform.Position_Mapping) when content is
  91. inserted directly at that position. By default, the decoration
  92. won't include the new content, but you can set this to `true`
  93. to make it inclusive.
  94. */
  95. inclusiveStart?: boolean;
  96. /**
  97. Determines how the right side of the decoration is mapped.
  98. See
  99. [`inclusiveStart`](https://prosemirror.net/docs/ref/#view.Decoration^inline^spec.inclusiveStart).
  100. */
  101. inclusiveEnd?: boolean;
  102. /**
  103. Specs may have arbitrary additional properties.
  104. */
  105. [key: string]: any;
  106. }): Decoration;
  107. /**
  108. Creates a node decoration. `from` and `to` should point precisely
  109. before and after a node in the document. That node, and only that
  110. node, will receive the given attributes.
  111. */
  112. static node(from: number, to: number, attrs: DecorationAttrs, spec?: any): Decoration;
  113. /**
  114. The spec provided when creating this decoration. Can be useful
  115. if you've stored extra information in that object.
  116. */
  117. get spec(): any;
  118. }
  119. /**
  120. A set of attributes to add to a decorated node. Most properties
  121. simply directly correspond to DOM attributes of the same name,
  122. which will be set to the property's value. These are exceptions:
  123. */
  124. type DecorationAttrs = {
  125. /**
  126. When non-null, the target node is wrapped in a DOM element of
  127. this type (and the other attributes are applied to this element).
  128. */
  129. nodeName?: string;
  130. /**
  131. A CSS class name or a space-separated set of class names to be
  132. _added_ to the classes that the node already had.
  133. */
  134. class?: string;
  135. /**
  136. A string of CSS to be _added_ to the node's existing `style` property.
  137. */
  138. style?: string;
  139. /**
  140. Any other properties are treated as regular DOM attributes.
  141. */
  142. [attribute: string]: string | undefined;
  143. };
  144. /**
  145. An object that can [provide](https://prosemirror.net/docs/ref/#view.EditorProps.decorations)
  146. decorations. Implemented by [`DecorationSet`](https://prosemirror.net/docs/ref/#view.DecorationSet),
  147. and passed to [node views](https://prosemirror.net/docs/ref/#view.EditorProps.nodeViews).
  148. */
  149. interface DecorationSource {
  150. /**
  151. Map the set of decorations in response to a change in the
  152. document.
  153. */
  154. map: (mapping: Mapping, node: Node) => DecorationSource;
  155. /**
  156. Extract a DecorationSource containing decorations for the given child node at the given offset.
  157. */
  158. forChild(offset: number, child: Node): DecorationSource;
  159. }
  160. /**
  161. A collection of [decorations](https://prosemirror.net/docs/ref/#view.Decoration), organized in such
  162. a way that the drawing algorithm can efficiently use and compare
  163. them. This is a persistent data structure—it is not modified,
  164. updates create a new value.
  165. */
  166. declare class DecorationSet implements DecorationSource {
  167. /**
  168. Create a set of decorations, using the structure of the given
  169. document. This will consume (modify) the `decorations` array, so
  170. you must make a copy if you want need to preserve that.
  171. */
  172. static create(doc: Node, decorations: Decoration[]): DecorationSet;
  173. /**
  174. Find all decorations in this set which touch the given range
  175. (including decorations that start or end directly at the
  176. boundaries) and match the given predicate on their spec. When
  177. `start` and `end` are omitted, all decorations in the set are
  178. considered. When `predicate` isn't given, all decorations are
  179. assumed to match.
  180. */
  181. find(start?: number, end?: number, predicate?: (spec: any) => boolean): Decoration[];
  182. private findInner;
  183. /**
  184. Map the set of decorations in response to a change in the
  185. document.
  186. */
  187. map(mapping: Mapping, doc: Node, options?: {
  188. /**
  189. When given, this function will be called for each decoration
  190. that gets dropped as a result of the mapping, passing the
  191. spec of that decoration.
  192. */
  193. onRemove?: (decorationSpec: any) => void;
  194. }): DecorationSet;
  195. /**
  196. Add the given array of decorations to the ones in the set,
  197. producing a new set. Consumes the `decorations` array. Needs
  198. access to the current document to create the appropriate tree
  199. structure.
  200. */
  201. add(doc: Node, decorations: Decoration[]): DecorationSet;
  202. private addInner;
  203. /**
  204. Create a new set that contains the decorations in this set, minus
  205. the ones in the given array.
  206. */
  207. remove(decorations: Decoration[]): DecorationSet;
  208. private removeInner;
  209. forChild(offset: number, node: Node): DecorationSet | DecorationGroup;
  210. /**
  211. The empty set of decorations.
  212. */
  213. static empty: DecorationSet;
  214. }
  215. declare class DecorationGroup implements DecorationSource {
  216. readonly members: readonly DecorationSet[];
  217. constructor(members: readonly DecorationSet[]);
  218. map(mapping: Mapping, doc: Node): DecorationSource;
  219. forChild(offset: number, child: Node): DecorationSource | DecorationSet;
  220. eq(other: DecorationGroup): boolean;
  221. locals(node: Node): readonly any[];
  222. static from(members: readonly DecorationSource[]): DecorationSource;
  223. }
  224. declare global {
  225. interface Node {
  226. pmViewDesc?: ViewDesc;
  227. }
  228. }
  229. /**
  230. By default, document nodes are rendered using the result of the
  231. [`toDOM`](https://prosemirror.net/docs/ref/#model.NodeSpec.toDOM) method of their spec, and managed
  232. entirely by the editor. For some use cases, such as embedded
  233. node-specific editing interfaces, you want more control over
  234. the behavior of a node's in-editor representation, and need to
  235. [define](https://prosemirror.net/docs/ref/#view.EditorProps.nodeViews) a custom node view.
  236. Mark views only support `dom` and `contentDOM`, and don't support
  237. any of the node view methods.
  238. Objects returned as node views must conform to this interface.
  239. */
  240. interface NodeView {
  241. /**
  242. The outer DOM node that represents the document node.
  243. */
  244. dom: DOMNode;
  245. /**
  246. The DOM node that should hold the node's content. Only meaningful
  247. if the node view also defines a `dom` property and if its node
  248. type is not a leaf node type. When this is present, ProseMirror
  249. will take care of rendering the node's children into it. When it
  250. is not present, the node view itself is responsible for rendering
  251. (or deciding not to render) its child nodes.
  252. */
  253. contentDOM?: HTMLElement | null;
  254. /**
  255. When given, this will be called when the view is updating itself.
  256. It will be given a node (possibly of a different type), an array
  257. of active decorations around the node (which are automatically
  258. drawn, and the node view may ignore if it isn't interested in
  259. them), and a [decoration source](https://prosemirror.net/docs/ref/#view.DecorationSource) that
  260. represents any decorations that apply to the content of the node
  261. (which again may be ignored). It should return true if it was
  262. able to update to that node, and false otherwise. If the node
  263. view has a `contentDOM` property (or no `dom` property), updating
  264. its child nodes will be handled by ProseMirror.
  265. */
  266. update?: (node: Node, decorations: readonly Decoration[], innerDecorations: DecorationSource) => boolean;
  267. /**
  268. Can be used to override the way the node's selected status (as a
  269. node selection) is displayed.
  270. */
  271. selectNode?: () => void;
  272. /**
  273. When defining a `selectNode` method, you should also provide a
  274. `deselectNode` method to remove the effect again.
  275. */
  276. deselectNode?: () => void;
  277. /**
  278. This will be called to handle setting the selection inside the
  279. node. The `anchor` and `head` positions are relative to the start
  280. of the node. By default, a DOM selection will be created between
  281. the DOM positions corresponding to those positions, but if you
  282. override it you can do something else.
  283. */
  284. setSelection?: (anchor: number, head: number, root: Document | ShadowRoot) => void;
  285. /**
  286. Can be used to prevent the editor view from trying to handle some
  287. or all DOM events that bubble up from the node view. Events for
  288. which this returns true are not handled by the editor.
  289. */
  290. stopEvent?: (event: Event) => boolean;
  291. /**
  292. Called when a DOM
  293. [mutation](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)
  294. or a selection change happens within the view. When the change is
  295. a selection change, the record will have a `type` property of
  296. `"selection"` (which doesn't occur for native mutation records).
  297. Return false if the editor should re-read the selection or
  298. re-parse the range around the mutation, true if it can safely be
  299. ignored.
  300. */
  301. ignoreMutation?: (mutation: MutationRecord) => boolean;
  302. /**
  303. Called when the node view is removed from the editor or the whole
  304. editor is destroyed. (Not available for marks.)
  305. */
  306. destroy?: () => void;
  307. }
  308. declare class ViewDesc {
  309. parent: ViewDesc | undefined;
  310. children: ViewDesc[];
  311. dom: DOMNode;
  312. contentDOM: HTMLElement | null;
  313. dirty: number;
  314. node: Node | null;
  315. constructor(parent: ViewDesc | undefined, children: ViewDesc[], dom: DOMNode, contentDOM: HTMLElement | null);
  316. matchesWidget(widget: Decoration): boolean;
  317. matchesMark(mark: Mark): boolean;
  318. matchesNode(node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource): boolean;
  319. matchesHack(nodeName: string): boolean;
  320. parseRule(): ParseRule | null;
  321. stopEvent(event: Event): boolean;
  322. get size(): number;
  323. get border(): number;
  324. destroy(): void;
  325. posBeforeChild(child: ViewDesc): number;
  326. get posBefore(): number;
  327. get posAtStart(): number;
  328. get posAfter(): number;
  329. get posAtEnd(): number;
  330. localPosFromDOM(dom: DOMNode, offset: number, bias: number): number;
  331. nearestDesc(dom: DOMNode): ViewDesc | undefined;
  332. nearestDesc(dom: DOMNode, onlyNodes: true): NodeViewDesc | undefined;
  333. getDesc(dom: DOMNode): ViewDesc | undefined;
  334. posFromDOM(dom: DOMNode, offset: number, bias: number): number;
  335. descAt(pos: number): ViewDesc | undefined;
  336. domFromPos(pos: number, side: number): {
  337. node: DOMNode;
  338. offset: number;
  339. atom?: number;
  340. };
  341. parseRange(from: number, to: number, base?: number): {
  342. node: DOMNode;
  343. from: number;
  344. to: number;
  345. fromOffset: number;
  346. toOffset: number;
  347. };
  348. emptyChildAt(side: number): boolean;
  349. domAfterPos(pos: number): DOMNode;
  350. setSelection(anchor: number, head: number, root: Document | ShadowRoot, force?: boolean): void;
  351. ignoreMutation(mutation: MutationRecord): boolean;
  352. get contentLost(): boolean | null;
  353. markDirty(from: number, to: number): void;
  354. markParentsDirty(): void;
  355. get domAtom(): boolean;
  356. get ignoreForCoords(): boolean;
  357. }
  358. declare class NodeViewDesc extends ViewDesc {
  359. node: Node;
  360. outerDeco: readonly Decoration[];
  361. innerDeco: DecorationSource;
  362. readonly nodeDOM: DOMNode;
  363. constructor(parent: ViewDesc | undefined, node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, dom: DOMNode, contentDOM: HTMLElement | null, nodeDOM: DOMNode, view: EditorView, pos: number);
  364. static create(parent: ViewDesc | undefined, node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, view: EditorView, pos: number): NodeViewDesc | TextViewDesc;
  365. parseRule(): ParseRule | null;
  366. matchesNode(node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource): boolean;
  367. get size(): number;
  368. get border(): 0 | 1;
  369. updateChildren(view: EditorView, pos: number): void;
  370. localCompositionInfo(view: EditorView, pos: number): {
  371. node: Text;
  372. pos: number;
  373. text: string;
  374. } | null;
  375. protectLocalComposition(view: EditorView, { node, pos, text }: {
  376. node: Text;
  377. pos: number;
  378. text: string;
  379. }): void;
  380. update(node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, view: EditorView): boolean;
  381. updateInner(node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, view: EditorView): void;
  382. updateOuterDeco(outerDeco: readonly Decoration[]): void;
  383. selectNode(): void;
  384. deselectNode(): void;
  385. get domAtom(): boolean;
  386. }
  387. declare class TextViewDesc extends NodeViewDesc {
  388. constructor(parent: ViewDesc | undefined, node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, dom: DOMNode, nodeDOM: DOMNode, view: EditorView);
  389. parseRule(): ParseRule;
  390. update(node: Node, outerDeco: readonly Decoration[], innerDeco: DecorationSource, view: EditorView): boolean;
  391. inParent(): boolean;
  392. domFromPos(pos: number): {
  393. node: globalThis.Node;
  394. offset: number;
  395. };
  396. localPosFromDOM(dom: DOMNode, offset: number, bias: number): number;
  397. ignoreMutation(mutation: MutationRecord): boolean;
  398. slice(from: number, to: number, view: EditorView): TextViewDesc;
  399. markDirty(from: number, to: number): void;
  400. get domAtom(): boolean;
  401. }
  402. /**
  403. An editor view manages the DOM structure that represents an
  404. editable document. Its state and behavior are determined by its
  405. [props](https://prosemirror.net/docs/ref/#view.DirectEditorProps).
  406. */
  407. declare class EditorView {
  408. private _props;
  409. private directPlugins;
  410. private _root;
  411. private mounted;
  412. private prevDirectPlugins;
  413. private pluginViews;
  414. /**
  415. The view's current [state](https://prosemirror.net/docs/ref/#state.EditorState).
  416. */
  417. state: EditorState;
  418. /**
  419. Create a view. `place` may be a DOM node that the editor should
  420. be appended to, a function that will place it into the document,
  421. or an object whose `mount` property holds the node to use as the
  422. document container. If it is `null`, the editor will not be
  423. added to the document.
  424. */
  425. constructor(place: null | DOMNode | ((editor: HTMLElement) => void) | {
  426. mount: HTMLElement;
  427. }, props: DirectEditorProps);
  428. /**
  429. An editable DOM node containing the document. (You probably
  430. should not directly interfere with its content.)
  431. */
  432. readonly dom: HTMLElement;
  433. /**
  434. Indicates whether the editor is currently [editable](https://prosemirror.net/docs/ref/#view.EditorProps.editable).
  435. */
  436. editable: boolean;
  437. /**
  438. When editor content is being dragged, this object contains
  439. information about the dragged slice and whether it is being
  440. copied or moved. At any other time, it is null.
  441. */
  442. dragging: null | {
  443. slice: Slice;
  444. move: boolean;
  445. };
  446. /**
  447. Holds `true` when a
  448. [composition](https://w3c.github.io/uievents/#events-compositionevents)
  449. is active.
  450. */
  451. get composing(): boolean;
  452. /**
  453. The view's current [props](https://prosemirror.net/docs/ref/#view.EditorProps).
  454. */
  455. get props(): DirectEditorProps;
  456. /**
  457. Update the view's props. Will immediately cause an update to
  458. the DOM.
  459. */
  460. update(props: DirectEditorProps): void;
  461. /**
  462. Update the view by updating existing props object with the object
  463. given as argument. Equivalent to `view.update(Object.assign({},
  464. view.props, props))`.
  465. */
  466. setProps(props: Partial<DirectEditorProps>): void;
  467. /**
  468. Update the editor's `state` prop, without touching any of the
  469. other props.
  470. */
  471. updateState(state: EditorState): void;
  472. private updateStateInner;
  473. private destroyPluginViews;
  474. private updatePluginViews;
  475. private updateDraggedNode;
  476. /**
  477. Goes over the values of a prop, first those provided directly,
  478. then those from plugins given to the view, then from plugins in
  479. the state (in order), and calls `f` every time a non-undefined
  480. value is found. When `f` returns a truthy value, that is
  481. immediately returned. When `f` isn't provided, it is treated as
  482. the identity function (the prop value is returned directly).
  483. */
  484. someProp<PropName extends keyof EditorProps, Result>(propName: PropName, f: (value: NonNullable<EditorProps[PropName]>) => Result): Result | undefined;
  485. someProp<PropName extends keyof EditorProps>(propName: PropName): NonNullable<EditorProps[PropName]> | undefined;
  486. /**
  487. Query whether the view has focus.
  488. */
  489. hasFocus(): boolean;
  490. /**
  491. Focus the editor.
  492. */
  493. focus(): void;
  494. /**
  495. Get the document root in which the editor exists. This will
  496. usually be the top-level `document`, but might be a [shadow
  497. DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM)
  498. root if the editor is inside one.
  499. */
  500. get root(): Document | ShadowRoot;
  501. /**
  502. When an existing editor view is moved to a new document or
  503. shadow tree, call this to make it recompute its root.
  504. */
  505. updateRoot(): void;
  506. /**
  507. Given a pair of viewport coordinates, return the document
  508. position that corresponds to them. May return null if the given
  509. coordinates aren't inside of the editor. When an object is
  510. returned, its `pos` property is the position nearest to the
  511. coordinates, and its `inside` property holds the position of the
  512. inner node that the position falls inside of, or -1 if it is at
  513. the top level, not in any node.
  514. */
  515. posAtCoords(coords: {
  516. left: number;
  517. top: number;
  518. }): {
  519. pos: number;
  520. inside: number;
  521. } | null;
  522. /**
  523. Returns the viewport rectangle at a given document position.
  524. `left` and `right` will be the same number, as this returns a
  525. flat cursor-ish rectangle. If the position is between two things
  526. that aren't directly adjacent, `side` determines which element
  527. is used. When < 0, the element before the position is used,
  528. otherwise the element after.
  529. */
  530. coordsAtPos(pos: number, side?: number): {
  531. left: number;
  532. right: number;
  533. top: number;
  534. bottom: number;
  535. };
  536. /**
  537. Find the DOM position that corresponds to the given document
  538. position. When `side` is negative, find the position as close as
  539. possible to the content before the position. When positive,
  540. prefer positions close to the content after the position. When
  541. zero, prefer as shallow a position as possible.
  542. Note that you should **not** mutate the editor's internal DOM,
  543. only inspect it (and even that is usually not necessary).
  544. */
  545. domAtPos(pos: number, side?: number): {
  546. node: DOMNode;
  547. offset: number;
  548. };
  549. /**
  550. Find the DOM node that represents the document node after the
  551. given position. May return `null` when the position doesn't point
  552. in front of a node or if the node is inside an opaque node view.
  553. This is intended to be able to call things like
  554. `getBoundingClientRect` on that DOM node. Do **not** mutate the
  555. editor DOM directly, or add styling this way, since that will be
  556. immediately overriden by the editor as it redraws the node.
  557. */
  558. nodeDOM(pos: number): DOMNode | null;
  559. /**
  560. Find the document position that corresponds to a given DOM
  561. position. (Whenever possible, it is preferable to inspect the
  562. document structure directly, rather than poking around in the
  563. DOM, but sometimes—for example when interpreting an event
  564. target—you don't have a choice.)
  565. The `bias` parameter can be used to influence which side of a DOM
  566. node to use when the position is inside a leaf node.
  567. */
  568. posAtDOM(node: DOMNode, offset: number, bias?: number): number;
  569. /**
  570. Find out whether the selection is at the end of a textblock when
  571. moving in a given direction. When, for example, given `"left"`,
  572. it will return true if moving left from the current cursor
  573. position would leave that position's parent textblock. Will apply
  574. to the view's current state by default, but it is possible to
  575. pass a different state.
  576. */
  577. endOfTextblock(dir: "up" | "down" | "left" | "right" | "forward" | "backward", state?: EditorState): boolean;
  578. /**
  579. Run the editor's paste logic with the given HTML string. The
  580. `event`, if given, will be passed to the
  581. [`handlePaste`](https://prosemirror.net/docs/ref/#view.EditorProps.handlePaste) hook.
  582. */
  583. pasteHTML(html: string, event?: ClipboardEvent): boolean;
  584. /**
  585. Run the editor's paste logic with the given plain-text input.
  586. */
  587. pasteText(text: string, event?: ClipboardEvent): boolean;
  588. /**
  589. Removes the editor from the DOM and destroys all [node
  590. views](https://prosemirror.net/docs/ref/#view.NodeView).
  591. */
  592. destroy(): void;
  593. /**
  594. This is true when the view has been
  595. [destroyed](https://prosemirror.net/docs/ref/#view.EditorView.destroy) (and thus should not be
  596. used anymore).
  597. */
  598. get isDestroyed(): boolean;
  599. /**
  600. Used for testing.
  601. */
  602. dispatchEvent(event: Event): void;
  603. /**
  604. Dispatch a transaction. Will call
  605. [`dispatchTransaction`](https://prosemirror.net/docs/ref/#view.DirectEditorProps.dispatchTransaction)
  606. when given, and otherwise defaults to applying the transaction to
  607. the current state and calling
  608. [`updateState`](https://prosemirror.net/docs/ref/#view.EditorView.updateState) with the result.
  609. This method is bound to the view instance, so that it can be
  610. easily passed around.
  611. */
  612. dispatch(tr: Transaction): void;
  613. }
  614. /**
  615. The type of function [provided](https://prosemirror.net/docs/ref/#view.EditorProps.nodeViews) to
  616. create [node views](https://prosemirror.net/docs/ref/#view.NodeView).
  617. */
  618. type NodeViewConstructor = (node: Node, view: EditorView, getPos: () => number | undefined, decorations: readonly Decoration[], innerDecorations: DecorationSource) => NodeView;
  619. /**
  620. The function types [used](https://prosemirror.net/docs/ref/#view.EditorProps.markViews) to create
  621. mark views.
  622. */
  623. type MarkViewConstructor = (mark: Mark, view: EditorView, inline: boolean) => {
  624. dom: HTMLElement;
  625. contentDOM?: HTMLElement;
  626. };
  627. /**
  628. Helper type that maps event names to event object types, but
  629. includes events that TypeScript's HTMLElementEventMap doesn't know
  630. about.
  631. */
  632. interface DOMEventMap extends HTMLElementEventMap {
  633. [event: string]: any;
  634. }
  635. /**
  636. Props are configuration values that can be passed to an editor view
  637. or included in a plugin. This interface lists the supported props.
  638. The various event-handling functions may all return `true` to
  639. indicate that they handled the given event. The view will then take
  640. care to call `preventDefault` on the event, except with
  641. `handleDOMEvents`, where the handler itself is responsible for that.
  642. How a prop is resolved depends on the prop. Handler functions are
  643. called one at a time, starting with the base props and then
  644. searching through the plugins (in order of appearance) until one of
  645. them returns true. For some props, the first plugin that yields a
  646. value gets precedence.
  647. The optional type parameter refers to the type of `this` in prop
  648. functions, and is used to pass in the plugin type when defining a
  649. [plugin](https://prosemirror.net/docs/ref/#state.Plugin).
  650. */
  651. interface EditorProps<P = any> {
  652. /**
  653. Can be an object mapping DOM event type names to functions that
  654. handle them. Such functions will be called before any handling
  655. ProseMirror does of events fired on the editable DOM element.
  656. Contrary to the other event handling props, when returning true
  657. from such a function, you are responsible for calling
  658. `preventDefault` yourself (or not, if you want to allow the
  659. default behavior).
  660. */
  661. handleDOMEvents?: {
  662. [event in keyof DOMEventMap]?: (this: P, view: EditorView, event: DOMEventMap[event]) => boolean | void;
  663. };
  664. /**
  665. Called when the editor receives a `keydown` event.
  666. */
  667. handleKeyDown?: (this: P, view: EditorView, event: KeyboardEvent) => boolean | void;
  668. /**
  669. Handler for `keypress` events.
  670. */
  671. handleKeyPress?: (this: P, view: EditorView, event: KeyboardEvent) => boolean | void;
  672. /**
  673. Whenever the user directly input text, this handler is called
  674. before the input is applied. If it returns `true`, the default
  675. behavior of actually inserting the text is suppressed.
  676. */
  677. handleTextInput?: (this: P, view: EditorView, from: number, to: number, text: string) => boolean | void;
  678. /**
  679. Called for each node around a click, from the inside out. The
  680. `direct` flag will be true for the inner node.
  681. */
  682. handleClickOn?: (this: P, view: EditorView, pos: number, node: Node, nodePos: number, event: MouseEvent, direct: boolean) => boolean | void;
  683. /**
  684. Called when the editor is clicked, after `handleClickOn` handlers
  685. have been called.
  686. */
  687. handleClick?: (this: P, view: EditorView, pos: number, event: MouseEvent) => boolean | void;
  688. /**
  689. Called for each node around a double click.
  690. */
  691. handleDoubleClickOn?: (this: P, view: EditorView, pos: number, node: Node, nodePos: number, event: MouseEvent, direct: boolean) => boolean | void;
  692. /**
  693. Called when the editor is double-clicked, after `handleDoubleClickOn`.
  694. */
  695. handleDoubleClick?: (this: P, view: EditorView, pos: number, event: MouseEvent) => boolean | void;
  696. /**
  697. Called for each node around a triple click.
  698. */
  699. handleTripleClickOn?: (this: P, view: EditorView, pos: number, node: Node, nodePos: number, event: MouseEvent, direct: boolean) => boolean | void;
  700. /**
  701. Called when the editor is triple-clicked, after `handleTripleClickOn`.
  702. */
  703. handleTripleClick?: (this: P, view: EditorView, pos: number, event: MouseEvent) => boolean | void;
  704. /**
  705. Can be used to override the behavior of pasting. `slice` is the
  706. pasted content parsed by the editor, but you can directly access
  707. the event to get at the raw content.
  708. */
  709. handlePaste?: (this: P, view: EditorView, event: ClipboardEvent, slice: Slice) => boolean | void;
  710. /**
  711. Called when something is dropped on the editor. `moved` will be
  712. true if this drop moves from the current selection (which should
  713. thus be deleted).
  714. */
  715. handleDrop?: (this: P, view: EditorView, event: DragEvent, slice: Slice, moved: boolean) => boolean | void;
  716. /**
  717. Called when the view, after updating its state, tries to scroll
  718. the selection into view. A handler function may return false to
  719. indicate that it did not handle the scrolling and further
  720. handlers or the default behavior should be tried.
  721. */
  722. handleScrollToSelection?: (this: P, view: EditorView) => boolean;
  723. /**
  724. Can be used to override the way a selection is created when
  725. reading a DOM selection between the given anchor and head.
  726. */
  727. createSelectionBetween?: (this: P, view: EditorView, anchor: ResolvedPos, head: ResolvedPos) => Selection | null;
  728. /**
  729. The [parser](https://prosemirror.net/docs/ref/#model.DOMParser) to use when reading editor changes
  730. from the DOM. Defaults to calling
  731. [`DOMParser.fromSchema`](https://prosemirror.net/docs/ref/#model.DOMParser^fromSchema) on the
  732. editor's schema.
  733. */
  734. domParser?: DOMParser;
  735. /**
  736. Can be used to transform pasted HTML text, _before_ it is parsed,
  737. for example to clean it up.
  738. */
  739. transformPastedHTML?: (this: P, html: string, view: EditorView) => string;
  740. /**
  741. The [parser](https://prosemirror.net/docs/ref/#model.DOMParser) to use when reading content from
  742. the clipboard. When not given, the value of the
  743. [`domParser`](https://prosemirror.net/docs/ref/#view.EditorProps.domParser) prop is used.
  744. */
  745. clipboardParser?: DOMParser;
  746. /**
  747. Transform pasted plain text. The `plain` flag will be true when
  748. the text is pasted as plain text.
  749. */
  750. transformPastedText?: (this: P, text: string, plain: boolean, view: EditorView) => string;
  751. /**
  752. A function to parse text from the clipboard into a document
  753. slice. Called after
  754. [`transformPastedText`](https://prosemirror.net/docs/ref/#view.EditorProps.transformPastedText).
  755. The default behavior is to split the text into lines, wrap them
  756. in `<p>` tags, and call
  757. [`clipboardParser`](https://prosemirror.net/docs/ref/#view.EditorProps.clipboardParser) on it.
  758. The `plain` flag will be true when the text is pasted as plain text.
  759. */
  760. clipboardTextParser?: (this: P, text: string, $context: ResolvedPos, plain: boolean, view: EditorView) => Slice;
  761. /**
  762. Can be used to transform pasted or dragged-and-dropped content
  763. before it is applied to the document.
  764. */
  765. transformPasted?: (this: P, slice: Slice, view: EditorView) => Slice;
  766. /**
  767. Can be used to transform copied or cut content before it is
  768. serialized to the clipboard.
  769. */
  770. transformCopied?: (this: P, slice: Slice, view: EditorView) => Slice;
  771. /**
  772. Allows you to pass custom rendering and behavior logic for
  773. nodes. Should map node names to constructor functions that
  774. produce a [`NodeView`](https://prosemirror.net/docs/ref/#view.NodeView) object implementing the
  775. node's display behavior. The third argument `getPos` is a
  776. function that can be called to get the node's current position,
  777. which can be useful when creating transactions to update it.
  778. Note that if the node is not in the document, the position
  779. returned by this function will be `undefined`.
  780. `decorations` is an array of node or inline decorations that are
  781. active around the node. They are automatically drawn in the
  782. normal way, and you will usually just want to ignore this, but
  783. they can also be used as a way to provide context information to
  784. the node view without adding it to the document itself.
  785. `innerDecorations` holds the decorations for the node's content.
  786. You can safely ignore this if your view has no content or a
  787. `contentDOM` property, since the editor will draw the decorations
  788. on the content. But if you, for example, want to create a nested
  789. editor with the content, it may make sense to provide it with the
  790. inner decorations.
  791. (For backwards compatibility reasons, [mark
  792. views](https://prosemirror.net/docs/ref/#view.EditorProps.markViews) can also be included in this
  793. object.)
  794. */
  795. nodeViews?: {
  796. [node: string]: NodeViewConstructor;
  797. };
  798. /**
  799. Pass custom mark rendering functions. Note that these cannot
  800. provide the kind of dynamic behavior that [node
  801. views](https://prosemirror.net/docs/ref/#view.NodeView) can—they just provide custom rendering
  802. logic. The third argument indicates whether the mark's content
  803. is inline.
  804. */
  805. markViews?: {
  806. [mark: string]: MarkViewConstructor;
  807. };
  808. /**
  809. The DOM serializer to use when putting content onto the
  810. clipboard. If not given, the result of
  811. [`DOMSerializer.fromSchema`](https://prosemirror.net/docs/ref/#model.DOMSerializer^fromSchema)
  812. will be used. This object will only have its
  813. [`serializeFragment`](https://prosemirror.net/docs/ref/#model.DOMSerializer.serializeFragment)
  814. method called, and you may provide an alternative object type
  815. implementing a compatible method.
  816. */
  817. clipboardSerializer?: DOMSerializer;
  818. /**
  819. A function that will be called to get the text for the current
  820. selection when copying text to the clipboard. By default, the
  821. editor will use [`textBetween`](https://prosemirror.net/docs/ref/#model.Node.textBetween) on the
  822. selected range.
  823. */
  824. clipboardTextSerializer?: (this: P, content: Slice, view: EditorView) => string;
  825. /**
  826. A set of [document decorations](https://prosemirror.net/docs/ref/#view.Decoration) to show in the
  827. view.
  828. */
  829. decorations?: (this: P, state: EditorState) => DecorationSource | null | undefined;
  830. /**
  831. When this returns false, the content of the view is not directly
  832. editable.
  833. */
  834. editable?: (this: P, state: EditorState) => boolean;
  835. /**
  836. Control the DOM attributes of the editable element. May be either
  837. an object or a function going from an editor state to an object.
  838. By default, the element will get a class `"ProseMirror"`, and
  839. will have its `contentEditable` attribute determined by the
  840. [`editable` prop](https://prosemirror.net/docs/ref/#view.EditorProps.editable). Additional classes
  841. provided here will be added to the class. For other attributes,
  842. the value provided first (as in
  843. [`someProp`](https://prosemirror.net/docs/ref/#view.EditorView.someProp)) will be used.
  844. */
  845. attributes?: {
  846. [name: string]: string;
  847. } | ((state: EditorState) => {
  848. [name: string]: string;
  849. });
  850. /**
  851. Determines the distance (in pixels) between the cursor and the
  852. end of the visible viewport at which point, when scrolling the
  853. cursor into view, scrolling takes place. Defaults to 0.
  854. */
  855. scrollThreshold?: number | {
  856. top: number;
  857. right: number;
  858. bottom: number;
  859. left: number;
  860. };
  861. /**
  862. Determines the extra space (in pixels) that is left above or
  863. below the cursor when it is scrolled into view. Defaults to 5.
  864. */
  865. scrollMargin?: number | {
  866. top: number;
  867. right: number;
  868. bottom: number;
  869. left: number;
  870. };
  871. }
  872. /**
  873. The props object given directly to the editor view supports some
  874. fields that can't be used in plugins:
  875. */
  876. interface DirectEditorProps extends EditorProps {
  877. /**
  878. The current state of the editor.
  879. */
  880. state: EditorState;
  881. /**
  882. A set of plugins to use in the view, applying their [plugin
  883. view](https://prosemirror.net/docs/ref/#state.PluginSpec.view) and
  884. [props](https://prosemirror.net/docs/ref/#state.PluginSpec.props). Passing plugins with a state
  885. component (a [state field](https://prosemirror.net/docs/ref/#state.PluginSpec.state) field or a
  886. [transaction](https://prosemirror.net/docs/ref/#state.PluginSpec.filterTransaction) filter or
  887. appender) will result in an error, since such plugins must be
  888. present in the state to work.
  889. */
  890. plugins?: readonly Plugin[];
  891. /**
  892. The callback over which to send transactions (state updates)
  893. produced by the view. If you specify this, you probably want to
  894. make sure this ends up calling the view's
  895. [`updateState`](https://prosemirror.net/docs/ref/#view.EditorView.updateState) method with a new
  896. state that has the transaction
  897. [applied](https://prosemirror.net/docs/ref/#state.EditorState.apply). The callback will be bound to have
  898. the view instance as its `this` binding.
  899. */
  900. dispatchTransaction?: (tr: Transaction) => void;
  901. }
  902. export { type DOMEventMap, Decoration, type DecorationAttrs, DecorationSet, type DecorationSource, type DirectEditorProps, type EditorProps, EditorView, type MarkViewConstructor, type NodeView, type NodeViewConstructor };