@prosekit/core
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -130,2 +130,12 @@ import { AllSelection } from '@prosekit/pm/state'; | ||
export declare function baseToggleMark(markType: MarkType, attrs?: Attrs | null, options?: { | ||
removeWhenPresent: boolean; | ||
}): Command; | ||
export declare function cache<T>(fn: () => T): () => T; | ||
declare const canUseRegexLookbehind: () => boolean; | ||
export { canUseRegexLookbehind } | ||
export { canUseRegexLookbehind as canUseRegexLookbehind_alias_1 } | ||
declare type ClickHandler = (view: EditorView, pos: number, event: MouseEvent) => boolean | void; | ||
@@ -1091,6 +1101,18 @@ export { ClickHandler } | ||
/** | ||
* Returns true if the given mark is missing in some part of the range. | ||
* Returns false if the entire range has the given mark. | ||
* | ||
* @internal | ||
*/ | ||
export declare function isMarkActive(state: EditorState, type: string | MarkType, attrs?: Attrs | null): boolean; | ||
declare function isMarkAbsent(node: ProseMirrorNode, from: number, to: number, markType: MarkType, attrs?: Attrs | null): boolean; | ||
export { isMarkAbsent } | ||
export { isMarkAbsent as isMarkAbsent_alias_1 } | ||
/** | ||
* @internal | ||
*/ | ||
declare function isMarkActive(state: EditorState, type: string | MarkType, attrs?: Attrs | null): boolean; | ||
export { isMarkActive } | ||
export { isMarkActive as isMarkActive_alias_1 } | ||
export declare function isNodeActive(state: EditorState, type: string | NodeType, attrs?: Attrs | null): boolean; | ||
@@ -1239,2 +1261,9 @@ | ||
/** | ||
* @internal | ||
*/ | ||
declare function maybeRun<T, R = T extends (...args: any[]) => void ? ReturnType<T> : T>(value: T, ...args: T extends (...args: any[]) => void ? Parameters<T> : never): R; | ||
export { maybeRun } | ||
export { maybeRun as maybeRun_alias_1 } | ||
/** | ||
* A function that is called when the editor view is mounted. | ||
@@ -1241,0 +1270,0 @@ * |
@@ -96,2 +96,3 @@ export { addMark } from './_tsup-dts-rollup'; | ||
export { SimplifyUnion } from './_tsup-dts-rollup'; | ||
export { canUseRegexLookbehind } from './_tsup-dts-rollup'; | ||
export { clsx } from './_tsup-dts-rollup'; | ||
@@ -103,2 +104,5 @@ export { defaultBlockAt } from './_tsup-dts-rollup'; | ||
export { isInCodeBlock } from './_tsup-dts-rollup'; | ||
export { isMarkAbsent } from './_tsup-dts-rollup'; | ||
export { isMarkActive } from './_tsup-dts-rollup'; | ||
export { maybeRun } from './_tsup-dts-rollup'; | ||
export { elementFromJSON } from './_tsup-dts-rollup'; | ||
@@ -105,0 +109,0 @@ export { elementFromNode } from './_tsup-dts-rollup'; |
@@ -255,5 +255,73 @@ // src/commands/add-mark.ts | ||
// src/commands/toggle-mark.ts | ||
import { toggleMark as baseToggleMark } from "@prosekit/pm/commands"; | ||
import "@prosekit/pm/model"; | ||
import "@prosekit/pm/state"; | ||
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; | ||
can = node.inlineContent && node.type.allowsMarkType(type); | ||
}); | ||
if (can) | ||
return true; | ||
} | ||
return false; | ||
} | ||
function baseToggleMark(markType, attrs = null, options) { | ||
const removeWhenPresent = (options && options.removeWhenPresent) !== false; | ||
return function(state, dispatch) { | ||
const { 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) { | ||
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)); | ||
} | ||
} | ||
dispatch(tr.scrollIntoView()); | ||
} | ||
} | ||
return true; | ||
}; | ||
} | ||
function toggleMark({ | ||
@@ -264,7 +332,5 @@ type, | ||
return (state, dispatch, view) => { | ||
return baseToggleMark(getMarkType(state.schema, type), attrs)( | ||
state, | ||
dispatch, | ||
view | ||
); | ||
return baseToggleMark(getMarkType(state.schema, type), attrs, { | ||
removeWhenPresent: false | ||
})(state, dispatch, view); | ||
}; | ||
@@ -797,2 +863,14 @@ } | ||
// src/utils/is-mark-absent.ts | ||
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; | ||
missing = !mark.isInSet(node2.marks) && !!parent && parent.type.allowsMarkType(markType); | ||
}); | ||
return missing; | ||
} | ||
// src/utils/is-mark-active.ts | ||
@@ -806,4 +884,3 @@ function isMarkActive(state, type, attrs) { | ||
} else { | ||
const markOrType = attrs ? markType.create(attrs) : markType; | ||
return state.doc.rangeHasMark(from, to, markOrType); | ||
return !isMarkAbsent(state.doc, from, to, markType, attrs); | ||
} | ||
@@ -813,10 +890,10 @@ } | ||
// src/utils/type-assertion.ts | ||
import { Mark, ProseMirrorNode } from "@prosekit/pm/model"; | ||
import { Mark, ProseMirrorNode as ProseMirrorNode2 } from "@prosekit/pm/model"; | ||
import { | ||
AllSelection, | ||
NodeSelection, | ||
TextSelection as TextSelection4 | ||
TextSelection as TextSelection5 | ||
} from "@prosekit/pm/state"; | ||
function isProseMirrorNode(node) { | ||
return node instanceof ProseMirrorNode; | ||
return node instanceof ProseMirrorNode2; | ||
} | ||
@@ -827,3 +904,3 @@ function isMark(mark) { | ||
function isTextSelection(sel) { | ||
return sel instanceof TextSelection4; | ||
return sel instanceof TextSelection5; | ||
} | ||
@@ -1940,2 +2017,22 @@ function isNodeSelection(sel) { | ||
// src/utils/cache.ts | ||
function cache(fn) { | ||
let result = void 0; | ||
return () => { | ||
if (result === void 0) { | ||
result = fn(); | ||
} | ||
return result; | ||
}; | ||
} | ||
// src/utils/can-use-regex-lookbehind.ts | ||
var canUseRegexLookbehind = cache(() => { | ||
try { | ||
return "ab".replace(new RegExp("(?<=a)b", "g"), "c") === "ac"; | ||
} catch (error) { | ||
return false; | ||
} | ||
}); | ||
// src/utils/clsx.ts | ||
@@ -1970,2 +2067,7 @@ import clsxLite from "clsx/lite"; | ||
// src/utils/maybe-run.ts | ||
function maybeRun(value, ...args) { | ||
return typeof value === "function" ? value(...args) : value; | ||
} | ||
// src/utils/unicode.ts | ||
@@ -1992,2 +2094,3 @@ var OBJECT_REPLACEMENT_CHARACTER = "\uFFFC"; | ||
addMark, | ||
canUseRegexLookbehind, | ||
clsx, | ||
@@ -2041,2 +2144,4 @@ createEditor, | ||
isMark, | ||
isMarkAbsent, | ||
isMarkActive, | ||
isNodeSelection, | ||
@@ -2049,2 +2154,3 @@ isProseMirrorNode, | ||
keymapFacet, | ||
maybeRun, | ||
nodeFromElement, | ||
@@ -2051,0 +2157,0 @@ nodeFromHTML, |
{ | ||
"name": "@prosekit/core", | ||
"type": "module", | ||
"version": "0.4.1", | ||
"version": "0.4.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
121195
3747