@@ -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
@@ -86,9 +87,8 @@ function applySuggestionsToTransform(
8687}
8788
8889function revertModifications ( node : Node , pos : number , tr : Transform ) {
89- const existingMods = node . marks . filter (
90- ( mark ) => mark . type === node . type . schema . marks [ "modification" ] ,
91- ) ;
90+ const { modification } = getSuggestionMarks ( node . type . schema ) ;
91+ const existingMods = node . marks . filter ( ( mark ) => mark . type === modification ) ;
9292 for ( const mod of existingMods ) {
9393 if (
9494 mod . attrs [ "type" ] === "attr" &&
@@ -129,12 +129,7 @@ function applyModificationsToTransform(
129129 dir : number ,
130130 suggestionId ?: number ,
131131) {
132- const { modification } = node . type . schema . marks ;
133- if ( ! modification ) {
134- throw new Error (
135- `Failed to apply modifications to node: schema does not contain modification mark. Did you forget to add it?` ,
136- ) ;
137- }
132+ const { modification } = getSuggestionMarks ( node . type . schema ) ;
138133
139134 const modificationIsInSet =
140135 suggestionId === undefined
@@ -177,17 +172,7 @@ function applyModificationsToTransform(
177172}
178173
179174function applySuggestionsToNode ( node : Node ) {
180- const { deletion, insertion } = node . type . schema . marks ;
181- if ( ! deletion ) {
182- throw new Error (
183- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
184- ) ;
185- }
186- if ( ! insertion ) {
187- throw new Error (
188- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
189- ) ;
190- }
175+ const { deletion, insertion } = getSuggestionMarks ( node . type . schema ) ;
191176
192177 if ( deletion . isInSet ( node . marks ) ) {
193178 return null ;
@@ -219,17 +204,7 @@ export function applySuggestions(
219204 state : EditorState ,
220205 dispatch ?: EditorView [ "dispatch" ] ,
221206) {
222- const { deletion, insertion } = state . schema . marks ;
223- if ( ! deletion ) {
224- throw new Error (
225- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
226- ) ;
227- }
228- if ( ! insertion ) {
229- throw new Error (
230- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
231- ) ;
232- }
207+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
233208
234209 const tr = state . tr ;
235210 applySuggestionsToTransform ( state . doc , tr , insertion , deletion ) ;
@@ -248,17 +223,7 @@ export function applySuggestions(
248223 */
249224export function applySuggestion ( suggestionId : number ) : Command {
250225 return ( state , dispatch ) => {
251- const { deletion, insertion } = state . schema . marks ;
252- if ( ! deletion ) {
253- throw new Error (
254- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
255- ) ;
256- }
257- if ( ! insertion ) {
258- throw new Error (
259- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
260- ) ;
261- }
226+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
262227
263228 const tr = state . tr ;
264229 applySuggestionsToTransform (
@@ -287,17 +252,7 @@ export function revertSuggestions(
287252 state : EditorState ,
288253 dispatch ?: EditorView [ "dispatch" ] ,
289254) {
290- const { deletion, insertion } = state . schema . marks ;
291- if ( ! deletion ) {
292- throw new Error (
293- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
294- ) ;
295- }
296- if ( ! insertion ) {
297- throw new Error (
298- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
299- ) ;
300- }
255+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
301256 const tr = state . tr ;
302257 applySuggestionsToTransform ( state . doc , tr , deletion , insertion ) ;
303258 applyModificationsToTransform ( tr . doc , tr , - 1 ) ;
@@ -315,17 +270,7 @@ export function revertSuggestions(
315270 */
316271export function revertSuggestion ( suggestionId : number ) : Command {
317272 return ( state , dispatch ) => {
318- const { deletion, insertion } = state . schema . marks ;
319- if ( ! deletion ) {
320- throw new Error (
321- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
322- ) ;
323- }
324- if ( ! insertion ) {
325- throw new Error (
326- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
327- ) ;
328- }
273+ const { deletion, insertion } = getSuggestionMarks ( state . schema ) ;
329274
330275 const tr = state . tr ;
331276 applySuggestionsToTransform (
@@ -348,22 +293,9 @@ export function revertSuggestion(suggestionId: number): Command {
348293 */
349294export function selectSuggestion ( suggestionId : number ) : Command {
350295 return ( state , dispatch ) => {
351- const { deletion, insertion, modification } = state . schema . marks ;
352- if ( ! deletion ) {
353- throw new Error (
354- `Failed to apply tracked changes to node: schema does not contain deletion mark. Did you forget to add it?` ,
355- ) ;
356- }
357- if ( ! insertion ) {
358- throw new Error (
359- `Failed to apply tracked changes to node: schema does not contain insertion mark. Did you forget to add it?` ,
360- ) ;
361- }
362- if ( ! modification ) {
363- throw new Error (
364- `Failed to apply tracked changes to node: schema does not contain modification mark. Did you forget to add it?` ,
365- ) ;
366- }
296+ const { deletion, insertion, modification } = getSuggestionMarks (
297+ state . schema ,
298+ ) ;
367299
368300 let changeStart = null as number | null ;
369301 let changeEnd = null as number | null ;
0 commit comments