prosemirror-test-builder
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,9 @@ | ||
## 1.1.1 (2023-05-17) | ||
### Bug fixes | ||
Make the return type of \`builders\` include properties for schema node/mark names. | ||
Include CommonJS type declarations in the package to please new TypeScript resolution settings. | ||
## 1.1.0 (2022-05-30) | ||
@@ -2,0 +10,0 @@ |
@@ -0,1 +1,3 @@ | ||
import * as orderedmap from 'orderedmap'; | ||
import * as prosemirror_model from 'prosemirror-model'; | ||
import { Attrs, Node, Schema } from 'prosemirror-model'; | ||
@@ -10,11 +12,20 @@ | ||
}; | ||
declare type NodeBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => Node; | ||
declare type NodeBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => Node & { | ||
tag: Tags; | ||
}; | ||
declare type MarkBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => ChildSpec; | ||
declare function builders(schema: Schema, names?: { | ||
declare type Builders<S extends Schema> = { | ||
schema: S; | ||
} & { | ||
[key in keyof S['nodes']]: NodeBuilder; | ||
} & { | ||
[key in keyof S['marks']]: MarkBuilder; | ||
} & { | ||
[name: string]: NodeBuilder | MarkBuilder; | ||
}; | ||
declare function builders<Nodes extends string = any, Marks extends string = any>(schema: Schema<Nodes, Marks>, names?: { | ||
[name: string]: Attrs; | ||
}): { | ||
schema: Schema; | ||
}; | ||
}): Builders<Schema<Nodes, Marks>>; | ||
declare const schema: Schema; | ||
declare const schema: Schema<keyof orderedmap.default<prosemirror_model.NodeSpec>, keyof orderedmap.default<prosemirror_model.MarkSpec>>; | ||
declare function eq<T extends { | ||
@@ -21,0 +32,0 @@ eq(other: T): boolean; |
{ | ||
"name": "prosemirror-test-builder", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Helpers for programatically building ProseMirror test documents", | ||
@@ -18,3 +18,3 @@ "type": "module", | ||
"name": "Marijn Haverbeke", | ||
"email": "marijnh@gmail.com", | ||
"email": "marijn@haverbeke.berlin", | ||
"web": "http://marijnhaverbeke.nl" | ||
@@ -21,0 +21,0 @@ } |
@@ -68,8 +68,18 @@ import {Node, NodeType, MarkType, Schema, Attrs} from "prosemirror-model" | ||
export type NodeBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => Node | ||
export type NodeBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => Node & {tag: Tags} | ||
export type MarkBuilder = (attrsOrFirstChild?: Attrs | ChildSpec, ...children: ChildSpec[]) => ChildSpec | ||
type Builders<S extends Schema> = { | ||
schema: S; | ||
} & { | ||
[key in keyof S['nodes']]: NodeBuilder | ||
} & { | ||
[key in keyof S['marks']]: MarkBuilder | ||
} & { | ||
[name: string]: NodeBuilder | MarkBuilder | ||
} | ||
/// Create a builder function for nodes with content. | ||
function block(type: NodeType, attrs: Attrs | null = null): NodeBuilder { | ||
let result: NodeBuilder = function(...args) { | ||
let result = function(...args: any[]) { | ||
let myAttrs = takeAttrs(attrs, args) | ||
@@ -82,3 +92,3 @@ let {nodes, tag} = flatten(type.schema, args as ChildSpec[], id) | ||
if (type.isLeaf) try { (result as any).flat = [type.create(attrs)] } catch(_) {} | ||
return result | ||
return result as NodeBuilder | ||
} | ||
@@ -98,3 +108,3 @@ | ||
export function builders(schema: Schema, names?: {[name: string]: Attrs}) { | ||
export function builders<Nodes extends string = any, Marks extends string = any>(schema: Schema<Nodes, Marks>, names?: {[name: string]: Attrs}) { | ||
let result = {schema} | ||
@@ -109,3 +119,3 @@ for (let name in schema.nodes) (result as any)[name] = block(schema.nodes[name], {}) | ||
} | ||
return result | ||
return result as Builders<Schema<Nodes, Marks>> | ||
} |
Sorry, the diff of this file is not supported yet
26820
10
557