@prosekit/core
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -604,2 +604,9 @@ import { AllSelection } from '@prosekit/pm/state'; | ||
*/ | ||
export declare class DOMDocumentNotFoundError extends ProseKitError { | ||
constructor(); | ||
} | ||
/** | ||
* @internal | ||
*/ | ||
export declare const domEventFacet: Facet<DOMEventPayload, PluginPayload>; | ||
@@ -624,2 +631,4 @@ | ||
export declare type DOMNode = InstanceType<typeof window.Node>; | ||
declare type DoubleClickHandler = (view: EditorView, pos: number, event: MouseEvent) => boolean | void; | ||
@@ -731,4 +740,28 @@ export { DoubleClickHandler } | ||
*/ | ||
export declare function elementFromHTML(html: string): HTMLElement; | ||
export declare function elementFromHTML(html: string, options?: { | ||
document?: Document; | ||
}): HTMLElement; | ||
/** | ||
* Parse a ProseMirror document JSON object to a HTML element. | ||
* | ||
* @public | ||
*/ | ||
declare function elementFromJSON(json: NodeJSON, schema: Schema, options?: { | ||
document?: Document; | ||
}): HTMLElement; | ||
export { elementFromJSON } | ||
export { elementFromJSON as elementFromJSON_alias_1 } | ||
/** | ||
* Serialize a ProseMirror node to a HTML element. | ||
* | ||
* @public | ||
*/ | ||
declare function elementFromNode(node: ProseMirrorNode, options?: { | ||
document?: Document; | ||
}): HTMLElement; | ||
export { elementFromNode } | ||
export { elementFromNode as elementFromNode_alias_1 } | ||
declare type EmptyValue = undefined | null | EmptyObject; | ||
@@ -896,2 +929,10 @@ | ||
export declare function findBrowserDocument(options?: { | ||
document?: Document; | ||
}): Document | undefined; | ||
export declare function findBrowserWindow(options?: { | ||
document?: Document; | ||
}): (Window & typeof globalThis) | null | undefined; | ||
/** | ||
@@ -908,5 +949,9 @@ * A function that is called when the editor gains or loses focus. | ||
export declare function getBrowserDocument(): Document | undefined; | ||
export declare function getBrowserDocument(options?: { | ||
document?: Document; | ||
}): Document; | ||
export declare function getBrowserWindow(): (Window & typeof globalThis) | null | undefined; | ||
export declare function getBrowserWindow(options?: { | ||
document?: Document; | ||
}): Window & typeof globalThis; | ||
@@ -947,2 +992,27 @@ export declare function getCustomSelection(state: EditorState, from?: number | null, to?: number | null): Selection_3; | ||
/** | ||
* @internal | ||
*/ | ||
export declare function htmlFromElement(element: HTMLElement): string; | ||
/** | ||
* Parse a ProseMirror document JSON object to a HTML string. | ||
* | ||
* @public | ||
*/ | ||
declare function htmlFromJSON(json: NodeJSON, schema: Schema, options?: { | ||
document?: Document; | ||
}): string; | ||
export { htmlFromJSON } | ||
export { htmlFromJSON as htmlFromJSON_alias_1 } | ||
/** | ||
* Serialize a ProseMirror node to a HTML string | ||
* | ||
* @public | ||
*/ | ||
declare function htmlFromNode(node: ProseMirrorNode): string; | ||
export { htmlFromNode } | ||
export { htmlFromNode as htmlFromNode_alias_1 } | ||
/** | ||
* Returns a command that inserts the given node at the current selection or at | ||
@@ -1041,16 +1111,16 @@ * the given position. | ||
/** | ||
* Parse a HTML element to a ProseMirror document JSON. | ||
* Serialize a HTML element to a ProseMirror document JSON object. | ||
* | ||
* @public | ||
*/ | ||
declare function jsonFromElement(element: HTMLElement, schema: Schema): NodeJSON; | ||
export { jsonFromElement } | ||
export { jsonFromElement as jsonFromElement_alias_1 } | ||
export declare function jsonFromElement(element: DOMNode, schema: Schema): NodeJSON; | ||
/** | ||
* Parse a HTML string to a ProseMirror document JSON. | ||
* Parse a HTML string to a ProseMirror document JSON object. | ||
* | ||
* @public | ||
*/ | ||
declare function jsonFromHTML(html: string, schema: Schema): NodeJSON; | ||
declare function jsonFromHTML(html: string, schema: Schema, options?: { | ||
document?: Document; | ||
}): NodeJSON; | ||
export { jsonFromHTML } | ||
@@ -1209,3 +1279,3 @@ export { jsonFromHTML as jsonFromHTML_alias_1 } | ||
*/ | ||
declare function nodeFromElement(element: HTMLElement, schema: Schema): ProseMirrorNode; | ||
declare function nodeFromElement(element: DOMNode, schema: Schema): ProseMirrorNode; | ||
export { nodeFromElement } | ||
@@ -1219,3 +1289,5 @@ export { nodeFromElement as nodeFromElement_alias_1 } | ||
*/ | ||
declare function nodeFromHTML(html: string, schema: Schema): ProseMirrorNode; | ||
declare function nodeFromHTML(html: string, schema: Schema, options?: { | ||
document?: Document; | ||
}): ProseMirrorNode; | ||
export { nodeFromHTML } | ||
@@ -1222,0 +1294,0 @@ export { nodeFromHTML as nodeFromHTML_alias_1 } |
@@ -102,3 +102,6 @@ export { addMark } from './_tsup-dts-rollup'; | ||
export { isInCodeBlock } from './_tsup-dts-rollup'; | ||
export { jsonFromElement } from './_tsup-dts-rollup'; | ||
export { elementFromJSON } from './_tsup-dts-rollup'; | ||
export { elementFromNode } from './_tsup-dts-rollup'; | ||
export { htmlFromJSON } from './_tsup-dts-rollup'; | ||
export { htmlFromNode } from './_tsup-dts-rollup'; | ||
export { jsonFromHTML } from './_tsup-dts-rollup'; | ||
@@ -105,0 +108,0 @@ export { jsonFromNode } from './_tsup-dts-rollup'; |
@@ -18,2 +18,9 @@ // src/commands/add-mark.ts | ||
}; | ||
var DOMDocumentNotFoundError = class extends ProseKitError { | ||
constructor() { | ||
super( | ||
"Unable to find browser Document. When not in the browser environment, you need to pass a DOM Document." | ||
); | ||
} | ||
}; | ||
@@ -437,7 +444,7 @@ // src/utils/get-mark-type.ts | ||
// src/utils/parse.ts | ||
import { DOMParser } from "@prosekit/pm/model"; | ||
import { DOMParser, DOMSerializer } from "@prosekit/pm/model"; | ||
import { EditorState } from "@prosekit/pm/state"; | ||
// src/utils/get-dom-api.ts | ||
function getGlobalBrowserDocument() { | ||
function findGlobalBrowserDocument() { | ||
if (typeof document !== "undefined") { | ||
@@ -450,3 +457,3 @@ return document; | ||
} | ||
function getGlobalBrowserWindow() { | ||
function findGlobalBrowserWindow() { | ||
if (typeof window !== "undefined") { | ||
@@ -459,46 +466,75 @@ return window; | ||
} | ||
function getBrowserWindow() { | ||
var _a; | ||
const win = getGlobalBrowserWindow(); | ||
if (win) { | ||
function findBrowserDocument(options) { | ||
var _a, _b, _c; | ||
return (_c = (_a = options == null ? void 0 : options.document) != null ? _a : findGlobalBrowserDocument()) != null ? _c : (_b = findGlobalBrowserWindow()) == null ? void 0 : _b.document; | ||
} | ||
function findBrowserWindow(options) { | ||
var _a, _b, _c, _d; | ||
return (_d = (_b = (_a = options == null ? void 0 : options.document) == null ? void 0 : _a.defaultView) != null ? _b : findGlobalBrowserWindow()) != null ? _d : (_c = findBrowserDocument(options)) == null ? void 0 : _c.defaultView; | ||
} | ||
function getBrowserDocument(options) { | ||
const doc = findBrowserDocument(options); | ||
if (doc) | ||
return doc; | ||
throw new DOMDocumentNotFoundError(); | ||
} | ||
function getBrowserWindow(options) { | ||
const win = findBrowserWindow(options); | ||
if (win) | ||
return win; | ||
} | ||
return (_a = getGlobalBrowserDocument()) == null ? void 0 : _a.defaultView; | ||
throw new DOMDocumentNotFoundError(); | ||
} | ||
// src/utils/parse.ts | ||
function nodeFromElement(element, schema) { | ||
return DOMParser.fromSchema(schema).parse(element); | ||
function jsonFromState(state) { | ||
return state.toJSON(); | ||
} | ||
function jsonFromElement(element, schema) { | ||
return jsonFromNode(nodeFromElement(element, schema)); | ||
function stateFromJSON(json, schema) { | ||
return EditorState.fromJSON({ schema }, json); | ||
} | ||
function nodeFromHTML(html, schema) { | ||
return nodeFromElement(elementFromHTML(html), schema); | ||
function jsonFromNode(node) { | ||
return node.toJSON(); | ||
} | ||
function jsonFromHTML(html, schema) { | ||
return jsonFromElement(elementFromHTML(html), schema); | ||
function nodeFromJSON(json, schema) { | ||
return schema.nodeFromJSON(json); | ||
} | ||
function elementFromHTML(html) { | ||
const win = getBrowserWindow(); | ||
if (!win) { | ||
throw new ProseKitError( | ||
"No Browser Document Found. You can only parse a HTML string in the browser environment." | ||
); | ||
function nodeFromElement(element, schema) { | ||
return DOMParser.fromSchema(schema).parse(element); | ||
} | ||
function elementFromNode(node, options) { | ||
const schema = node.type.schema; | ||
const serializer = DOMSerializer.fromSchema(schema); | ||
if (schema.topNodeType !== node.type) { | ||
return serializer.serializeNode(node, options); | ||
} | ||
const doc = getBrowserDocument(); | ||
const div = doc.createElement("div"); | ||
return serializer.serializeFragment(node.content, options, div); | ||
} | ||
function elementFromHTML(html, options) { | ||
const win = getBrowserWindow(options); | ||
const parser = new win.DOMParser(); | ||
return parser.parseFromString(`<body>${html}</body>`, "text/html").body; | ||
return parser.parseFromString(`<body><div>${html}</div></body>`, "text/html").body.firstElementChild; | ||
} | ||
function jsonFromState(state) { | ||
return state.toJSON(); | ||
function htmlFromElement(element) { | ||
return element.outerHTML; | ||
} | ||
function jsonFromNode(node) { | ||
return node.toJSON(); | ||
function nodeFromHTML(html, schema, options) { | ||
return nodeFromElement(elementFromHTML(html, options), schema); | ||
} | ||
function nodeFromJSON(json, schema) { | ||
return schema.nodeFromJSON(json); | ||
function htmlFromNode(node) { | ||
return elementFromNode(node).outerHTML; | ||
} | ||
function stateFromJSON(json, schema) { | ||
return EditorState.fromJSON({ schema }, json); | ||
function jsonFromElement(element, schema) { | ||
return jsonFromNode(nodeFromElement(element, schema)); | ||
} | ||
function elementFromJSON(json, schema, options) { | ||
return elementFromNode(nodeFromJSON(json, schema), options); | ||
} | ||
function jsonFromHTML(html, schema, options) { | ||
return jsonFromElement(elementFromHTML(html, options), schema); | ||
} | ||
function htmlFromJSON(json, schema, options) { | ||
return htmlFromElement(elementFromJSON(json, schema, options)); | ||
} | ||
@@ -1982,5 +2018,9 @@ // src/extensions/default-state.ts | ||
defineUpdateHandler, | ||
elementFromJSON, | ||
elementFromNode, | ||
expandMark, | ||
getMarkType, | ||
getNodeType, | ||
htmlFromJSON, | ||
htmlFromNode, | ||
insertNode, | ||
@@ -1993,3 +2033,2 @@ isAllSelection, | ||
isTextSelection, | ||
jsonFromElement, | ||
jsonFromHTML, | ||
@@ -1996,0 +2035,0 @@ jsonFromNode, |
{ | ||
"name": "@prosekit/core", | ||
"type": "module", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "author": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115064
3592