prosemirror-paste-rules
Advanced tools
Comparing version 0.0.0-pr2169.2 to 0.0.0-pr2222.1
@@ -1,11 +0,175 @@ | ||
export { pasteRules } from './_tsup-dts-rollup'; | ||
export { isInCode } from './_tsup-dts-rollup'; | ||
export { MarkPasteRule } from './_tsup-dts-rollup'; | ||
export { NodePasteRule } from './_tsup-dts-rollup'; | ||
export { TextPasteRule } from './_tsup-dts-rollup'; | ||
export { FileHandlerProps } from './_tsup-dts-rollup'; | ||
export { FilePasteHandlerProps } from './_tsup-dts-rollup'; | ||
export { FileDropHandlerProps } from './_tsup-dts-rollup'; | ||
export { FilePasteRule } from './_tsup-dts-rollup'; | ||
export { PasteRule } from './_tsup-dts-rollup'; | ||
export { IsInCodeOptions } from './_tsup-dts-rollup'; | ||
import { MarkType, NodeType, Fragment, Node } from 'prosemirror-model'; | ||
import { Plugin, Selection } from 'prosemirror-state'; | ||
import { EditorView } from 'prosemirror-view'; | ||
import { ExtensionPriority } from '@remirror/core-constants'; | ||
/** | ||
* Create the paste plugin handler. | ||
*/ | ||
declare function pasteRules(pasteRules: PasteRule[]): Plugin; | ||
interface BasePasteRule { | ||
/** | ||
* The priority for the extension. Can be a number, or if you're using it with | ||
* `remirror` then use the `ExtensionPriority` enum. | ||
* | ||
* @defaultValue 10 | ||
*/ | ||
priority?: ExtensionPriority; | ||
} | ||
interface BaseRegexPasteRule extends BasePasteRule { | ||
/** | ||
* The regular expression to test against. | ||
*/ | ||
regexp: RegExp; | ||
/** | ||
* Only match at the start of the text block. | ||
*/ | ||
startOfTextBlock?: boolean; | ||
/** | ||
* Ignore the match when all characters in the capture group are whitespace. | ||
* | ||
* This helps stop situations from occurring where the a capture group matches | ||
* but you don't want an update if it's all whitespace. | ||
* | ||
* @defaultValue false | ||
*/ | ||
ignoreWhitespace?: boolean; | ||
/** | ||
* The names of nodes for which this paste rule can be ignored. This means | ||
* that if content is within any of the nodes provided the transformation will | ||
* be ignored. | ||
*/ | ||
ignoredNodes?: string[]; | ||
/** | ||
* The names of marks for which this paste rule can be ignored. This means | ||
* that if the matched content contains this mark it will be ignored. | ||
*/ | ||
ignoredMarks?: string[]; | ||
} | ||
interface BaseContentPasteRule extends BaseRegexPasteRule { | ||
/** | ||
* A helper function for setting the attributes for a transformation. | ||
* | ||
* The second parameter is `true` when the attributes are retrieved for a replacement. | ||
*/ | ||
getAttributes?: Record<string, unknown> | ((match: RegExpExecArray, isReplacement: boolean) => Record<string, unknown> | undefined); | ||
} | ||
/** | ||
* For adding marks to text when a paste rule is activated. | ||
*/ | ||
interface MarkPasteRule extends BaseContentPasteRule { | ||
/** | ||
* The type of rule. | ||
*/ | ||
type: 'mark'; | ||
/** | ||
* The prosemirror mark type instance. | ||
*/ | ||
markType: MarkType; | ||
/** | ||
* Set to `true` to replace the selection. When the regex matches for the | ||
* selected text. | ||
* | ||
* Can be a function which receives the text that will be replaced. | ||
*/ | ||
replaceSelection?: boolean | ((replacedText: string) => boolean); | ||
/** | ||
* A function that transforms the match into the desired text value. | ||
* | ||
* Return an empty string to delete all content. | ||
* | ||
* Return `false` to invalidate the match. | ||
*/ | ||
transformMatch?: (match: RegExpExecArray) => string | null | undefined | false; | ||
} | ||
interface NodePasteRule extends BaseContentPasteRule { | ||
/** | ||
* The type of rule. | ||
*/ | ||
type: 'node'; | ||
/** | ||
* The node type to create. | ||
*/ | ||
nodeType: NodeType; | ||
/** | ||
* A function that transforms the match into the content to use when creating | ||
* a node. | ||
* | ||
* Pass `() => {}` to remove the matched text. | ||
* | ||
* If this function is undefined, then the text node that is cut from the match | ||
* will be used as the content. | ||
*/ | ||
getContent?: (match: RegExpExecArray) => Fragment | Node | Node[] | undefined | void; | ||
} | ||
/** | ||
* For handling simpler text updates. | ||
*/ | ||
interface TextPasteRule extends BaseRegexPasteRule { | ||
/** | ||
* The type of rule. | ||
*/ | ||
type: 'text'; | ||
/** | ||
* A function that transforms the match into the desired text value. | ||
* | ||
* Return an empty string to delete all content. | ||
* | ||
* Return `false` to invalidate the match. | ||
*/ | ||
transformMatch?: (match: RegExpExecArray) => string | null | undefined | false; | ||
} | ||
type FileHandlerProps = FilePasteHandlerProps | FileDropHandlerProps; | ||
interface FilePasteHandlerProps { | ||
type: 'paste'; | ||
/** All the matching files */ | ||
files: File[]; | ||
event: ClipboardEvent; | ||
view: EditorView; | ||
selection: Selection; | ||
} | ||
interface FileDropHandlerProps { | ||
type: 'drop'; | ||
/** All the matching files */ | ||
files: File[]; | ||
event: DragEvent; | ||
view: EditorView; | ||
pos: number; | ||
} | ||
/** | ||
* For handling pasting files and also file drops. | ||
*/ | ||
interface FilePasteRule extends BasePasteRule { | ||
type: 'file'; | ||
/** | ||
* A regex test for the file type. | ||
*/ | ||
regexp?: RegExp; | ||
/** | ||
* The names of nodes for which this paste rule can be ignored. This means | ||
* that if content is within any of the nodes provided the transformation will | ||
* be ignored. | ||
*/ | ||
ignoredNodes?: string[]; | ||
/** | ||
* Return `false` to defer to the next image handler. | ||
* | ||
* The file | ||
*/ | ||
fileHandler: (props: FileHandlerProps) => boolean; | ||
} | ||
type PasteRule = FilePasteRule | TextPasteRule | NodePasteRule | MarkPasteRule; | ||
interface IsInCodeOptions { | ||
/** | ||
* When this is set to true ensure the selection is fully contained within a code block. This means that selections that span multiple characters must all be within a code region for it to return true. | ||
* | ||
* @defaultValue true | ||
*/ | ||
contained?: boolean; | ||
} | ||
/** | ||
* Check whether the current selection is completely contained within a code block or mark. | ||
*/ | ||
declare function isInCode(selection: Selection, { contained }?: IsInCodeOptions): boolean; | ||
export { type FileDropHandlerProps, type FileHandlerProps, type FilePasteHandlerProps, type FilePasteRule, type IsInCodeOptions, type MarkPasteRule, type NodePasteRule, type PasteRule, type TextPasteRule, isInCode, pasteRules }; |
@@ -1,3 +0,6 @@ | ||
// src/paste-rules-plugin.ts | ||
import { Fragment, Slice } from "prosemirror-model"; | ||
// packages/prosemirror-paste-rules/src/paste-rules-plugin.ts | ||
import { | ||
Fragment, | ||
Slice | ||
} from "prosemirror-model"; | ||
import { Plugin, PluginKey } from "prosemirror-state"; | ||
@@ -7,3 +10,6 @@ import { ExtensionPriority } from "@remirror/core-constants"; | ||
function pasteRules(pasteRules2) { | ||
const sortedPasteRules = sort(pasteRules2, (a, z) => (z.priority ?? ExtensionPriority.Low) - (a.priority ?? ExtensionPriority.Low)); | ||
const sortedPasteRules = sort( | ||
pasteRules2, | ||
(a, z) => (z.priority ?? ExtensionPriority.Low) - (a.priority ?? ExtensionPriority.Low) | ||
); | ||
const regexPasteRules2 = []; | ||
@@ -21,3 +27,3 @@ const filePasteRules = []; | ||
key: pastePluginKey, | ||
view: editorView => { | ||
view: (editorView) => { | ||
view = editorView; | ||
@@ -28,12 +34,12 @@ return {}; | ||
// The regex based paste rules are passed into this function to take care of. | ||
transformPasted: slice => { | ||
transformPasted: (slice) => { | ||
const $pos = view.state.selection.$from; | ||
const nodeName = $pos.node().type.name; | ||
const markNames = new Set($pos.marks().map(mark => mark.type.name)); | ||
const markNames = new Set($pos.marks().map((mark) => mark.type.name)); | ||
for (const rule of regexPasteRules2) { | ||
if ( | ||
// The parent node is ignored. | ||
rule.ignoredNodes?.includes(nodeName) || | ||
// The current position contains ignored marks. | ||
rule.ignoredMarks?.some(ignored => markNames.has(ignored))) { | ||
// The parent node is ignored. | ||
rule.ignoredNodes?.includes(nodeName) || // The current position contains ignored marks. | ||
rule.ignoredMarks?.some((ignored) => markNames.has(ignored)) | ||
) { | ||
continue; | ||
@@ -45,6 +51,3 @@ } | ||
if (canBeReplaced && match && rule.type === "mark" && rule.replaceSelection) { | ||
const { | ||
from, | ||
to | ||
} = view.state.selection; | ||
const { from, to } = view.state.selection; | ||
const textSlice = view.state.doc.slice(from, to); | ||
@@ -54,9 +57,6 @@ const textContent2 = textSlice.content.textBetween(0, textSlice.content.size); | ||
const newTextNodes = []; | ||
const { | ||
getAttributes, | ||
markType | ||
} = rule; | ||
const { getAttributes, markType } = rule; | ||
const attributes = isFunction(getAttributes) ? getAttributes(match, true) : getAttributes; | ||
const mark = markType.create(attributes); | ||
textSlice.content.forEach(textNode => { | ||
textSlice.content.forEach((textNode) => { | ||
if (textNode.isText) { | ||
@@ -70,6 +70,7 @@ const marks = mark.addToSet(textNode.marks); | ||
} | ||
const { | ||
nodes: transformedNodes, | ||
transformed | ||
} = regexPasteRuleHandler(slice.content, rule, view.state.schema); | ||
const { nodes: transformedNodes, transformed } = regexPasteRuleHandler( | ||
slice.content, | ||
rule, | ||
view.state.schema | ||
); | ||
if (transformed) { | ||
@@ -88,30 +89,17 @@ slice = rule.type === "node" && rule.nodeType.isBlock ? new Slice(Fragment.fromArray(transformedNodes), 0, 0) : new Slice(Fragment.fromArray(transformedNodes), slice.openStart, slice.openEnd); | ||
} | ||
const { | ||
clipboardData | ||
} = event; | ||
const { clipboardData } = event; | ||
if (!clipboardData) { | ||
return false; | ||
} | ||
const allFiles = [...clipboardData.items].map(data => data.getAsFile()).filter(file => !!file); | ||
const allFiles = [...clipboardData.items].map((data) => data.getAsFile()).filter((file) => !!file); | ||
if (allFiles.length === 0) { | ||
return false; | ||
} | ||
const { | ||
selection | ||
} = view2.state; | ||
for (const { | ||
fileHandler, | ||
regexp | ||
} of filePasteRules) { | ||
const files = regexp ? allFiles.filter(file => regexp.test(file.type)) : allFiles; | ||
const { selection } = view2.state; | ||
for (const { fileHandler, regexp } of filePasteRules) { | ||
const files = regexp ? allFiles.filter((file) => regexp.test(file.type)) : allFiles; | ||
if (files.length === 0) { | ||
continue; | ||
} | ||
if (fileHandler({ | ||
event, | ||
files, | ||
selection, | ||
view: view2, | ||
type: "paste" | ||
})) { | ||
if (fileHandler({ event, files, selection, view: view2, type: "paste" })) { | ||
event.preventDefault(); | ||
@@ -129,7 +117,3 @@ return true; | ||
} | ||
const { | ||
dataTransfer, | ||
clientX, | ||
clientY | ||
} = event; | ||
const { dataTransfer, clientX, clientY } = event; | ||
if (!dataTransfer) { | ||
@@ -142,21 +126,9 @@ return false; | ||
} | ||
const pos = view2.posAtCoords({ | ||
left: clientX, | ||
top: clientY | ||
})?.pos ?? view2.state.selection.anchor; | ||
for (const { | ||
fileHandler, | ||
regexp | ||
} of filePasteRules) { | ||
const files = regexp ? allFiles.filter(file => regexp.test(file.type)) : allFiles; | ||
const pos = view2.posAtCoords({ left: clientX, top: clientY })?.pos ?? view2.state.selection.anchor; | ||
for (const { fileHandler, regexp } of filePasteRules) { | ||
const files = regexp ? allFiles.filter((file) => regexp.test(file.type)) : allFiles; | ||
if (files.length === 0) { | ||
continue; | ||
} | ||
if (fileHandler({ | ||
event, | ||
files, | ||
pos, | ||
view: view2, | ||
type: "drop" | ||
})) { | ||
if (fileHandler({ event, files, pos, view: view2, type: "drop" })) { | ||
event.preventDefault(); | ||
@@ -175,15 +147,6 @@ return true; | ||
return function handler(props) { | ||
const { | ||
fragment, | ||
rule, | ||
nodes | ||
} = props; | ||
const { | ||
regexp, | ||
ignoreWhitespace, | ||
ignoredMarks, | ||
ignoredNodes | ||
} = rule; | ||
const { fragment, rule, nodes } = props; | ||
const { regexp, ignoreWhitespace, ignoredMarks, ignoredNodes } = rule; | ||
let transformed = false; | ||
fragment.forEach(child => { | ||
fragment.forEach((child) => { | ||
if (ignoredNodes?.includes(child.type.name) || isCodeNode(child)) { | ||
@@ -194,7 +157,3 @@ nodes.push(child); | ||
if (!child.isText) { | ||
const childResult = handler({ | ||
fragment: child.content, | ||
rule, | ||
nodes: [] | ||
}); | ||
const childResult = handler({ fragment: child.content, rule, nodes: [] }); | ||
transformed ||= childResult.transformed; | ||
@@ -209,3 +168,3 @@ const content = Fragment.fromArray(childResult.nodes); | ||
} | ||
if (child.marks.some(mark => isCodeMark(mark) || ignoredMarks?.includes(mark.type.name))) { | ||
if (child.marks.some((mark) => isCodeMark(mark) || ignoredMarks?.includes(mark.type.name))) { | ||
nodes.push(child); | ||
@@ -220,5 +179,6 @@ return; | ||
if ( | ||
// This helps prevent matches which are only whitespace from triggering | ||
// an update. | ||
ignoreWhitespace && capturedValue?.trim() === "" || !fullValue) { | ||
// This helps prevent matches which are only whitespace from triggering | ||
// an update. | ||
ignoreWhitespace && capturedValue?.trim() === "" || !fullValue | ||
) { | ||
return; | ||
@@ -241,9 +201,3 @@ } | ||
} | ||
transformer({ | ||
nodes, | ||
rule, | ||
textNode, | ||
match, | ||
schema | ||
}); | ||
transformer({ nodes, rule, textNode, match, schema }); | ||
transformed = true; | ||
@@ -256,21 +210,8 @@ pos = end; | ||
}); | ||
return { | ||
nodes, | ||
transformed | ||
}; | ||
return { nodes, transformed }; | ||
}; | ||
} | ||
function markRuleTransformer(props) { | ||
const { | ||
nodes, | ||
rule, | ||
textNode, | ||
match, | ||
schema | ||
} = props; | ||
const { | ||
transformMatch, | ||
getAttributes, | ||
markType | ||
} = rule; | ||
const { nodes, rule, textNode, match, schema } = props; | ||
const { transformMatch, getAttributes, markType } = rule; | ||
const attributes = isFunction(getAttributes) ? getAttributes(match, false) : getAttributes; | ||
@@ -291,12 +232,4 @@ const text = textNode.text ?? ""; | ||
function textRuleTransformer(props) { | ||
const { | ||
nodes, | ||
rule, | ||
textNode, | ||
match, | ||
schema | ||
} = props; | ||
const { | ||
transformMatch | ||
} = rule; | ||
const { nodes, rule, textNode, match, schema } = props; | ||
const { transformMatch } = rule; | ||
const transformedCapturedValue = transformMatch?.(match); | ||
@@ -310,13 +243,4 @@ if (transformedCapturedValue === "" || transformedCapturedValue === false) { | ||
function nodeRuleTransformer(props) { | ||
const { | ||
nodes, | ||
rule, | ||
textNode, | ||
match | ||
} = props; | ||
const { | ||
getAttributes, | ||
nodeType, | ||
getContent | ||
} = rule; | ||
const { nodes, rule, textNode, match } = props; | ||
const { getAttributes, nodeType, getContent } = rule; | ||
const attributes = isFunction(getAttributes) ? getAttributes(match, false) : getAttributes; | ||
@@ -330,19 +254,7 @@ const content = (getContent ? getContent(match) : textNode) || void 0; | ||
case "mark": | ||
return createPasteRuleHandler(markRuleTransformer, schema)({ | ||
fragment, | ||
nodes, | ||
rule | ||
}); | ||
return createPasteRuleHandler(markRuleTransformer, schema)({ fragment, nodes, rule }); | ||
case "node": | ||
return createPasteRuleHandler(nodeRuleTransformer, schema)({ | ||
fragment, | ||
nodes, | ||
rule | ||
}); | ||
return createPasteRuleHandler(nodeRuleTransformer, schema)({ fragment, nodes, rule }); | ||
default: | ||
return createPasteRuleHandler(textRuleTransformer, schema)({ | ||
fragment, | ||
nodes, | ||
rule | ||
}); | ||
return createPasteRuleHandler(textRuleTransformer, schema)({ fragment, nodes, rule }); | ||
} | ||
@@ -354,5 +266,3 @@ } | ||
} | ||
function isInCode(selection, { | ||
contained = true | ||
} = {}) { | ||
function isInCode(selection, { contained = true } = {}) { | ||
if (selection.empty) { | ||
@@ -386,5 +296,3 @@ return resolvedPosInCode(selection.$head); | ||
function getDataTransferFiles(event) { | ||
const { | ||
dataTransfer | ||
} = event; | ||
const { dataTransfer } = event; | ||
if (!dataTransfer) { | ||
@@ -397,3 +305,3 @@ return []; | ||
if (dataTransfer.items?.length) { | ||
return [...dataTransfer.items].map(item => item.getAsFile()).filter(item => !!item); | ||
return [...dataTransfer.items].map((item) => item.getAsFile()).filter((item) => !!item); | ||
} | ||
@@ -406,2 +314,5 @@ return []; | ||
} | ||
export { isInCode, pasteRules }; | ||
export { | ||
isInCode, | ||
pasteRules | ||
}; |
{ | ||
"name": "prosemirror-paste-rules", | ||
"version": "0.0.0-pr2169.2", | ||
"version": "0.0.0-pr2222.1", | ||
"description": "Better handling of pasted content in your prosemirror editor.", | ||
@@ -34,11 +34,10 @@ "homepage": "https://github.com/remirror/remirror/tree/HEAD/packages/prosemirror-paste-rules", | ||
"@babel/runtime": "^7.22.3", | ||
"@remirror/core-constants": "0.0.0-pr2169.2", | ||
"@remirror/core-helpers": "0.0.0-pr2169.2", | ||
"@remirror/core-constants": "0.0.0-pr2222.1", | ||
"@remirror/core-helpers": "0.0.0-pr2222.1", | ||
"escape-string-regexp": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@remirror/cli": "0.0.0-pr2169.2", | ||
"prosemirror-model": "^1.19.3", | ||
"prosemirror-state": "^1.4.3", | ||
"prosemirror-view": "^1.32.3" | ||
"prosemirror-view": "^1.31.7" | ||
}, | ||
@@ -48,3 +47,3 @@ "peerDependencies": { | ||
"prosemirror-state": "^1.4.2", | ||
"prosemirror-view": "^1.32.3" | ||
"prosemirror-view": "^1.31.2" | ||
}, | ||
@@ -57,6 +56,3 @@ "peerDependenciesMeta": {}, | ||
"sizeLimit": "10 KB" | ||
}, | ||
"scripts": { | ||
"build": "remirror-cli build" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
3
93718
11
1283
+ Added@remirror/core-constants@0.0.0-pr2222.1(transitive)
+ Added@remirror/core-helpers@0.0.0-pr2222.1(transitive)
+ Added@remirror/types@0.0.0-pr2222.1(transitive)
+ Addedprosemirror-view@1.37.0(transitive)
- Removed@remirror/core-constants@0.0.0-pr2169.2(transitive)
- Removed@remirror/core-helpers@0.0.0-pr2169.2(transitive)
- Removed@remirror/types@0.0.0-pr2169.2(transitive)
- Removedprosemirror-view@1.36.0(transitive)