@prosekit/core
Advanced tools
Comparing version 0.0.0-next-20240602042411 to 0.0.0-next-20240605080819
@@ -10,4 +10,3 @@ // src/commands/add-mark.ts | ||
var ProseKitError = class extends Error { | ||
}; | ||
var EditorNotFoundError = class extends ProseKitError { | ||
}, EditorNotFoundError = class extends ProseKitError { | ||
constructor() { | ||
@@ -18,4 +17,3 @@ super( | ||
} | ||
}; | ||
var DOMDocumentNotFoundError = class extends ProseKitError { | ||
}, DOMDocumentNotFoundError = class extends ProseKitError { | ||
constructor() { | ||
@@ -30,7 +28,6 @@ super( | ||
function getMarkType(schema, type) { | ||
if (typeof type === "string") { | ||
const markType = schema.marks[type]; | ||
if (!markType) { | ||
if (typeof type == "string") { | ||
let markType = schema.marks[type]; | ||
if (!markType) | ||
throw new ProseKitError(`Cannot find mark type "${type}"`); | ||
} | ||
return markType; | ||
@@ -45,10 +42,4 @@ } | ||
var _a, _b; | ||
const mark = getMarkType(state.schema, options.type).create(options.attrs); | ||
const from = (_a = options.from) != null ? _a : state.selection.from; | ||
const to = (_b = options.to) != null ? _b : state.selection.to; | ||
if (from > to) { | ||
return false; | ||
} | ||
dispatch == null ? void 0 : dispatch(state.tr.addMark(from, to, mark)); | ||
return true; | ||
let mark = getMarkType(state.schema, options.type).create(options.attrs), from = (_a = options.from) != null ? _a : state.selection.from, to = (_b = options.to) != null ? _b : state.selection.to; | ||
return from > to ? !1 : (dispatch == null || dispatch(state.tr.addMark(from, to, mark)), !0); | ||
}; | ||
@@ -61,48 +52,22 @@ } | ||
return (state, dispatch) => { | ||
const markType = getMarkType(state.schema, options.type); | ||
const predicate = (mark) => mark.type === markType; | ||
const from = expandMarkBefore(state.selection.$from, predicate); | ||
const to = expandMarkAfter(state.selection.$to, predicate); | ||
if (from === state.selection.from && to === state.selection.to) { | ||
return false; | ||
} | ||
if (dispatch) { | ||
dispatch(state.tr.setSelection(TextSelection.create(state.doc, from, to))); | ||
} | ||
return true; | ||
let markType = getMarkType(state.schema, options.type), predicate = (mark) => mark.type === markType, from = expandMarkBefore(state.selection.$from, predicate), to = expandMarkAfter(state.selection.$to, predicate); | ||
return from === state.selection.from && to === state.selection.to ? !1 : (dispatch && dispatch(state.tr.setSelection(TextSelection.create(state.doc, from, to))), !0); | ||
}; | ||
} | ||
function expandMarkBefore($pos, predicate) { | ||
const { parent } = $pos; | ||
if (!$pos.marks().some(predicate)) { | ||
let { parent } = $pos; | ||
if (!$pos.marks().some(predicate)) | ||
return $pos.pos; | ||
} | ||
const index = $pos.index(); | ||
let boundaryIndex = index; | ||
for (let i = index; i >= 0; i--) { | ||
const node = parent.child(i); | ||
if (node.marks.some(predicate)) { | ||
boundaryIndex = i; | ||
} else { | ||
break; | ||
} | ||
} | ||
let index = $pos.index(), boundaryIndex = index; | ||
for (let i = index; i >= 0 && parent.child(i).marks.some(predicate); i--) | ||
boundaryIndex = i; | ||
return $pos.posAtIndex(boundaryIndex); | ||
} | ||
function expandMarkAfter($pos, predicate) { | ||
const { parent } = $pos; | ||
if (!$pos.marks().some(predicate)) { | ||
let { parent } = $pos; | ||
if (!$pos.marks().some(predicate)) | ||
return $pos.pos; | ||
} | ||
const index = Math.max(0, $pos.indexAfter() - 1); | ||
const childCount = parent.childCount; | ||
let boundaryIndex = index; | ||
for (let i = index; i < childCount; i++) { | ||
const node = parent.child(i); | ||
if (node.marks.some(predicate)) { | ||
boundaryIndex = i; | ||
} else { | ||
break; | ||
} | ||
} | ||
let index = Math.max(0, $pos.indexAfter() - 1), childCount = parent.childCount, boundaryIndex = index; | ||
for (let i = index; i < childCount && parent.child(i).marks.some(predicate); i++) | ||
boundaryIndex = i; | ||
return $pos.posAtIndex(boundaryIndex) + parent.child(boundaryIndex).nodeSize; | ||
@@ -117,5 +82,4 @@ } | ||
function assert(condition, message = "Assertion failed") { | ||
if (!condition) { | ||
if (!condition) | ||
throw new ProseKitError(message); | ||
} | ||
} | ||
@@ -126,7 +90,6 @@ | ||
function getNodeType(schema, type) { | ||
if (typeof type === "string") { | ||
const nodeType = schema.nodes[type]; | ||
if (!nodeType) { | ||
if (typeof type == "string") { | ||
let nodeType = schema.nodes[type]; | ||
if (!nodeType) | ||
throw new ProseKitError(`Cannot find ProseMirror node type "${type}"`); | ||
} | ||
return nodeType; | ||
@@ -140,5 +103,3 @@ } | ||
function setSelectionAround(tr, pos) { | ||
const docSize = tr.doc.content.size; | ||
const $pos = tr.doc.resolve(pos > docSize ? docSize : pos < 0 ? 0 : pos); | ||
const selection = TextSelection2.between($pos, $pos); | ||
let docSize = tr.doc.content.size, $pos = tr.doc.resolve(pos > docSize ? docSize : pos < 0 ? 0 : pos), selection = TextSelection2.between($pos, $pos); | ||
tr.setSelection(selection); | ||
@@ -151,5 +112,5 @@ } | ||
var _a; | ||
const node = options.node ? options.node : options.type ? getNodeType(state.schema, options.type).createAndFill(options.attrs) : null; | ||
let node = options.node ? options.node : options.type ? getNodeType(state.schema, options.type).createAndFill(options.attrs) : null; | ||
assert(node, "You must provide either a node or a type"); | ||
const insertPos = insertPoint( | ||
let insertPos = insertPoint( | ||
state.doc, | ||
@@ -159,10 +120,8 @@ (_a = options.pos) != null ? _a : state.selection.to, | ||
); | ||
if (insertPos == null) | ||
return false; | ||
if (insertPos == null) return !1; | ||
if (dispatch) { | ||
const tr = state.tr.insert(insertPos, node); | ||
setSelectionAround(tr, insertPos + node.nodeSize); | ||
dispatch(tr); | ||
let tr = state.tr.insert(insertPos, node); | ||
setSelectionAround(tr, insertPos + node.nodeSize), dispatch(tr); | ||
} | ||
return true; | ||
return !0; | ||
}; | ||
@@ -177,11 +136,4 @@ } | ||
var _a, _b; | ||
const markType = getMarkType(state.schema, options.type); | ||
const mark = options.attrs ? markType.create(options.attrs) : markType; | ||
const from = (_a = options.from) != null ? _a : state.selection.from; | ||
const to = (_b = options.to) != null ? _b : state.selection.to; | ||
if (from > to) { | ||
return false; | ||
} | ||
dispatch == null ? void 0 : dispatch(state.tr.removeMark(from, to, mark)); | ||
return true; | ||
let markType = getMarkType(state.schema, options.type), mark = options.attrs ? markType.create(options.attrs) : markType, from = (_a = options.from) != null ? _a : state.selection.from, to = (_b = options.to) != null ? _b : state.selection.to; | ||
return from > to ? !1 : (dispatch == null || dispatch(state.tr.removeMark(from, to, mark)), !0); | ||
}; | ||
@@ -196,6 +148,5 @@ } | ||
function getCustomSelection(state, from, to) { | ||
const pos = from != null ? from : to; | ||
let pos = from != null ? from : to; | ||
if (pos != null) { | ||
const $from = state.doc.resolve(from != null ? from : pos); | ||
const $to = state.doc.resolve(to != null ? to : pos); | ||
let $from = state.doc.resolve(from != null ? from : pos), $to = state.doc.resolve(to != null ? to : pos); | ||
return TextSelection3.between($from, $to); | ||
@@ -209,8 +160,5 @@ } | ||
return (state, dispatch) => { | ||
const nodeType = getNodeType(state.schema, options.type); | ||
const selection = getCustomSelection(state, options.from, options.to); | ||
const attrs = options.attrs; | ||
let applicable = false; | ||
let nodeType = getNodeType(state.schema, options.type), selection = getCustomSelection(state, options.from, options.to), attrs = options.attrs, applicable = !1; | ||
for (let i = 0; i < selection.ranges.length && !applicable; i++) { | ||
const { | ||
let { | ||
$from: { pos: from }, | ||
@@ -220,20 +168,17 @@ $to: { pos: to } | ||
state.doc.nodesBetween(from, to, (node, pos) => { | ||
if (applicable) | ||
return false; | ||
if (!node.isTextblock || node.hasMarkup(nodeType, attrs)) | ||
return; | ||
if (node.type == nodeType) { | ||
applicable = true; | ||
} else { | ||
const $pos = state.doc.resolve(pos), index = $pos.index(); | ||
applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType); | ||
} | ||
if (applicable) return !1; | ||
if (!(!node.isTextblock || node.hasMarkup(nodeType, attrs))) | ||
if (node.type == nodeType) | ||
applicable = !0; | ||
else { | ||
let $pos = state.doc.resolve(pos), index = $pos.index(); | ||
applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType); | ||
} | ||
}); | ||
} | ||
if (!applicable) | ||
return false; | ||
if (!applicable) return !1; | ||
if (dispatch) { | ||
const tr = state.tr; | ||
for (const range of selection.ranges) { | ||
const { | ||
let tr = state.tr; | ||
for (let range of selection.ranges) { | ||
let { | ||
$from: { pos: from }, | ||
@@ -246,3 +191,3 @@ $to: { pos: to } | ||
} | ||
return true; | ||
return !0; | ||
}; | ||
@@ -254,6 +199,3 @@ } | ||
function getNodeTypes(schema, types) { | ||
if (Array.isArray(types)) { | ||
return types.map((type) => getNodeType(schema, type)); | ||
} | ||
return [getNodeType(schema, types)]; | ||
return Array.isArray(types) ? types.map((type) => getNodeType(schema, type)) : [getNodeType(schema, types)]; | ||
} | ||
@@ -265,27 +207,16 @@ | ||
var _a, _b; | ||
const nodeTypes = getNodeTypes(state.schema, options.type); | ||
const from = (_a = options.pos) != null ? _a : state.selection.from; | ||
const to = (_b = options.pos) != null ? _b : state.selection.to; | ||
const positions = []; | ||
state.doc.nodesBetween(from, to, (node, pos) => { | ||
if (nodeTypes.includes(node.type)) { | ||
positions.push(pos); | ||
} | ||
if (!dispatch && positions.length > 0) { | ||
return false; | ||
} | ||
}); | ||
if (positions.length === 0) { | ||
return false; | ||
} | ||
let nodeTypes = getNodeTypes(state.schema, options.type), from = (_a = options.pos) != null ? _a : state.selection.from, to = (_b = options.pos) != null ? _b : state.selection.to, positions = []; | ||
if (state.doc.nodesBetween(from, to, (node, pos) => { | ||
if (nodeTypes.includes(node.type) && positions.push(pos), !dispatch && positions.length > 0) | ||
return !1; | ||
}), positions.length === 0) | ||
return !1; | ||
if (dispatch) { | ||
const { tr } = state; | ||
for (const pos of positions) { | ||
for (const [key, value] of Object.entries(options.attrs)) { | ||
let { tr } = state; | ||
for (let pos of positions) | ||
for (let [key, value] of Object.entries(options.attrs)) | ||
tr.setNodeAttribute(pos, key, value); | ||
} | ||
} | ||
dispatch(tr); | ||
} | ||
return true; | ||
return !0; | ||
}; | ||
@@ -298,68 +229,46 @@ } | ||
function markApplies(doc, ranges, type) { | ||
for (const { $from, $to } of ranges) { | ||
let can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) : false; | ||
doc.nodesBetween($from.pos, $to.pos, (node) => { | ||
if (can) | ||
return false; | ||
for (let { $from, $to } of ranges) { | ||
let can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) : !1; | ||
if (doc.nodesBetween($from.pos, $to.pos, (node) => { | ||
if (can) return !1; | ||
can = node.inlineContent && node.type.allowsMarkType(type); | ||
}); | ||
if (can) | ||
return true; | ||
}), can) return !0; | ||
} | ||
return false; | ||
return !1; | ||
} | ||
function baseToggleMark(markType, attrs = null, options) { | ||
const removeWhenPresent = (options && options.removeWhenPresent) !== false; | ||
let removeWhenPresent = (options && options.removeWhenPresent) !== !1; | ||
return function(state, dispatch) { | ||
const { empty, $cursor, ranges } = state.selection; | ||
let { empty, $cursor, ranges } = state.selection; | ||
if (empty && !$cursor || !markApplies(state.doc, ranges, markType)) | ||
return false; | ||
if (dispatch) { | ||
if ($cursor) { | ||
if (markType.isInSet(state.storedMarks || $cursor.marks())) | ||
dispatch(state.tr.removeStoredMark(markType)); | ||
else | ||
dispatch(state.tr.addStoredMark(markType.create(attrs))); | ||
} else { | ||
let add; | ||
const tr = state.tr; | ||
if (removeWhenPresent) { | ||
add = !ranges.some( | ||
(r) => state.doc.rangeHasMark(r.$from.pos, r.$to.pos, markType) | ||
); | ||
} else { | ||
add = !ranges.every((r) => { | ||
let missing = false; | ||
tr.doc.nodesBetween(r.$from.pos, r.$to.pos, (node, pos, parent) => { | ||
if (missing) | ||
return false; | ||
missing = !markType.isInSet(node.marks) && !!parent && parent.type.allowsMarkType(markType) && !(node.isText && /^\s*$/.test( | ||
node.textBetween( | ||
Math.max(0, r.$from.pos - pos), | ||
Math.min(node.nodeSize, r.$to.pos - pos) | ||
) | ||
)); | ||
}); | ||
return !missing; | ||
}); | ||
} | ||
for (const { $from, $to } of ranges) { | ||
if (!add) { | ||
return !1; | ||
if (dispatch) | ||
if ($cursor) | ||
markType.isInSet(state.storedMarks || $cursor.marks()) ? dispatch(state.tr.removeStoredMark(markType)) : dispatch(state.tr.addStoredMark(markType.create(attrs))); | ||
else { | ||
let add, tr = state.tr; | ||
removeWhenPresent ? add = !ranges.some( | ||
(r) => state.doc.rangeHasMark(r.$from.pos, r.$to.pos, markType) | ||
) : add = !ranges.every((r) => { | ||
let missing = !1; | ||
return tr.doc.nodesBetween(r.$from.pos, r.$to.pos, (node, pos, parent) => { | ||
if (missing) return !1; | ||
missing = !markType.isInSet(node.marks) && !!parent && parent.type.allowsMarkType(markType) && !(node.isText && /^\s*$/.test( | ||
node.textBetween( | ||
Math.max(0, r.$from.pos - pos), | ||
Math.min(node.nodeSize, r.$to.pos - pos) | ||
) | ||
)); | ||
}), !missing; | ||
}); | ||
for (let { $from, $to } of ranges) | ||
if (!add) | ||
tr.removeMark($from.pos, $to.pos, markType); | ||
} else { | ||
let from = $from.pos, to = $to.pos; | ||
const start = $from.nodeAfter, end = $to.nodeBefore; | ||
const spaceStart = start && start.isText ? /^\s*/.exec(start.text)[0].length : 0; | ||
const spaceEnd = end && end.isText ? /\s*$/.exec(end.text)[0].length : 0; | ||
if (from + spaceStart < to) { | ||
from += spaceStart; | ||
to -= spaceEnd; | ||
} | ||
tr.addMark(from, to, markType.create(attrs)); | ||
else { | ||
let from = $from.pos, to = $to.pos, start = $from.nodeAfter, end = $to.nodeBefore, spaceStart = start && start.isText ? /^\s*/.exec(start.text)[0].length : 0, spaceEnd = end && end.isText ? /\s*$/.exec(end.text)[0].length : 0; | ||
from + spaceStart < to && (from += spaceStart, to -= spaceEnd), tr.addMark(from, to, markType.create(attrs)); | ||
} | ||
} | ||
dispatch(tr.scrollIntoView()); | ||
} | ||
} | ||
return true; | ||
return !0; | ||
}; | ||
@@ -371,7 +280,5 @@ } | ||
}) { | ||
return (state, dispatch, view) => { | ||
return baseToggleMark(getMarkType(state.schema, type), attrs, { | ||
removeWhenPresent: false | ||
})(state, dispatch, view); | ||
}; | ||
return (state, dispatch, view) => baseToggleMark(getMarkType(state.schema, type), attrs, { | ||
removeWhenPresent: !1 | ||
})(state, dispatch, view); | ||
} | ||
@@ -386,9 +293,7 @@ | ||
function attrsMatch(nodeOrMark, attrs) { | ||
const currentAttrs = nodeOrMark.attrs; | ||
for (const [key, value] of Object.entries(attrs)) { | ||
if (currentAttrs[key] !== value) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
let currentAttrs = nodeOrMark.attrs; | ||
for (let [key, value] of Object.entries(attrs)) | ||
if (currentAttrs[key] !== value) | ||
return !1; | ||
return !0; | ||
} | ||
@@ -398,11 +303,9 @@ | ||
function isNodeActive(state, type, attrs) { | ||
const $pos = state.selection.$from; | ||
const nodeType = getNodeType(state.schema, type); | ||
let $pos = state.selection.$from, nodeType = getNodeType(state.schema, type); | ||
for (let depth = $pos.depth; depth >= 0; depth--) { | ||
const node = $pos.node(depth); | ||
if (node.type === nodeType && (!attrs || attrsMatch(node, attrs))) { | ||
return true; | ||
} | ||
let node = $pos.node(depth); | ||
if (node.type === nodeType && (!attrs || attrsMatch(node, attrs))) | ||
return !0; | ||
} | ||
return false; | ||
return !1; | ||
} | ||
@@ -417,9 +320,6 @@ | ||
if (isNodeActive(state, type, attrs)) { | ||
const defaultType = state.schema.topNodeType.contentMatch.defaultType; | ||
if (!defaultType) { | ||
return false; | ||
} | ||
return setBlockType2(defaultType)(state, dispatch, view); | ||
let defaultType = state.schema.topNodeType.contentMatch.defaultType; | ||
return defaultType ? setBlockType2(defaultType)(state, dispatch, view) : !1; | ||
} else { | ||
const nodeType = getNodeType(state.schema, type); | ||
let nodeType = getNodeType(state.schema, type); | ||
return setBlockType2(nodeType, attrs)(state, dispatch, view); | ||
@@ -439,10 +339,3 @@ } | ||
// src/types/priority.ts | ||
var Priority = /* @__PURE__ */ ((Priority2) => { | ||
Priority2[Priority2["lowest"] = 0] = "lowest"; | ||
Priority2[Priority2["low"] = 1] = "low"; | ||
Priority2[Priority2["default"] = 2] = "default"; | ||
Priority2[Priority2["high"] = 3] = "high"; | ||
Priority2[Priority2["highest"] = 4] = "highest"; | ||
return Priority2; | ||
})(Priority || {}); | ||
var Priority = /* @__PURE__ */ ((Priority2) => (Priority2[Priority2.lowest = 0] = "lowest", Priority2[Priority2.low = 1] = "low", Priority2[Priority2.default = 2] = "default", Priority2[Priority2.high = 3] = "high", Priority2[Priority2.highest = 4] = "highest", Priority2))(Priority || {}); | ||
@@ -454,8 +347,5 @@ // src/facets/base-extension.ts | ||
function uniqPush(prev, next) { | ||
const result = [...prev]; | ||
for (const item of next) { | ||
if (!result.includes(item)) { | ||
result.push(item); | ||
} | ||
} | ||
let result = [...prev]; | ||
for (let item of next) | ||
result.includes(item) || result.push(item); | ||
return result; | ||
@@ -482,17 +372,11 @@ } | ||
function unionInput(a, b) { | ||
if (!a && !b) | ||
return null; | ||
return uniqPush(a != null ? a : [], b != null ? b : []); | ||
return !a && !b ? null : uniqPush(a != null ? a : [], b != null ? b : []); | ||
} | ||
function subtractInput(a, b) { | ||
if (!a) | ||
return null; | ||
if (!b) | ||
return [...a]; | ||
return arraySubstract(a, b); | ||
return a ? b ? arraySubstract(a, b) : [...a] : null; | ||
} | ||
function unionChildren(a, b) { | ||
const merged = new Map(a); | ||
for (const [key, valueB] of b.entries()) { | ||
const valueA = a.get(key); | ||
let merged = new Map(a); | ||
for (let [key, valueB] of b.entries()) { | ||
let valueA = a.get(key); | ||
merged.set(key, valueA ? unionFacetNode(valueA, valueB) : valueB); | ||
@@ -503,8 +387,6 @@ } | ||
function subtractChildren(a, b) { | ||
const merged = new Map(a); | ||
for (const [key, valueB] of b.entries()) { | ||
const valueA = a.get(key); | ||
if (valueA) { | ||
merged.set(key, subtractFacetNode(valueA, valueB)); | ||
} | ||
let merged = new Map(a); | ||
for (let [key, valueB] of b.entries()) { | ||
let valueA = a.get(key); | ||
valueA && merged.set(key, subtractFacetNode(valueA, valueB)); | ||
} | ||
@@ -514,4 +396,3 @@ return merged; | ||
function unionFacetNode(a, b) { | ||
assert(a.facet === b.facet); | ||
return new FacetNode( | ||
return assert(a.facet === b.facet), new FacetNode( | ||
a.facet, | ||
@@ -523,4 +404,3 @@ zip5(a.inputs, b.inputs, unionInput), | ||
function subtractFacetNode(a, b) { | ||
assert(a.facet === b.facet); | ||
return new FacetNode( | ||
return assert(a.facet === b.facet), new FacetNode( | ||
a.facet, | ||
@@ -541,49 +421,35 @@ zip5(a.inputs, b.inputs, subtractInput), | ||
var _a, _b, _c; | ||
const inputs = [null, null, null, null, null]; | ||
const output = [null, null, null, null, null]; | ||
let inputs = [null, null, null, null, null], output = [null, null, null, null, null]; | ||
for (let pri = 0; pri < 5; pri++) { | ||
const input = this.inputs[pri]; | ||
if (input) { | ||
inputs[pri] = [...input]; | ||
} | ||
let input = this.inputs[pri]; | ||
input && (inputs[pri] = [...input]); | ||
} | ||
for (const child of this.children.values()) { | ||
const childOutput = child.getOutput(); | ||
for (let pri = 0; pri < 5; pri++) { | ||
if (childOutput[pri]) { | ||
const input = inputs[pri] || (inputs[pri] = []); | ||
input.push(childOutput[pri]); | ||
} | ||
} | ||
for (let child of this.children.values()) { | ||
let childOutput = child.getOutput(); | ||
for (let pri = 0; pri < 5; pri++) | ||
childOutput[pri] && (inputs[pri] || (inputs[pri] = [])).push(childOutput[pri]); | ||
} | ||
if (this.facet.singleton) { | ||
const reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer); | ||
const input = inputs.filter(Boolean).flat(); | ||
let reducer = (_a = this.reducers)[_b = 2 /* default */] || (_a[_b] = this.facet.reducer), input = inputs.filter(Boolean).flat(); | ||
output[2 /* default */] = reducer(input); | ||
} else { | ||
} else | ||
for (let pri = 0; pri < 5; pri++) { | ||
const input = inputs[pri]; | ||
let input = inputs[pri]; | ||
if (input) { | ||
const reducer = (_c = this.reducers)[pri] || (_c[pri] = this.facet.reducer); | ||
let reducer = (_c = this.reducers)[pri] || (_c[pri] = this.facet.reducer); | ||
output[pri] = reducer(input); | ||
} | ||
} | ||
} | ||
return output; | ||
} | ||
getOutput() { | ||
if (!this.output) { | ||
this.output = this.calcOutput(); | ||
} | ||
return this.output; | ||
return this.output || (this.output = this.calcOutput()), this.output; | ||
} | ||
getSingletonOutput() { | ||
assert(this.facet.singleton); | ||
return this.getOutput()[2 /* default */]; | ||
return assert(this.facet.singleton), this.getOutput()[2 /* default */]; | ||
} | ||
getRootOutput() { | ||
assert(this.isRoot()); | ||
const output = this.getSingletonOutput(); | ||
assert(output); | ||
return output; | ||
let output = this.getSingletonOutput(); | ||
return assert(output), output; | ||
} | ||
@@ -599,4 +465,3 @@ isRoot() { | ||
// src/facets/facet.ts | ||
var facetCount = 0; | ||
var Facet = class { | ||
var facetCount = 0, Facet = class { | ||
/** | ||
@@ -612,6 +477,3 @@ * @internal | ||
this.index = facetCount++; | ||
assert((_reduce || _reducer) && !(_reduce && _reducer)); | ||
this.parent = parent; | ||
this.singleton = singleton; | ||
this.path = parent ? [...parent.path, this.index] : []; | ||
assert((_reduce || _reducer) && !(_reduce && _reducer)), this.parent = parent, this.singleton = singleton, this.path = parent ? [...parent.path, this.index] : []; | ||
} | ||
@@ -627,3 +489,3 @@ get reducer() { | ||
options.parent, | ||
(_a = options.singleton) != null ? _a : false, | ||
(_a = options.singleton) != null ? _a : !1, | ||
options.reducer, | ||
@@ -637,13 +499,6 @@ options.reduce | ||
var _a; | ||
let schema; | ||
let commands; | ||
let stateFunc; | ||
let view; | ||
for (const input of inputs) { | ||
schema = input.schema || schema; | ||
commands = input.commands || commands; | ||
stateFunc = input.state || stateFunc; | ||
view = input.view || view; | ||
} | ||
const state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema }); | ||
let schema, commands, stateFunc, view; | ||
for (let input of inputs) | ||
schema = input.schema || schema, commands = input.commands || commands, stateFunc = input.state || stateFunc, view = input.view || view; | ||
let state = schema && ((_a = stateFunc == null ? void 0 : stateFunc({ schema })) != null ? _a : { schema }); | ||
return { schema, state, commands, view }; | ||
@@ -653,3 +508,3 @@ } | ||
null, | ||
true, | ||
!0, | ||
rootReducer | ||
@@ -662,8 +517,7 @@ ); | ||
assert(specs.length <= 1); | ||
const spec = specs[0]; | ||
const schema = spec ? new Schema4(spec) : null; | ||
return { schema }; | ||
let spec = specs[0]; | ||
return { schema: spec ? new Schema4(spec) : null }; | ||
}, | ||
parent: rootFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
@@ -682,3 +536,3 @@ | ||
var _a, _b; | ||
const pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */; | ||
let pri = (_a = priority != null ? priority : this.priority) != null ? _a : 2 /* default */; | ||
return (_b = this.trees)[pri] || (_b[pri] = this.createTree(pri)); | ||
@@ -692,5 +546,4 @@ } | ||
let node = this.getTree(); | ||
for (const index of facet.path) { | ||
for (let index of facet.path) | ||
node = node == null ? void 0 : node.children.get(index); | ||
} | ||
return (_a = node == null ? void 0 : node.getOutput()) != null ? _a : null; | ||
@@ -700,3 +553,3 @@ } | ||
var _a, _b; | ||
const output = this.findFacetOutput(schemaFacet); | ||
let output = this.findFacetOutput(schemaFacet); | ||
return (_b = (_a = output == null ? void 0 : output.find(Boolean)) == null ? void 0 : _a.schema) != null ? _b : null; | ||
@@ -721,8 +574,7 @@ } | ||
var _a; | ||
const pri = (_a = this.priority) != null ? _a : priority; | ||
const inputs = [null, null, null, null, null]; | ||
let pri = (_a = this.priority) != null ? _a : priority, inputs = [null, null, null, null, null]; | ||
inputs[pri] = [...this.payloads]; | ||
let node = new FacetNode(this.facet, inputs); | ||
while (node.facet.parent) { | ||
const children = /* @__PURE__ */ new Map([[node.facet.index, node]]); | ||
for (; node.facet.parent; ) { | ||
let children = /* @__PURE__ */ new Map([[node.facet.index, node]]); | ||
node = new FacetNode(node.facet.parent, void 0, children); | ||
@@ -740,7 +592,5 @@ } | ||
reduce: () => { | ||
let callbacks = []; | ||
const state = (ctx) => { | ||
let callbacks = [], state = (ctx) => { | ||
var _a, _b, _c, _d, _e, _f; | ||
const configs = callbacks.map((cb) => cb(ctx)); | ||
const config = { | ||
let configs = callbacks.map((cb) => cb(ctx)), config = { | ||
schema: ctx.schema, | ||
@@ -750,24 +600,14 @@ storedMarks: [], | ||
}; | ||
for (const c of configs) { | ||
config.schema = (_a = config.schema) != null ? _a : c.schema; | ||
config.doc = (_b = config.doc) != null ? _b : c.doc; | ||
config.selection = (_c = config.selection) != null ? _c : c.selection; | ||
config.storedMarks = [...config.storedMarks, ...(_d = c.storedMarks) != null ? _d : []]; | ||
config.plugins = uniqPush((_e = config.plugins) != null ? _e : [], (_f = c.plugins) != null ? _f : []); | ||
} | ||
assert( | ||
for (let c of configs) | ||
config.schema = (_a = config.schema) != null ? _a : c.schema, config.doc = (_b = config.doc) != null ? _b : c.doc, config.selection = (_c = config.selection) != null ? _c : c.selection, config.storedMarks = [...config.storedMarks, ...(_d = c.storedMarks) != null ? _d : []], config.plugins = uniqPush((_e = config.plugins) != null ? _e : [], (_f = c.plugins) != null ? _f : []); | ||
return assert( | ||
config.doc || config.schema, | ||
"Can't create state without a schema nor a document" | ||
); | ||
if (config.doc) { | ||
config.schema = void 0; | ||
} | ||
return config; | ||
), config.doc && (config.schema = void 0), config; | ||
}; | ||
return function reducer(inputs) { | ||
callbacks = inputs; | ||
return { state }; | ||
return function(inputs) { | ||
return callbacks = inputs, { state }; | ||
}; | ||
}, | ||
singleton: true, | ||
singleton: !0, | ||
parent: rootFacet | ||
@@ -782,16 +622,12 @@ }); | ||
function findGlobalBrowserDocument() { | ||
if (typeof document !== "undefined") { | ||
if (typeof document != "undefined") | ||
return document; | ||
} | ||
if (typeof globalThis !== "undefined" && globalThis.document) { | ||
if (typeof globalThis != "undefined" && globalThis.document) | ||
return globalThis.document; | ||
} | ||
} | ||
function findGlobalBrowserWindow() { | ||
if (typeof window !== "undefined") { | ||
if (typeof window != "undefined") | ||
return window; | ||
} | ||
if (typeof globalThis !== "undefined" && globalThis.window) { | ||
if (typeof globalThis != "undefined" && globalThis.window) | ||
return globalThis.window; | ||
} | ||
} | ||
@@ -807,11 +643,9 @@ function findBrowserDocument(options) { | ||
function getBrowserDocument(options) { | ||
const doc = findBrowserDocument(options); | ||
if (doc) | ||
return doc; | ||
let doc = findBrowserDocument(options); | ||
if (doc) return doc; | ||
throw new DOMDocumentNotFoundError(); | ||
} | ||
function getBrowserWindow(options) { | ||
const win = findBrowserWindow(options); | ||
if (win) | ||
return win; | ||
let win = findBrowserWindow(options); | ||
if (win) return win; | ||
throw new DOMDocumentNotFoundError(); | ||
@@ -834,25 +668,16 @@ } | ||
function nodeFromElement(element, options) { | ||
const Parser = options.DOMParser || DOMParser; | ||
const schema = options.schema; | ||
let Parser = options.DOMParser || DOMParser, schema = options.schema; | ||
return Parser.fromSchema(schema).parse(element); | ||
} | ||
function elementFromNode(node, options) { | ||
const Serializer = (options == null ? void 0 : options.DOMSerializer) || DOMSerializer; | ||
const document2 = getBrowserDocument(options); | ||
const schema = node.type.schema; | ||
const serializer = Serializer.fromSchema(schema); | ||
if (schema.topNodeType !== node.type) { | ||
return serializer.serializeNode(node, { document: document2 }); | ||
} else { | ||
return serializer.serializeFragment( | ||
node.content, | ||
{ document: document2 }, | ||
document2.createElement("div") | ||
); | ||
} | ||
let Serializer = (options == null ? void 0 : options.DOMSerializer) || DOMSerializer, document2 = getBrowserDocument(options), schema = node.type.schema, serializer = Serializer.fromSchema(schema); | ||
return schema.topNodeType !== node.type ? serializer.serializeNode(node, { document: document2 }) : serializer.serializeFragment( | ||
node.content, | ||
{ document: document2 }, | ||
document2.createElement("div") | ||
); | ||
} | ||
function elementFromHTML(html, options) { | ||
const win = getBrowserWindow(options); | ||
const parser = new win.DOMParser(); | ||
return parser.parseFromString(`<body><div>${html}</div></body>`, "text/html").body.firstElementChild; | ||
let win = getBrowserWindow(options); | ||
return new win.DOMParser().parseFromString(`<body><div>${html}</div></body>`, "text/html").body.firstElementChild; | ||
} | ||
@@ -887,24 +712,10 @@ function htmlFromElement(element) { | ||
}) { | ||
if (defaultHTML && defaultDoc) { | ||
if (defaultHTML && defaultDoc) | ||
throw new ProseKitError( | ||
"Only one of defaultHTML and defaultDoc can be provided" | ||
); | ||
} | ||
return defineFacetPayload(stateFacet, [ | ||
({ schema }) => { | ||
const config = {}; | ||
if (defaultHTML) { | ||
if (typeof defaultHTML === "string") { | ||
defaultDoc = jsonFromHTML(defaultHTML, { schema }); | ||
} else { | ||
defaultDoc = jsonFromElement(defaultHTML, { schema }); | ||
} | ||
} | ||
if (defaultDoc) { | ||
config.doc = schema.nodeFromJSON(defaultDoc); | ||
if (defaultSelection) { | ||
config.selection = Selection.fromJSON(config.doc, defaultSelection); | ||
} | ||
} | ||
return config; | ||
let config = {}; | ||
return defaultHTML && (typeof defaultHTML == "string" ? defaultDoc = jsonFromHTML(defaultHTML, { schema }) : defaultDoc = jsonFromElement(defaultHTML, { schema })), defaultDoc && (config.doc = schema.nodeFromJSON(defaultDoc), defaultSelection && (config.selection = Selection.fromJSON(config.doc, defaultSelection))), config; | ||
} | ||
@@ -917,20 +728,15 @@ ]); | ||
function deepEquals(a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (!a || !b) { | ||
return false; | ||
} | ||
if (Array.isArray(a) && Array.isArray(b)) { | ||
if (a === b) | ||
return !0; | ||
if (!a || !b) | ||
return !1; | ||
if (Array.isArray(a) && Array.isArray(b)) | ||
return a.length === b.length && a.every((x, i) => deepEquals(x, b[i])); | ||
} | ||
if (a instanceof OrderedMap && b instanceof OrderedMap) { | ||
if (a instanceof OrderedMap && b instanceof OrderedMap) | ||
return a.size === b.size && deepEquals(a.toObject(), b.toObject()); | ||
} | ||
if (typeof a === "object" && typeof b === "object") { | ||
const aKeys = Object.keys(a); | ||
const bKeys = Object.keys(b); | ||
if (typeof a == "object" && typeof b == "object") { | ||
let aKeys = Object.keys(a), bKeys = Object.keys(b); | ||
return aKeys.length === bKeys.length && aKeys.every((key) => deepEquals(a[key], b[key])); | ||
} | ||
return false; | ||
return !1; | ||
} | ||
@@ -943,10 +749,7 @@ | ||
function isMarkAbsent(node, from, to, markType, attrs) { | ||
const mark = attrs ? markType.create(attrs) : markType; | ||
let missing = false; | ||
node.nodesBetween(from, to, (node2, pos, parent) => { | ||
if (missing) | ||
return false; | ||
let mark = attrs ? markType.create(attrs) : markType, missing = !1; | ||
return node.nodesBetween(from, to, (node2, pos, parent) => { | ||
if (missing) return !1; | ||
missing = !mark.isInSet(node2.marks) && !!parent && parent.type.allowsMarkType(markType); | ||
}); | ||
return missing; | ||
}), missing; | ||
} | ||
@@ -956,10 +759,4 @@ | ||
function isMarkActive(state, type, attrs) { | ||
const { from, $from, to, empty } = state.selection; | ||
const markType = getMarkType(state.schema, type); | ||
if (empty) { | ||
const mark = attrs ? markType.create(attrs) : markType; | ||
return !!mark.isInSet(state.storedMarks || $from.marks()); | ||
} else { | ||
return !isMarkAbsent(state.doc, from, to, markType, attrs); | ||
} | ||
let { from, $from, to, empty } = state.selection, markType = getMarkType(state.schema, type); | ||
return empty ? !!(attrs ? markType.create(attrs) : markType).isInSet(state.storedMarks || $from.marks()) : !isMarkAbsent(state.doc, from, to, markType, attrs); | ||
} | ||
@@ -992,62 +789,44 @@ | ||
function createNodeBuilder(getState, type) { | ||
const builder = (...args) => buildNode(type, args); | ||
builder.isActive = (attrs) => { | ||
const state = getState(); | ||
return state ? isNodeActive(state, type, attrs) : false; | ||
}; | ||
return builder; | ||
let builder = (...args) => buildNode(type, args); | ||
return builder.isActive = (attrs) => { | ||
let state = getState(); | ||
return state ? isNodeActive(state, type, attrs) : !1; | ||
}, builder; | ||
} | ||
function createMarkBuilder(getState, type) { | ||
const builder = (...args) => buildMark(type, args); | ||
builder.isActive = (attrs) => { | ||
const state = getState(); | ||
return state ? isMarkActive(state, type, attrs) : false; | ||
}; | ||
return builder; | ||
let builder = (...args) => buildMark(type, args); | ||
return builder.isActive = (attrs) => { | ||
let state = getState(); | ||
return state ? isMarkActive(state, type, attrs) : !1; | ||
}, builder; | ||
} | ||
function buildMark(type, args) { | ||
const [attrs, children] = normalizeArgs(args); | ||
let [attrs, children] = normalizeArgs(args); | ||
return flattenChildren(type.schema, children, type.create(attrs)); | ||
} | ||
function buildNode(type, args) { | ||
const [attrs, children] = normalizeArgs(args); | ||
const node = type.createAndFill(attrs, flattenChildren(type.schema, children)); | ||
if (!node) { | ||
let [attrs, children] = normalizeArgs(args), node = type.createAndFill(attrs, flattenChildren(type.schema, children)); | ||
if (!node) | ||
throw new ProseKitError(`Couldn't create node ${type.name}`); | ||
} | ||
return node; | ||
} | ||
function flattenChildren(schema, children, mark) { | ||
const nodes = []; | ||
for (const child of children) { | ||
if (typeof child === "string") { | ||
if (child) { | ||
nodes.push(schema.text(child, mark ? [mark] : null)); | ||
} | ||
} else if (Array.isArray(child)) { | ||
let nodes = []; | ||
for (let child of children) | ||
if (typeof child == "string") | ||
child && nodes.push(schema.text(child, mark ? [mark] : null)); | ||
else if (Array.isArray(child)) | ||
nodes.push(...flattenChildren(schema, child, mark)); | ||
} else if (isProseMirrorNode(child)) { | ||
else if (isProseMirrorNode(child)) | ||
nodes.push(mark ? child.mark(mark.addToSet(child.marks)) : child); | ||
} else { | ||
else | ||
throw new ProseKitError(`Invalid node child: ${typeof child}`); | ||
} | ||
} | ||
return nodes; | ||
} | ||
function normalizeArgs(args) { | ||
const [attrs, ...children] = args; | ||
if (isNodeChild(attrs)) { | ||
children.unshift(attrs); | ||
return [null, children]; | ||
} else if (typeof attrs === "object") { | ||
return [attrs, children]; | ||
} else { | ||
return [null, children]; | ||
} | ||
let [attrs, ...children] = args; | ||
return isNodeChild(attrs) ? (children.unshift(attrs), [null, children]) : typeof attrs == "object" ? [attrs, children] : [null, children]; | ||
} | ||
function isNodeChild(value) { | ||
if (!value) { | ||
return false; | ||
} | ||
return typeof value === "string" || Array.isArray(value) || isProseMirrorNode(value); | ||
return value ? typeof value == "string" || Array.isArray(value) || isProseMirrorNode(value) : !1; | ||
} | ||
@@ -1069,10 +848,7 @@ | ||
var _a; | ||
const pri = (_a = this.priority) != null ? _a : priority; | ||
const extensions = [...this.extension]; | ||
extensions.sort((a, b) => { | ||
let pri = (_a = this.priority) != null ? _a : priority, extensions = [...this.extension]; | ||
return extensions.sort((a, b) => { | ||
var _a2, _b; | ||
return ((_a2 = a.priority) != null ? _a2 : pri) - ((_b = b.priority) != null ? _b : pri); | ||
}); | ||
const children = extensions.map((ext) => ext.getTree(pri)); | ||
return children.reduce(unionFacetNode, new FacetNode(rootFacet)); | ||
}), extensions.map((ext) => ext.getTree(pri)).reduce(unionFacetNode, new FacetNode(rootFacet)); | ||
} | ||
@@ -1083,3 +859,3 @@ }; | ||
function union(extension) { | ||
const array = Array.isArray(extension) ? extension : [extension]; | ||
let array = Array.isArray(extension) ? extension : [extension]; | ||
return new UnionExtensionImpl( | ||
@@ -1092,15 +868,11 @@ array | ||
function createEditor(options) { | ||
const { defaultDoc, defaultHTML, defaultSelection } = options; | ||
let extension = options.extension; | ||
if (defaultDoc || defaultHTML) { | ||
extension = union([ | ||
extension, | ||
defineDefaultState({ | ||
defaultDoc, | ||
defaultHTML, | ||
defaultSelection | ||
}) | ||
]); | ||
} | ||
return Editor.create(new EditorInstance(extension)); | ||
let { defaultDoc, defaultHTML, defaultSelection } = options, extension = options.extension; | ||
return (defaultDoc || defaultHTML) && (extension = union([ | ||
extension, | ||
defineDefaultState({ | ||
defaultDoc, | ||
defaultHTML, | ||
defaultSelection | ||
}) | ||
])), Editor.create(new EditorInstance(extension)); | ||
} | ||
@@ -1111,19 +883,11 @@ var EditorInstance = class { | ||
this.commandAppliers = {}; | ||
this.mount = this.mount.bind(this); | ||
this.unmount = this.unmount.bind(this); | ||
this.tree = extension.getTree(); | ||
const payload = this.tree.getRootOutput(); | ||
const schema = payload.schema; | ||
const stateConfig = payload.state; | ||
this.mount = this.mount.bind(this), this.unmount = this.unmount.bind(this), this.tree = extension.getTree(); | ||
let payload = this.tree.getRootOutput(), schema = payload.schema, stateConfig = payload.state; | ||
assert(schema && stateConfig, "Schema must be defined"); | ||
const state = EditorState2.create(stateConfig); | ||
this.cachedState = state; | ||
if (payload.commands) { | ||
for (const [name, commandCreator] of Object.entries(payload.commands)) { | ||
let state = EditorState2.create(stateConfig); | ||
if (this.cachedState = state, payload.commands) | ||
for (let [name, commandCreator] of Object.entries(payload.commands)) | ||
this.defineCommand(name, commandCreator); | ||
} | ||
} | ||
this.directEditorProps = { state, ...payload.view }; | ||
this.schema = this.directEditorProps.state.schema; | ||
const getState = () => this.getState(); | ||
this.directEditorProps = { state, ...payload.view }, this.schema = this.directEditorProps.state.schema; | ||
let getState = () => this.getState(); | ||
this.nodeBuilders = Object.fromEntries( | ||
@@ -1134,4 +898,3 @@ Object.values(this.schema.nodes).map((type) => [ | ||
]) | ||
); | ||
this.markBuilders = Object.fromEntries( | ||
), this.markBuilders = Object.fromEntries( | ||
Object.values(this.schema.marks).map((type) => [ | ||
@@ -1144,53 +907,38 @@ type.name, | ||
getState() { | ||
if (this.view) { | ||
this.cachedState = this.view.state; | ||
} | ||
return this.cachedState; | ||
return this.view && (this.cachedState = this.view.state), this.cachedState; | ||
} | ||
updateExtension(extension, add) { | ||
var _a, _b, _c, _d; | ||
const view = this.view; | ||
if (!view || view.isDestroyed) { | ||
let view = this.view; | ||
if (!view || view.isDestroyed) | ||
return; | ||
} | ||
const tree = extension.getTree(); | ||
const payload = tree.getRootOutput(); | ||
if (payload == null ? void 0 : payload.schema) { | ||
let tree = extension.getTree(), payload = tree.getRootOutput(); | ||
if (payload != null && payload.schema) | ||
throw new ProseKitError("Schema cannot be changed"); | ||
} | ||
if (payload == null ? void 0 : payload.view) { | ||
if (payload != null && payload.view) | ||
throw new ProseKitError("View cannot be changed"); | ||
} | ||
const oldPayload = this.tree.getRootOutput(); | ||
const oldPlugins = [...(_b = (_a = view.state) == null ? void 0 : _a.plugins) != null ? _b : []]; | ||
let oldPayload = this.tree.getRootOutput(), oldPlugins = [...(_b = (_a = view.state) == null ? void 0 : _a.plugins) != null ? _b : []]; | ||
this.tree = add ? unionFacetNode(this.tree, tree) : subtractFacetNode(this.tree, tree); | ||
const newPayload = this.tree.getRootOutput(); | ||
const newPlugins = [...(_d = (_c = newPayload == null ? void 0 : newPayload.state) == null ? void 0 : _c.plugins) != null ? _d : []]; | ||
let newPayload = this.tree.getRootOutput(), newPlugins = [...(_d = (_c = newPayload == null ? void 0 : newPayload.state) == null ? void 0 : _c.plugins) != null ? _d : []]; | ||
if (!deepEquals(oldPlugins, newPlugins)) { | ||
const state = view.state.reconfigure({ plugins: newPlugins }); | ||
let state = view.state.reconfigure({ plugins: newPlugins }); | ||
view.updateState(state); | ||
} | ||
if ((newPayload == null ? void 0 : newPayload.commands) && !deepEquals(oldPayload == null ? void 0 : oldPayload.commands, newPayload == null ? void 0 : newPayload.commands)) { | ||
const commands = newPayload.commands; | ||
const names = Object.keys(commands); | ||
for (const name of names) { | ||
if (newPayload != null && newPayload.commands && !deepEquals(oldPayload == null ? void 0 : oldPayload.commands, newPayload == null ? void 0 : newPayload.commands)) { | ||
let commands = newPayload.commands, names = Object.keys(commands); | ||
for (let name of names) | ||
this.defineCommand(name, commands[name]); | ||
} | ||
} | ||
} | ||
mount(place) { | ||
if (this.view) { | ||
if (this.view) | ||
throw new ProseKitError("Editor is already mounted"); | ||
} | ||
if (!place) { | ||
if (!place) | ||
throw new ProseKitError("Can't mount editor without a place"); | ||
} | ||
this.view = new EditorView({ mount: place }, this.directEditorProps); | ||
} | ||
unmount() { | ||
if (!this.view) { | ||
if (!this.view) | ||
throw new ProseKitError("Editor is not mounted yet"); | ||
} | ||
this.view.destroy(); | ||
this.view = null; | ||
this.view.destroy(), this.view = null; | ||
} | ||
@@ -1201,41 +949,25 @@ get mounted() { | ||
get assertView() { | ||
if (!this.view) { | ||
if (!this.view) | ||
throw new ProseKitError("Editor is not mounted"); | ||
} | ||
return this.view; | ||
} | ||
definePlugins(plugins) { | ||
const view = this.assertView; | ||
const state = view.state; | ||
const newPlugins = [...plugins, ...state.plugins]; | ||
const newState = state.reconfigure({ plugins: newPlugins }); | ||
let view = this.assertView, state = view.state, newPlugins = [...plugins, ...state.plugins], newState = state.reconfigure({ plugins: newPlugins }); | ||
view.setProps({ state: newState }); | ||
} | ||
removePlugins(plugins) { | ||
const view = this.view; | ||
if (!view) | ||
return; | ||
const state = view.state; | ||
const newPlugins = state.plugins.filter((p) => !plugins.includes(p)); | ||
const newState = state.reconfigure({ plugins: newPlugins }); | ||
let view = this.view; | ||
if (!view) return; | ||
let state = view.state, newPlugins = state.plugins.filter((p) => !plugins.includes(p)), newState = state.reconfigure({ plugins: newPlugins }); | ||
view.setProps({ state: newState }); | ||
} | ||
defineCommand(name, commandCreator) { | ||
const applier = (...args) => { | ||
const view = this.view; | ||
if (!view) { | ||
return false; | ||
} | ||
const command = commandCreator(...args); | ||
return command(view.state, view.dispatch.bind(view), view); | ||
let applier = (...args) => { | ||
let view = this.view; | ||
return view ? commandCreator(...args)(view.state, view.dispatch.bind(view), view) : !1; | ||
}; | ||
applier.canApply = (...args) => { | ||
const view = this.view; | ||
if (!view) { | ||
return false; | ||
} | ||
const command = commandCreator(...args); | ||
return command(view.state, void 0, view); | ||
}; | ||
this.commandAppliers[name] = applier; | ||
let view = this.view; | ||
return view ? commandCreator(...args)(view.state, void 0, view) : !1; | ||
}, this.commandAppliers[name] = applier; | ||
} | ||
@@ -1245,10 +977,6 @@ removeCommand(name) { | ||
} | ||
}; | ||
var Editor = class _Editor { | ||
}, Editor = class _Editor { | ||
constructor(instance) { | ||
this.afterMounted = []; | ||
this.instance = instance; | ||
this.mount = this.mount.bind(this); | ||
this.unmount = this.unmount.bind(this); | ||
this.use = this.use.bind(this); | ||
this.instance = instance, this.mount = this.mount.bind(this), this.unmount = this.unmount.bind(this), this.use = this.use.bind(this); | ||
} | ||
@@ -1259,5 +987,4 @@ /** | ||
static create(instance) { | ||
if (!(instance instanceof EditorInstance)) { | ||
if (!(instance instanceof EditorInstance)) | ||
throw new TypeError("Invalid EditorInstance"); | ||
} | ||
return new _Editor(instance); | ||
@@ -1291,3 +1018,3 @@ } | ||
var _a, _b; | ||
return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : false; | ||
return (_b = (_a = this.instance.view) == null ? void 0 : _a.hasFocus()) != null ? _b : !1; | ||
} | ||
@@ -1299,7 +1026,5 @@ /** | ||
mount(place) { | ||
if (!place) { | ||
if (!place) | ||
return this.unmount(); | ||
} | ||
this.instance.mount(place); | ||
this.afterMounted.forEach((callback) => callback()); | ||
this.instance.mount(place), this.afterMounted.forEach((callback) => callback()); | ||
} | ||
@@ -1310,5 +1035,3 @@ /** | ||
unmount() { | ||
if (this.mounted) { | ||
this.instance.unmount(); | ||
} | ||
this.mounted && this.instance.unmount(); | ||
} | ||
@@ -1320,3 +1043,3 @@ /** | ||
var _a; | ||
(_a = this.instance.view) == null ? void 0 : _a.focus(); | ||
(_a = this.instance.view) == null || _a.focus(); | ||
} | ||
@@ -1328,17 +1051,14 @@ /** | ||
var _a; | ||
(_a = this.instance.view) == null ? void 0 : _a.dom.blur(); | ||
(_a = this.instance.view) == null || _a.dom.blur(); | ||
} | ||
use(extension) { | ||
if (!this.mounted) { | ||
let lazyRemove = null; | ||
const lazyCreate = () => { | ||
let lazyRemove = null, lazyCreate = () => { | ||
lazyRemove = this.use(extension); | ||
}; | ||
this.afterMounted.push(lazyCreate); | ||
return () => { | ||
lazyRemove == null ? void 0 : lazyRemove(); | ||
return this.afterMounted.push(lazyCreate), () => { | ||
lazyRemove == null || lazyRemove(); | ||
}; | ||
} | ||
this.instance.updateExtension(extension, true); | ||
return () => this.instance.updateExtension(extension, false); | ||
return this.instance.updateExtension(extension, !0), () => this.instance.updateExtension(extension, !1); | ||
} | ||
@@ -1358,5 +1078,4 @@ get state() { | ||
function withPriority(extension, priority) { | ||
const result = union(extension); | ||
result.priority = priority; | ||
return result; | ||
let result = union(extension); | ||
return result.priority = priority, result; | ||
} | ||
@@ -1370,8 +1089,3 @@ | ||
}) { | ||
return (state, dispatch) => { | ||
if (text) { | ||
dispatch == null ? void 0 : dispatch(state.tr.insertText(text, from, to)); | ||
} | ||
return true; | ||
}; | ||
return (state, dispatch) => (text && (dispatch == null || dispatch(state.tr.insertText(text, from, to))), !0); | ||
} | ||
@@ -1382,6 +1096,3 @@ | ||
function selectAll() { | ||
return (state, dispatch) => { | ||
dispatch == null ? void 0 : dispatch(state.tr.setSelection(new AllSelection2(state.doc))); | ||
return true; | ||
}; | ||
return (state, dispatch) => (dispatch == null || dispatch(state.tr.setSelection(new AllSelection2(state.doc))), !0); | ||
} | ||
@@ -1398,11 +1109,6 @@ | ||
return (state, dispatch) => { | ||
const { $from, $to } = state.selection; | ||
const range = $from.blockRange($to); | ||
if (!range) | ||
return false; | ||
const wrapping = findWrapping(range, nodeType, attrs); | ||
if (!wrapping) | ||
return false; | ||
dispatch == null ? void 0 : dispatch(state.tr.wrap(range, wrapping)); | ||
return true; | ||
let { $from, $to } = state.selection, range = $from.blockRange($to); | ||
if (!range) return !1; | ||
let wrapping = findWrapping(range, nodeType, attrs); | ||
return wrapping ? (dispatch == null || dispatch(state.tr.wrap(range, wrapping)), !0) : !1; | ||
}; | ||
@@ -1413,8 +1119,5 @@ } | ||
var commandFacet = defineFacet({ | ||
reducer: (inputs) => { | ||
const commands = Object.assign({}, ...inputs); | ||
return { commands }; | ||
}, | ||
reducer: (inputs) => ({ commands: Object.assign({}, ...inputs) }), | ||
parent: rootFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
@@ -1448,18 +1151,13 @@ | ||
var _a; | ||
let nodes = OrderedMap2.from({}); | ||
let marks = OrderedMap2.from({}); | ||
let topNode = void 0; | ||
for (const spec of specs) { | ||
nodes = nodes.append(spec.nodes); | ||
marks = marks.append((_a = spec.marks) != null ? _a : {}); | ||
topNode = topNode != null ? topNode : spec.topNode; | ||
} | ||
let nodes = OrderedMap2.from({}), marks = OrderedMap2.from({}), topNode; | ||
for (let spec of specs) | ||
nodes = nodes.append(spec.nodes), marks = marks.append((_a = spec.marks) != null ? _a : {}), topNode = topNode != null ? topNode : spec.topNode; | ||
return { nodes, marks, topNode }; | ||
}, | ||
parent: schemaFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
// src/utils/is-element.ts | ||
var hasElement = typeof Element !== "undefined"; | ||
var hasElement = typeof Element != "undefined"; | ||
function isElement(value) { | ||
@@ -1476,23 +1174,13 @@ return hasElement && value instanceof Element; | ||
function defineNodeSpec(options) { | ||
const payload = [options, void 0]; | ||
return defineFacetPayload(nodeSpecFacet, [payload]); | ||
return defineFacetPayload(nodeSpecFacet, [[options, void 0]]); | ||
} | ||
function defineNodeAttr(options) { | ||
const payload = [void 0, options]; | ||
return defineFacetPayload(nodeSpecFacet, [payload]); | ||
return defineFacetPayload(nodeSpecFacet, [[void 0, options]]); | ||
} | ||
var nodeSpecFacet = defineFacet({ | ||
reducer: (payloads) => { | ||
let nodes = OrderedMap3.from({}); | ||
let topNodeName = void 0; | ||
const specPayloads = payloads.map((input) => input[0]).filter(isNotNull); | ||
const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
for (const { name, topNode, ...spec } of specPayloads) { | ||
assert(!nodes.get(name), `Node type ${name} can only be defined once`); | ||
if (topNode) { | ||
topNodeName = name; | ||
} | ||
nodes = nodes.addToStart(name, spec); | ||
} | ||
for (const { | ||
let nodes = OrderedMap3.from({}), topNodeName, specPayloads = payloads.map((input) => input[0]).filter(isNotNull), attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
for (let { name, topNode, ...spec } of specPayloads) | ||
assert(!nodes.get(name), `Node type ${name} can only be defined once`), topNode && (topNodeName = name), nodes = nodes.addToStart(name, spec); | ||
for (let { | ||
type, | ||
@@ -1505,59 +1193,36 @@ attr, | ||
} of attrPayloads) { | ||
const spec = nodes.get(type); | ||
assert(spec, `Node type ${type} must be defined`); | ||
if (!spec.attrs) { | ||
spec.attrs = {}; | ||
} | ||
spec.attrs[attr] = { | ||
let spec = nodes.get(type); | ||
if (assert(spec, `Node type ${type} must be defined`), spec.attrs || (spec.attrs = {}), spec.attrs[attr] = { | ||
default: defaultValue, | ||
splittable | ||
}; | ||
if (toDOM && spec.toDOM) { | ||
const existingToDom = spec.toDOM; | ||
}, toDOM && spec.toDOM) { | ||
let existingToDom = spec.toDOM; | ||
spec.toDOM = (node) => { | ||
const dom = existingToDom(node); | ||
if (!dom) { | ||
let dom = existingToDom(node); | ||
if (!dom) | ||
return dom; | ||
} | ||
const attrDOM = toDOM(node.attrs[attr]); | ||
if (!attrDOM) { | ||
let attrDOM = toDOM(node.attrs[attr]); | ||
if (!attrDOM) | ||
return dom; | ||
} | ||
const [key, value] = attrDOM; | ||
if (!key) { | ||
return dom; | ||
} | ||
if (Array.isArray(dom)) { | ||
if (typeof dom[1] === "object") { | ||
return [ | ||
dom[0], | ||
setObjectAttribute( | ||
dom[1], | ||
key, | ||
value | ||
), | ||
...dom.slice(2) | ||
]; | ||
} else { | ||
return [dom[0], { [key]: value }, ...dom.slice(1)]; | ||
} | ||
} else if (isElement(dom)) { | ||
setElementAttribute(dom, key, value); | ||
} else if (typeof dom === "object" && "dom" in dom && isElement(dom.dom)) { | ||
setElementAttribute(dom.dom, key, value); | ||
} | ||
return dom; | ||
let [key, value] = attrDOM; | ||
return key ? Array.isArray(dom) ? typeof dom[1] == "object" ? [ | ||
dom[0], | ||
setObjectAttribute( | ||
dom[1], | ||
key, | ||
value | ||
), | ||
...dom.slice(2) | ||
] : [dom[0], { [key]: value }, ...dom.slice(1)] : (isElement(dom) ? setElementAttribute(dom, key, value) : typeof dom == "object" && "dom" in dom && isElement(dom.dom) && setElementAttribute(dom.dom, key, value), dom) : dom; | ||
}; | ||
} | ||
if (parseDOM && spec.parseDOM) { | ||
for (const rule of spec.parseDOM) { | ||
const existingGetAttrs = rule.getAttrs; | ||
const existingAttrs = rule.attrs; | ||
if (parseDOM && spec.parseDOM) | ||
for (let rule of spec.parseDOM) { | ||
let existingGetAttrs = rule.getAttrs, existingAttrs = rule.attrs; | ||
rule.getAttrs = (dom) => { | ||
var _a; | ||
const attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs; | ||
if (attrs === false || !dom || !isElement(dom)) { | ||
let attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs; | ||
if (attrs === !1 || !dom || !isElement(dom)) | ||
return attrs != null ? attrs : null; | ||
} | ||
const value = parseDOM(dom); | ||
let value = parseDOM(dom); | ||
return { | ||
@@ -1569,3 +1234,2 @@ ...attrs, | ||
} | ||
} | ||
} | ||
@@ -1575,15 +1239,9 @@ return { nodes, topNode: topNodeName }; | ||
parent: schemaSpecFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
function setObjectAttribute(obj, key, value) { | ||
if (key === "style") { | ||
value = `${value}${obj.style || ""}`; | ||
} | ||
return { ...obj, [key]: value }; | ||
return key === "style" && (value = `${value}${obj.style || ""}`), { ...obj, [key]: value }; | ||
} | ||
function setElementAttribute(element, key, value) { | ||
if (key === "style") { | ||
value = `${value}${element.getAttribute("style") || ""}`; | ||
} | ||
element.setAttribute(key, value); | ||
key === "style" && (value = `${value}${element.getAttribute("style") || ""}`), element.setAttribute(key, value); | ||
} | ||
@@ -1596,3 +1254,3 @@ | ||
content: "block+", | ||
topNode: true | ||
topNode: !0 | ||
}); | ||
@@ -1608,31 +1266,23 @@ } | ||
function definePlugin(plugin) { | ||
if (plugin instanceof Plugin2) { | ||
if (plugin instanceof Plugin2) | ||
return defineFacetPayload(pluginFacet, [() => [plugin]]); | ||
} | ||
if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin2)) { | ||
if (Array.isArray(plugin) && plugin.every((p) => p instanceof Plugin2)) | ||
return defineFacetPayload(pluginFacet, [() => plugin]); | ||
} | ||
if (typeof plugin === "function") { | ||
if (typeof plugin == "function") | ||
return defineFacetPayload(pluginFacet, [plugin]); | ||
} | ||
throw new TypeError("Invalid plugin"); | ||
} | ||
var pluginFacet = defineFacet({ | ||
reducer: (payloads) => { | ||
return ({ schema }) => { | ||
const plugins = []; | ||
for (const payload of payloads) { | ||
if (payload instanceof Plugin2) { | ||
plugins.push(payload); | ||
} else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin2)) { | ||
plugins.push(...payload); | ||
} else if (typeof payload === "function") { | ||
plugins.push(...[payload({ schema })].flat()); | ||
} else { | ||
throw new ProseKitError("Invalid plugin"); | ||
} | ||
} | ||
plugins.reverse(); | ||
return { plugins }; | ||
}; | ||
reducer: (payloads) => ({ schema }) => { | ||
let plugins = []; | ||
for (let payload of payloads) | ||
if (payload instanceof Plugin2) | ||
plugins.push(payload); | ||
else if (Array.isArray(payload) && payload.every((p) => p instanceof Plugin2)) | ||
plugins.push(...payload); | ||
else if (typeof payload == "function") | ||
plugins.push(...[payload({ schema })].flat()); | ||
else | ||
throw new ProseKitError("Invalid plugin"); | ||
return plugins.reverse(), { plugins }; | ||
}, | ||
@@ -1654,24 +1304,15 @@ parent: stateFacet | ||
reduce: () => { | ||
let mountHandlers = []; | ||
let updateHandlers = []; | ||
let unmountHandlers = []; | ||
const plugin = new ProseMirrorPlugin2({ | ||
let mountHandlers = [], updateHandlers = [], unmountHandlers = [], plugin = new ProseMirrorPlugin2({ | ||
key: pluginKey, | ||
view: (view) => { | ||
mountHandlers.forEach((fn) => fn(view)); | ||
return { | ||
update: (view2, prevState) => { | ||
updateHandlers.forEach((fn) => fn(view2, prevState)); | ||
}, | ||
destroy: () => { | ||
unmountHandlers.forEach((fn) => fn()); | ||
} | ||
}; | ||
} | ||
}); | ||
const register = (input) => { | ||
mountHandlers = []; | ||
updateHandlers = []; | ||
unmountHandlers = []; | ||
for (const args of input) { | ||
view: (view) => (mountHandlers.forEach((fn) => fn(view)), { | ||
update: (view2, prevState) => { | ||
updateHandlers.forEach((fn) => fn(view2, prevState)); | ||
}, | ||
destroy: () => { | ||
unmountHandlers.forEach((fn) => fn()); | ||
} | ||
}) | ||
}), register = (input) => { | ||
mountHandlers = [], updateHandlers = [], unmountHandlers = []; | ||
for (let args of input) | ||
switch (args[0]) { | ||
@@ -1688,13 +1329,10 @@ case "mount": | ||
} | ||
} | ||
}; | ||
return function reducer(input) { | ||
register(input); | ||
return plugin; | ||
return function(input) { | ||
return register(input), plugin; | ||
}; | ||
}, | ||
parent: pluginFacet, | ||
singleton: true | ||
}); | ||
var pluginKey = new PluginKey("prosekit-plugin-view-handler"); | ||
singleton: !0 | ||
}), pluginKey = new PluginKey("prosekit-plugin-view-handler"); | ||
@@ -1704,5 +1342,3 @@ // src/extensions/events/doc-change.ts | ||
return defineUpdateHandler((view, prevState) => { | ||
if (!view.state.doc.eq(prevState.doc)) { | ||
handler(view, prevState); | ||
} | ||
view.state.doc.eq(prevState.doc) || handler(view, prevState); | ||
}); | ||
@@ -1721,8 +1357,6 @@ } | ||
function combinedEventHandler(...args) { | ||
for (const handler of _handlers) { | ||
if (handler(...args)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
for (let handler of _handlers) | ||
if (handler(...args)) | ||
return !0; | ||
return !1; | ||
} | ||
@@ -1734,10 +1368,6 @@ return [setHandlers, combinedEventHandler]; | ||
function groupEntries(entries) { | ||
const map = {}; | ||
for (const [key, value] of entries) { | ||
const values = map[key]; | ||
if (!values) { | ||
map[key] = [value]; | ||
} else { | ||
values.push(value); | ||
} | ||
let map = {}; | ||
for (let [key, value] of entries) { | ||
let values = map[key]; | ||
values ? values.push(value) : map[key] = [value]; | ||
} | ||
@@ -1755,38 +1385,29 @@ return map; | ||
reduce: () => { | ||
const setHandlersMap = {}; | ||
const combinedHandlerMap = {}; | ||
let plugin = null; | ||
const update = (payloads) => { | ||
let setHandlersMap = {}, combinedHandlerMap = {}, plugin = null, update = (payloads) => { | ||
var _a; | ||
let hasNewEvent = false; | ||
for (const [event] of payloads) { | ||
let hasNewEvent = !1; | ||
for (let [event] of payloads) | ||
if (!setHandlersMap[event]) { | ||
hasNewEvent = true; | ||
const [setHandlers, combinedHandler] = combineEventHandlers(); | ||
hasNewEvent = !0; | ||
let [setHandlers, combinedHandler] = combineEventHandlers(); | ||
setHandlersMap[event] = setHandlers; | ||
const e = (view, eventObject) => { | ||
return combinedHandler(view, eventObject); | ||
}; | ||
let e = (view, eventObject) => combinedHandler(view, eventObject); | ||
combinedHandlerMap[event] = e; | ||
} | ||
} | ||
const map = groupEntries(payloads); | ||
for (const [event, setHandlers] of Object.entries(setHandlersMap)) { | ||
const handlers = (_a = map[event]) != null ? _a : []; | ||
let map = groupEntries(payloads); | ||
for (let [event, setHandlers] of Object.entries(setHandlersMap)) { | ||
let handlers = (_a = map[event]) != null ? _a : []; | ||
setHandlers(handlers); | ||
} | ||
if (hasNewEvent) { | ||
plugin = new ProseMirrorPlugin3({ | ||
key: new PluginKey2("prosekit-dom-event-handler"), | ||
props: { handleDOMEvents: combinedHandlerMap } | ||
}); | ||
} | ||
hasNewEvent && (plugin = new ProseMirrorPlugin3({ | ||
key: new PluginKey2("prosekit-dom-event-handler"), | ||
props: { handleDOMEvents: combinedHandlerMap } | ||
})); | ||
}; | ||
return function reducer(inputs) { | ||
update(inputs); | ||
return plugin != null ? plugin : []; | ||
return function(inputs) { | ||
return update(inputs), plugin != null ? plugin : []; | ||
}; | ||
}, | ||
parent: pluginFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
@@ -1834,41 +1455,14 @@ | ||
reduce: () => { | ||
const [update, plugin] = setupEditorEventPlugin(); | ||
return (entries) => { | ||
update(entries); | ||
return plugin; | ||
}; | ||
let [update, plugin] = setupEditorEventPlugin(); | ||
return (entries) => (update(entries), plugin); | ||
}, | ||
parent: pluginFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
function setupEditorEventPlugin() { | ||
const [setKeyDownHandlers, handleKeyDown] = combineEventHandlers(); | ||
const [setKeyPressHandlers, handleKeyPress] = combineEventHandlers(); | ||
const [setTextInputHandlers, handleTextInput] = combineEventHandlers(); | ||
const [setClickOnHandlers, handleClickOn] = combineEventHandlers(); | ||
const [setClickHandlers, handleClick] = combineEventHandlers(); | ||
const [setDoubleClickOnHandlers, handleDoubleClickOn] = combineEventHandlers(); | ||
const [setDoubleClickHandlers, handleDoubleClick] = combineEventHandlers(); | ||
const [setTripleClickOnHandlers, handleTripleClickOn] = combineEventHandlers(); | ||
const [setTripleClickHandlers, handleTripleClick] = combineEventHandlers(); | ||
const [setPasteHandlers, handlePaste] = combineEventHandlers(); | ||
const [setDropHandlers, handleDrop] = combineEventHandlers(); | ||
const [setScrollToSelectionHandlers, handleScrollToSelection] = combineEventHandlers(); | ||
const update = (entries) => { | ||
let [setKeyDownHandlers, handleKeyDown] = combineEventHandlers(), [setKeyPressHandlers, handleKeyPress] = combineEventHandlers(), [setTextInputHandlers, handleTextInput] = combineEventHandlers(), [setClickOnHandlers, handleClickOn] = combineEventHandlers(), [setClickHandlers, handleClick] = combineEventHandlers(), [setDoubleClickOnHandlers, handleDoubleClickOn] = combineEventHandlers(), [setDoubleClickHandlers, handleDoubleClick] = combineEventHandlers(), [setTripleClickOnHandlers, handleTripleClickOn] = combineEventHandlers(), [setTripleClickHandlers, handleTripleClick] = combineEventHandlers(), [setPasteHandlers, handlePaste] = combineEventHandlers(), [setDropHandlers, handleDrop] = combineEventHandlers(), [setScrollToSelectionHandlers, handleScrollToSelection] = combineEventHandlers(), update = (entries) => { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l; | ||
const map = groupEntries(entries); | ||
setKeyDownHandlers((_a = map.keyDown) != null ? _a : []); | ||
setKeyPressHandlers((_b = map.keyPress) != null ? _b : []); | ||
setTextInputHandlers((_c = map.textInput) != null ? _c : []); | ||
setClickOnHandlers((_d = map.clickOn) != null ? _d : []); | ||
setClickHandlers((_e = map.click) != null ? _e : []); | ||
setDoubleClickOnHandlers((_f = map.doubleClickOn) != null ? _f : []); | ||
setDoubleClickHandlers((_g = map.doubleClick) != null ? _g : []); | ||
setTripleClickOnHandlers((_h = map.tripleClickOn) != null ? _h : []); | ||
setTripleClickHandlers((_i = map.tripleClick) != null ? _i : []); | ||
setPasteHandlers((_j = map.paste) != null ? _j : []); | ||
setDropHandlers((_k = map.drop) != null ? _k : []); | ||
setScrollToSelectionHandlers((_l = map.scrollToSelection) != null ? _l : []); | ||
}; | ||
const plugin = new ProseMirrorPlugin4({ | ||
let map = groupEntries(entries); | ||
setKeyDownHandlers((_a = map.keyDown) != null ? _a : []), setKeyPressHandlers((_b = map.keyPress) != null ? _b : []), setTextInputHandlers((_c = map.textInput) != null ? _c : []), setClickOnHandlers((_d = map.clickOn) != null ? _d : []), setClickHandlers((_e = map.click) != null ? _e : []), setDoubleClickOnHandlers((_f = map.doubleClickOn) != null ? _f : []), setDoubleClickHandlers((_g = map.doubleClick) != null ? _g : []), setTripleClickOnHandlers((_h = map.tripleClickOn) != null ? _h : []), setTripleClickHandlers((_i = map.tripleClick) != null ? _i : []), setPasteHandlers((_j = map.paste) != null ? _j : []), setDropHandlers((_k = map.drop) != null ? _k : []), setScrollToSelectionHandlers((_l = map.scrollToSelection) != null ? _l : []); | ||
}, plugin = new ProseMirrorPlugin4({ | ||
key: new PluginKey3("prosekit-editor-event"), | ||
@@ -1895,7 +1489,5 @@ props: { | ||
function defineFocusChangeHandler(handler) { | ||
const handleFocus = () => handler(true); | ||
const handleBlur = () => handler(false); | ||
return defineFacetPayload(domEventFacet, [ | ||
["focus", handleFocus], | ||
["blur", handleBlur] | ||
["focus", () => handler(!0)], | ||
["blur", () => handler(!1)] | ||
]); | ||
@@ -1908,3 +1500,3 @@ } | ||
// src/utils/env.ts | ||
var isApple = typeof navigator !== "undefined" ? /Mac|iP(hone|[ao]d)/.test(navigator.platform) : false; | ||
var isApple = typeof navigator != "undefined" ? /Mac|iP(hone|[ao]d)/.test(navigator.platform) : !1; | ||
@@ -1936,3 +1528,3 @@ // src/extensions/keymap.ts | ||
var _a; | ||
const priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 1 /* low */; | ||
let priority = (_a = options == null ? void 0 : options.priority) != null ? _a : 1 /* low */; | ||
return withPriority(defineKeymap(customBaseKeymap), priority); | ||
@@ -1942,33 +1534,21 @@ } | ||
reduce: () => { | ||
let handler = null; | ||
const handlerWrapper = (view, event) => { | ||
if (handler) | ||
return handler(view, event); | ||
return false; | ||
}; | ||
const plugin = new Plugin3({ | ||
let handler = null, handlerWrapper = (view, event) => handler ? handler(view, event) : !1, plugin = new Plugin3({ | ||
key: keymapPluginKey, | ||
props: { handleKeyDown: handlerWrapper } | ||
}); | ||
return (keymaps) => { | ||
handler = keydownHandler( | ||
mergeKeymaps( | ||
// The keymap at the end have a higher priority. | ||
toReversed(keymaps) | ||
) | ||
); | ||
return plugin; | ||
}; | ||
return (keymaps) => (handler = keydownHandler( | ||
mergeKeymaps( | ||
// The keymap at the end have a higher priority. | ||
toReversed(keymaps) | ||
) | ||
), plugin); | ||
}, | ||
parent: pluginFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
function mergeKeymaps(keymaps) { | ||
const bindings = {}; | ||
for (const keymap of keymaps) { | ||
for (const [key, command] of Object.entries(keymap)) { | ||
const commands = bindings[key] || (bindings[key] = []); | ||
commands.push(command); | ||
} | ||
} | ||
let bindings = {}; | ||
for (let keymap of keymaps) | ||
for (let [key, command] of Object.entries(keymap)) | ||
(bindings[key] || (bindings[key] = [])).push(command); | ||
return Object.fromEntries( | ||
@@ -1985,10 +1565,7 @@ Object.entries(bindings).map(([key, commands]) => [ | ||
function defineHistory() { | ||
const keymap = { | ||
let keymap = { | ||
"Mod-z": undo, | ||
"Shift-Mod-z": redo | ||
}; | ||
if (!isApple) { | ||
keymap["Mod-y"] = redo; | ||
} | ||
return union([ | ||
return isApple || (keymap["Mod-y"] = redo), union([ | ||
definePlugin(history()), | ||
@@ -2005,21 +1582,16 @@ defineKeymap(keymap), | ||
function defineMarkSpec(options) { | ||
const payload = [options, void 0]; | ||
return defineFacetPayload(markSpecFacet, [payload]); | ||
return defineFacetPayload(markSpecFacet, [[options, void 0]]); | ||
} | ||
function defineMarkAttr(options) { | ||
const payload = [void 0, options]; | ||
return defineFacetPayload(markSpecFacet, [payload]); | ||
return defineFacetPayload(markSpecFacet, [[void 0, options]]); | ||
} | ||
var markSpecFacet = defineFacet({ | ||
reducer: (payloads) => { | ||
const marks = {}; | ||
const specPayloads = payloads.map((input) => input[0]).filter(isNotNull); | ||
const attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
for (const { name, ...spec } of specPayloads) { | ||
if (marks[name]) { | ||
let marks = {}, specPayloads = payloads.map((input) => input[0]).filter(isNotNull), attrPayloads = payloads.map((input) => input[1]).filter(isNotNull); | ||
for (let { name, ...spec } of specPayloads) { | ||
if (marks[name]) | ||
throw new ProseKitError(`Mark type ${name} has already been defined`); | ||
} | ||
marks[name] = spec; | ||
} | ||
for (const { | ||
for (let { | ||
type, | ||
@@ -2031,52 +1603,29 @@ attr, | ||
} of attrPayloads) { | ||
const spec = marks[type]; | ||
if (!spec) { | ||
let spec = marks[type]; | ||
if (!spec) | ||
throw new ProseKitError( | ||
`Mark type ${type} must be defined before defining attributes` | ||
); | ||
} | ||
if (!spec.attrs) { | ||
spec.attrs = {}; | ||
} | ||
spec.attrs[attr] = { default: defaultValue }; | ||
if (toDOM && spec.toDOM) { | ||
const existingToDom = spec.toDOM; | ||
if (spec.attrs || (spec.attrs = {}), spec.attrs[attr] = { default: defaultValue }, toDOM && spec.toDOM) { | ||
let existingToDom = spec.toDOM; | ||
spec.toDOM = (mark, inline) => { | ||
const dom = existingToDom(mark, inline); | ||
if (!dom) { | ||
let dom = existingToDom(mark, inline); | ||
if (!dom) | ||
return dom; | ||
} | ||
const attrDOM = toDOM(mark.attrs[attr]); | ||
if (!attrDOM) { | ||
let attrDOM = toDOM(mark.attrs[attr]); | ||
if (!attrDOM) | ||
return dom; | ||
} | ||
const [key, value] = attrDOM; | ||
if (!key) { | ||
return dom; | ||
} | ||
if (Array.isArray(dom)) { | ||
if (typeof dom[1] === "object") { | ||
return [dom[0], { ...dom[1], [key]: value }, ...dom.slice(2)]; | ||
} else { | ||
return [dom[0], { [key]: value }, ...dom.slice(1)]; | ||
} | ||
} else if (isElement(dom)) { | ||
dom.setAttribute(key, value); | ||
} else if (typeof dom === "object" && "dom" in dom && isElement(dom.dom)) { | ||
dom.dom.setAttribute(key, value); | ||
} | ||
return dom; | ||
let [key, value] = attrDOM; | ||
return key ? Array.isArray(dom) ? typeof dom[1] == "object" ? [dom[0], { ...dom[1], [key]: value }, ...dom.slice(2)] : [dom[0], { [key]: value }, ...dom.slice(1)] : (isElement(dom) ? dom.setAttribute(key, value) : typeof dom == "object" && "dom" in dom && isElement(dom.dom) && dom.dom.setAttribute(key, value), dom) : dom; | ||
}; | ||
} | ||
if (parseDOM && spec.parseDOM) { | ||
for (const rule of spec.parseDOM) { | ||
const existingGetAttrs = rule.getAttrs; | ||
const existingAttrs = rule.attrs; | ||
if (parseDOM && spec.parseDOM) | ||
for (let rule of spec.parseDOM) { | ||
let existingGetAttrs = rule.getAttrs, existingAttrs = rule.attrs; | ||
rule.getAttrs = (dom) => { | ||
var _a; | ||
const attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs; | ||
if (attrs === false || !dom || !isElement(dom)) { | ||
let attrs = (_a = existingGetAttrs == null ? void 0 : existingGetAttrs(dom)) != null ? _a : existingAttrs; | ||
if (attrs === !1 || !dom || !isElement(dom)) | ||
return attrs != null ? attrs : null; | ||
} | ||
const value = parseDOM(dom); | ||
let value = parseDOM(dom); | ||
return { | ||
@@ -2088,3 +1637,2 @@ ...attrs, | ||
} | ||
} | ||
} | ||
@@ -2094,3 +1642,3 @@ return { marks, nodes: {} }; | ||
parent: schemaSpecFacet, | ||
singleton: true | ||
singleton: !0 | ||
}); | ||
@@ -2106,8 +1654,5 @@ | ||
reducer: (inputs) => { | ||
const nodeViews = {}; | ||
for (const input of inputs) { | ||
if (!nodeViews[input.name]) { | ||
nodeViews[input.name] = input.constructor; | ||
} | ||
} | ||
let nodeViews = {}; | ||
for (let input of inputs) | ||
nodeViews[input.name] || (nodeViews[input.name] = input.constructor); | ||
return () => [ | ||
@@ -2131,22 +1676,14 @@ new ProseMirrorPlugin5({ | ||
reducer: (inputs) => { | ||
const nodeViews = {}; | ||
const options = {}; | ||
const factories = {}; | ||
for (const input of inputs) { | ||
const group = input.group; | ||
if (input.name == null) { | ||
factories[group] = input.factory; | ||
} else { | ||
options[group] || (options[group] = []); | ||
options[group].push({ | ||
name: input.name, | ||
args: input.args | ||
}); | ||
} | ||
let nodeViews = {}, options = {}, factories = {}; | ||
for (let input of inputs) { | ||
let group = input.group; | ||
input.name == null ? factories[group] = input.factory : (options[group] || (options[group] = []), options[group].push({ | ||
name: input.name, | ||
args: input.args | ||
})); | ||
} | ||
for (const [group, factory] of Object.entries(factories)) { | ||
const groupOptions = options[group] || []; | ||
for (const { name, args } of groupOptions) { | ||
for (let [group, factory] of Object.entries(factories)) { | ||
let groupOptions = options[group] || []; | ||
for (let { name, args } of groupOptions) | ||
nodeViews[name] = factory(args); | ||
} | ||
} | ||
@@ -2189,9 +1726,4 @@ return () => [ | ||
function cache(fn) { | ||
let result = void 0; | ||
return () => { | ||
if (result === void 0) { | ||
result = fn(); | ||
} | ||
return result; | ||
}; | ||
let result; | ||
return () => (result === void 0 && (result = fn()), result); | ||
} | ||
@@ -2204,3 +1736,3 @@ | ||
} catch (error) { | ||
return false; | ||
return !1; | ||
} | ||
@@ -2216,5 +1748,4 @@ }); | ||
for (let i = 0; i < match.edgeCount; i++) { | ||
const { type } = match.edge(i); | ||
if (type.isTextblock && !type.hasRequiredAttrs()) | ||
return type; | ||
let { type } = match.edge(i); | ||
if (type.isTextblock && !type.hasRequiredAttrs()) return type; | ||
} | ||
@@ -2227,4 +1758,3 @@ return null; | ||
function getId() { | ||
id = (id + 1) % Number.MAX_SAFE_INTEGER; | ||
return `id:${id}`; | ||
return id = (id + 1) % Number.MAX_SAFE_INTEGER, `id:${id}`; | ||
} | ||
@@ -2242,3 +1772,3 @@ | ||
function maybeRun(value, ...args) { | ||
return typeof value === "function" ? value(...args) : value; | ||
return typeof value == "function" ? value(...args) : value; | ||
} | ||
@@ -2251,8 +1781,3 @@ | ||
function withSkipCodeBlock(command) { | ||
return (state, dispatch, view) => { | ||
if (isInCodeBlock(state.selection)) { | ||
return false; | ||
} | ||
return command(state, dispatch, view); | ||
}; | ||
return (state, dispatch, view) => isInCodeBlock(state.selection) ? !1 : command(state, dispatch, view); | ||
} | ||
@@ -2259,0 +1784,0 @@ export { |
{ | ||
"name": "@prosekit/core", | ||
"type": "module", | ||
"version": "0.0.0-next-20240602042411", | ||
"version": "0.0.0-next-20240605080819", | ||
"private": false, | ||
@@ -38,10 +38,10 @@ "author": { | ||
"dependencies": { | ||
"@prosekit/pm": "0.0.0-next-20240602042411", | ||
"clsx": "^2.1.1", | ||
"orderedmap": "^2.1.1", | ||
"prosemirror-splittable": "^0.1.1", | ||
"type-fest": "^4.18.3" | ||
"type-fest": "^4.19.0", | ||
"@prosekit/pm": "^0.0.0-next-20240605080819" | ||
}, | ||
"devDependencies": { | ||
"tsup": "^8.0.2", | ||
"tsup": "^8.1.0", | ||
"typescript": "^5.4.5", | ||
@@ -48,0 +48,0 @@ "vitest": "^1.6.0", |
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
120042
3451
+ Added@prosekit/pm@0.0.0-next-20240901092634(transitive)
- Removed@prosekit/pm@0.0.0-next-20240602042411(transitive)
Updatedtype-fest@^4.19.0