@@ -15,6 +15,7 @@ import { type EditorView } from "prosemirror-view";
1515
1616import { findSuggestionMarkEnd } from "./findSuggestionMarkEnd.js" ;
1717import { suggestChangesKey } from "./plugin.js" ;
18+ import { getSuggestionMarks } from "./utils.js" ;
1819
1920/**
2021 * Given a node and a transform, add a set of steps to the
@@ -103,9 +104,8 @@ function applySuggestionsToTransform(
103104}
104105
105106function revertModifications ( node : Node , pos : number , tr : Transform ) {
106- const existingMods = node . marks . filter (
107- ( mark ) => mark . type === node . type . schema . marks [ "modification" ] ,
108- ) ;
107+ const { modification } = getSuggestionMarks ( node . type . schema ) ;
108+ const existingMods = node . marks . filter ( ( mark ) => mark . type === modification ) ;
109109 for ( const mod of existingMods ) {
110110 if (
111111 mod . attrs [ "type" ] === "attr" &&
@@ -146,12 +146,7 @@ function applyModificationsToTransform(
146146 dir : number ,
147147 suggestionId ?: number ,
148148) {
149- const { modification } = node . type . schema . marks ;
150- if ( ! modification ) {
151- throw new Error (
152- `Failed to apply modifications to node: schema does not contain modification mark. Did you forget to add it?` ,
153- ) ;
154- }
149+ const { modification } = getSuggestionMarks ( node . type . schema ) ;
155150
156151 const modificationIsInSet =
157152 suggestionId === undefined
@@ -194,17 +189,7 @@ function applyModificationsToTransform(
194189}
195190
196191function applySuggestionsToNode ( node : Node ) {
197- const { deletion, insertion } = node . type . schema . marks ;
198- if ( ! deletion ) {
199- throw new Error (
200- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
201- ) ;
202- }
203- if ( ! insertion ) {
204- throw new Error (
205- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
206- ) ;
207- }
192+ const { deletion, insertion } = getSuggestionMarks ( node . type . schema ) ;
208193
209194 if ( deletion . isInSet ( node . marks ) ) {
210195 return null ;
@@ -236,17 +221,7 @@ export function applySuggestions(
236221 state : EditorState ,
237222 dispatch ?: EditorView [ "dispatch" ] ,
238223) {
239- const { deletion, insertion } = state . schema . marks ;
240- if ( ! deletion ) {
241- throw new Error (
242- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
243- ) ;
244- }
245- if ( ! insertion ) {
246- throw new Error (
247- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
248- ) ;
249- }
224+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
250225
251226 const tr = state . tr ;
252227 applySuggestionsToTransform ( state . doc , tr , insertion , deletion ) ;
@@ -265,17 +240,7 @@ export function applySuggestions(
265240 */
266241export function applySuggestion ( suggestionId : number ) : Command {
267242 return ( state , dispatch ) => {
268- const { deletion, insertion } = state . schema . marks ;
269- if ( ! deletion ) {
270- throw new Error (
271- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
272- ) ;
273- }
274- if ( ! insertion ) {
275- throw new Error (
276- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
277- ) ;
278- }
243+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
279244
280245 const tr = state . tr ;
281246 applySuggestionsToTransform (
@@ -304,17 +269,7 @@ export function revertSuggestions(
304269 state : EditorState ,
305270 dispatch ?: EditorView [ "dispatch" ] ,
306271) {
307- const { deletion, insertion } = state . schema . marks ;
308- if ( ! deletion ) {
309- throw new Error (
310- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
311- ) ;
312- }
313- if ( ! insertion ) {
314- throw new Error (
315- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
316- ) ;
317- }
272+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
318273 const tr = state . tr ;
319274 applySuggestionsToTransform ( state . doc , tr , deletion , insertion ) ;
320275 applyModificationsToTransform ( tr . doc , tr , - 1 ) ;
@@ -332,17 +287,7 @@ export function revertSuggestions(
332287 */
333288export function revertSuggestion ( suggestionId : number ) : Command {
334289 return ( state , dispatch ) => {
335- const { deletion, insertion } = state . schema . marks ;
336- if ( ! deletion ) {
337- throw new Error (
338- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
339- ) ;
340- }
341- if ( ! insertion ) {
342- throw new Error (
343- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
344- ) ;
345- }
290+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
346291
347292 const tr = state . tr ;
348293 applySuggestionsToTransform (
@@ -365,22 +310,9 @@ export function revertSuggestion(suggestionId: number): Command {
365310 */
366311export function selectSuggestion ( suggestionId : number ) : Command {
367312 return ( state , dispatch ) => {
368- const { deletion, insertion, modification } = state . schema . marks ;
369- if ( ! deletion ) {
370- throw new Error (
371- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
372- ) ;
373- }
374- if ( ! insertion ) {
375- throw new Error (
376- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
377- ) ;
378- }
379- if ( ! modification ) {
380- throw new Error (
381- `Failed to apply tracked changes to node: schema does not contain modification mark. Did you forget to add it?` ,
382- ) ;
383- }
313+ const { deletion, insertion, modification } = getSuggestionMarks (
314+ state . schema ,
315+ ) ;
384316
385317 let changeStart = null as number | null ;
386318 let changeEnd = null as number | null ;
0 commit comments