Skip to content

Commit

Permalink
Merge pull request #46 from benrbray/benrbray/gh31/fix-prosemirror-types
Browse files Browse the repository at this point in the history
[gh31] update `prosemirror` to latest version, address breaking changes to types
  • Loading branch information
benrbray authored Apr 30, 2023
2 parents 133800e + 0bb88a3 commit 907f9d4
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 844 deletions.
869 changes: 247 additions & 622 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "noteworthy",
"version": "0.0.1",
"description": "Text editor with rich document semantics, based on ProseMirror.",
"description": "Markdown editor with excellent math support!",
"repository": {
"type": "git",
"url": "github:benrbray/noteworthy"
Expand Down Expand Up @@ -105,7 +105,6 @@
"prosemirror-schema-list": "^1.1.4",
"prosemirror-state": "^1.3.4",
"prosemirror-suggest": "^0.7.6",
"prosemirror-tables": "^1.1.1",
"prosemirror-transform": "^1.2.12",
"prosemirror-view": "^1.18.0",
"remark": "^13.0.0",
Expand All @@ -131,18 +130,7 @@
"@types/lodash": "^4.14.158",
"@types/mdast": "^3.0.3",
"@types/mocha": "^10.0.1",
"@types/prosemirror-commands": "^1.0.4",
"@types/prosemirror-gapcursor": "^1.0.2",
"@types/prosemirror-history": "^1.0.2",
"@types/prosemirror-inputrules": "^1.0.4",
"@types/prosemirror-keymap": "^1.0.4",
"@types/prosemirror-model": "^1.11.3",
"@types/prosemirror-schema-basic": "^1.0.2",
"@types/prosemirror-schema-list": "^1.0.3",
"@types/prosemirror-state": "^1.2.6",
"@types/prosemirror-tables": "^0.9.1",
"@types/prosemirror-transform": "^1.1.2",
"@types/prosemirror-view": "^1.17.1",
"@types/source-map-support": "^0.5.3",
"@types/unist": "^2.0.3",
"asar": "^3.0.3",
Expand Down
30 changes: 15 additions & 15 deletions src/common/extensions/editor-config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// prosemirror imports
import { Schema, Node as ProseNode, Mark as ProseMark, NodeSpec, MarkSpec, DOMOutputSpec } from "prosemirror-model";
import { Schema as ProseSchema, Node as ProseNode, Mark as ProseMark, NodeSpec, MarkSpec } from "prosemirror-model";
import { InputRule } from "prosemirror-inputrules"
import {
chainCommands, baseKeymap, Keymap, Command as ProseCommand,
chainCommands, baseKeymap,
} from "prosemirror-commands"
import { Plugin as ProsePlugin } from "prosemirror-state";
import { Plugin as ProsePlugin, Command as ProseCommand } from "prosemirror-state";
import { keymap as makeKeymap } from "prosemirror-keymap";

// patched prosemirror types
import { NodeViewConstructor, ProseSchema } from "@common/types";
import { ProseKeymap } from "@common/types";
import { markMapBasic, markMapStringLiteral, MdParser, nodeMapBasic, nodeMapLeaf, nodeMapStringLiteral } from "@common/markdown/mdast2prose";

// unist / unified
Expand Down Expand Up @@ -41,6 +41,7 @@ import { SyntaxExtension, NodeExtension, MarkExtension, MdastNodeMapType, MdastM
import * as prose2mdast from "@common/markdown/prose2mdast";
import * as mdast2prose from "@common/markdown/mdast2prose";
import { makeInputRulePlugin } from "@common/prosemirror/inputRules";
import { NodeViewConstructor } from "prosemirror-view";

//// EDITOR CONFIG /////////////////////////////////////////

Expand All @@ -51,13 +52,13 @@ type AstParent<BaseT extends Uni.Node> = Uni.Parent & BaseT & { children: AstNod

// -- Mdast2Prose --------------------------------------------------------------

export type UnistNodeTest<S extends ProseSchema = ProseSchema, T extends Uni.Node = Uni.Node> = {
export type UnistNodeTest<T extends Uni.Node = Uni.Node> = {
test?: (x: T) => boolean;
map: (x: T, children: ProseNode<S>[], ctx: unknown, state: unknown) => ProseNode<S>[];
map: (x: T, children: ProseNode[], ctx: unknown, state: unknown) => ProseNode[];
}

export type UnistMapper<K extends string = string, S extends ProseSchema = ProseSchema>
= DefaultMap<K, UnistNodeTest<S>[]>;
export type UnistMapper<K extends string = string>
= DefaultMap<K, UnistNodeTest[]>;

// -- Prose2Mdast --------------------------------------------------------------

Expand All @@ -78,7 +79,7 @@ export type ProseNodeMap<Ctx=unknown, St=unknown>
export type ProseMarkMap
= (mark: ProseMark, node:Uni.Node) => Uni.Node;

export type ProseNodeTest<S extends ProseSchema = ProseSchema, N extends ProseNode<S> = ProseNode<S>> = {
export type ProseNodeTest<N extends ProseNode = ProseNode> = {
test?: (x: N) => boolean;
map: ProseNodeMap<unknown, unknown>
}
Expand All @@ -91,11 +92,10 @@ export type ProseMarkTest<M extends ProseMark = ProseMark> = {

export type ProseMapper<
N extends string = string,
M extends string = string,
S extends ProseSchema<N,M> = ProseSchema<N,M>
M extends string = string
> = {
/** A map from ProseMirror Nodes -> Unist Nodes */
nodes: DefaultMap<N, ProseNodeTest<S>[]>;
nodes: DefaultMap<N, ProseNodeTest[]>;
/** A map from ProseMirror Marks -> Unist Nodes */
marks: DefaultMap<M, ProseMarkTest[]>;
}
Expand All @@ -114,7 +114,7 @@ export class EditorConfig<S extends ProseSchema = ProseSchema> {
private _parser: MdParser<S>;
private _serializer: MdSerializer;

constructor(extensions:SyntaxExtension<S>[], plugins:ProsePlugin[], keymap:Keymap){
constructor(extensions:SyntaxExtension<S>[], plugins:ProsePlugin[], keymap: ProseKeymap){
/** Step 1: Build Schema
* @effect Populates the `.type` field of each extension with a
* ProseMirror NodeType, which can be referenced during plugin creation.
Expand Down Expand Up @@ -263,7 +263,7 @@ export class EditorConfig<S extends ProseSchema = ProseSchema> {
// build schema
// TODO: speicalize to ProseSchema<N,M> for some N,M?
// TODO: is this cast sound?
let schema = new Schema({
let schema = new ProseSchema({
"nodes" : nodeSpecs,
"marks" : markSpecs
}) as S & ProseSchema<"error_block","error_inline">;
Expand Down Expand Up @@ -312,7 +312,7 @@ export class EditorConfig<S extends ProseSchema = ProseSchema> {

// include extension keymaps
/** @todo (9/27/20) sort keymap entries by priority? */
let keymap:Keymap = { };
let keymap: ProseKeymap = { };
keymaps.forEach((cmds, key) => {
keymap[key] = chainCommands(...cmds);
});
Expand Down
17 changes: 8 additions & 9 deletions src/common/extensions/extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// prosemirror imports
import { Node as ProseNode, NodeSpec, MarkSpec } from "prosemirror-model";
import { Schema as ProseSchema, Node as ProseNode, NodeSpec, MarkSpec } from "prosemirror-model";
import { InputRule } from "prosemirror-inputrules"
import { Keymap } from "prosemirror-commands"
import { NodeView } from "prosemirror-view";
import { NodeViewConstructor } from "prosemirror-view";

// project imports
import { ProseSchema, ProseNodeType, ProseMarkType, NodeViewConstructor } from "@common/types";
import { ProseNodeType, ProseMarkType, ProseKeymap } from "@common/types";

// unist
import * as Uni from "unist";
Expand Down Expand Up @@ -43,15 +42,15 @@ export abstract class SyntaxExtension<S extends ProseSchema = ProseSchema, N ext
}

createInputRules(): InputRule[] { return []; }
createKeymap(): Keymap { return {}; }
createKeymap(): ProseKeymap { return {}; }
}

export abstract class NodeExtension<N extends string = string> extends SyntaxExtension<ProseSchema<N,string>, N> {
/**
* Returns the ProseMirror NodeType defined by this extension.
* (not available until the extension has been used to create a schema)
*/
get nodeType(): ProseNodeType<ProseSchema, N> {
get nodeType(): ProseNodeType {
let type = this.store.schema.nodes[this.name];
if(type === undefined) { throw new Error(`error retrieving node type for extension ${this.name}`); }
return type;
Expand All @@ -66,7 +65,7 @@ export abstract class MarkExtension<M extends string = string> extends SyntaxExt
* Returns the ProseMirror MarkType defined by this extension.
* (not available until the extension has been used to create a schema)
*/
get markType(): ProseMarkType<ProseSchema<string, M>, M> {
get markType(): ProseMarkType {
let type = this.store.schema.marks[this.name];
if(type === undefined) { throw new Error(`error retrieving mark type for extension ${this.name}`); }
return type;
Expand Down Expand Up @@ -133,15 +132,15 @@ export type MdNodeMap_Custom<
S extends ProseSchema = ProseSchema
> = {
mapType: "node_custom",
mapNode: (node: N, children: ProseNode<S>[], ctx: unknown, state: unknown) => ProseNode<S>[];
mapNode: (node: N, children: ProseNode[], ctx: unknown, state: unknown) => ProseNode[];
}

export type MdMarkMap_Custom<
N extends Uni.Node = Uni.Node,
S extends ProseSchema = ProseSchema
> = {
mapType: "mark_custom",
mapMark: (node: N, children: ProseNode<S>[]) => ProseNode<S>[];
mapMark: (node: N, children: ProseNode[]) => ProseNode[];
}

export enum MdastNodeMapType {
Expand Down
23 changes: 12 additions & 11 deletions src/common/extensions/mark-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// prosemirror imports
import { MarkType, MarkSpec, DOMOutputSpec, Mark as ProseMark } from "prosemirror-model";
import { InputRule } from "prosemirror-inputrules"
import { toggleMark, Keymap } from "prosemirror-commands"
import { toggleMark } from "prosemirror-commands"

// project imports
import * as Md from "@common/markdown/markdown-ast";
Expand All @@ -14,6 +14,7 @@ import * as Uni from "unist";
import * as Mdast from "mdast";
import { AnyChildren, markMapBasic } from "@common/markdown/mdast2prose";
import { unistIsStringLiteral } from "@common/markdown/unist-utils";
import { ProseKeymap } from "@common/types";

//// MARK EXTENSIONS ///////////////////////////////////////

Expand Down Expand Up @@ -41,11 +42,11 @@ export class BoldExtension extends MarkSyntaxExtension<Md.Strong> {
};
}

createInputRules(): InputRule<any>[] {
createInputRules(): InputRule[] {
return [boldRule(this.markType)];
}

createKeymap(): Keymap { return {
createKeymap(): ProseKeymap { return {
"Mod-b" : toggleMark(this.markType),
"Mod-B" : toggleMark(this.markType)
}}
Expand Down Expand Up @@ -81,11 +82,11 @@ export class ItalicExtension extends MarkSyntaxExtension<Md.Emphasis> {
};
}

createInputRules(): InputRule<any>[] {
createInputRules(): InputRule[] {
return [italicRule(this.markType)];
}

createKeymap(): Keymap { return {
createKeymap(): ProseKeymap { return {
"Mod-i" : toggleMark(this.markType),
"Mod-I" : toggleMark(this.markType)
}}
Expand Down Expand Up @@ -116,7 +117,7 @@ export class ItalicExtension extends MarkSyntaxExtension<Md.Emphasis> {
// };
// }

// createKeymap(): Keymap { return {
// createKeymap(): ProseKeymap { return {
// "Mod-d" : toggleMark(this.type),
// "Mod-D" : toggleMark(this.type)
// }}
Expand Down Expand Up @@ -152,7 +153,7 @@ export class LinkExtension extends MarkSyntaxExtension<Md.Link> {
};
}

createKeymap(): Keymap {
createKeymap(): ProseKeymap {
return { "Mod-k" : (state, dispatch, view) => {
// only insert link when highlighting text
if(state.selection.empty){ return false; }
Expand Down Expand Up @@ -240,7 +241,7 @@ export class LinkExtension extends MarkSyntaxExtension<Md.Link> {
// };
// }

// createKeymap(): Keymap { return {
// createKeymap(): ProseKeymap { return {
// "Mod-u" : toggleMark(this.type)
// }}

Expand All @@ -266,7 +267,7 @@ export class CodeExtension extends MarkSyntaxExtension<Md.InlineCode> {
};
}

createKeymap(): Keymap {
createKeymap(): ProseKeymap {
// note: on mac, pressing Cmd+` does not register as an event
// https://github.com/ProseMirror/prosemirror/issues/540
return { "Ctrl-`" : toggleMark(this.markType) };
Expand Down Expand Up @@ -307,7 +308,7 @@ export class CodeExtension extends MarkSyntaxExtension<Md.InlineCode> {
// };
// }

// createKeymap(): Keymap { return {
// createKeymap(): ProseKeymap { return {
// "Mod-u" : toggleMark(this.type)
// }}

Expand All @@ -334,7 +335,7 @@ export class WikilinkExtension extends MarkSyntaxExtension<Md.Wikilink> {
};
}

createKeymap(): Keymap { return {
createKeymap(): ProseKeymap { return {
"Mod-[" : toggleMark(this.markType)
}}

Expand Down
Loading

0 comments on commit 907f9d4

Please sign in to comment.