@portabletext/editor 2.9.1 → 2.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import {Editor} from 'slate'
1
2
  import type {
2
3
  AbstractBehaviorEventType,
3
4
  SyntheticBehaviorEvent,
@@ -79,133 +80,135 @@ export function performOperation({
79
80
  context: BehaviorOperationImplementationContext
80
81
  operation: BehaviorOperation
81
82
  }) {
82
- try {
83
- switch (operation.type) {
84
- case 'annotation.add': {
85
- behaviorOperationImplementations['annotation.add']({
86
- context,
87
- operation: operation,
88
- })
89
- break
90
- }
91
- case 'annotation.remove': {
92
- behaviorOperationImplementations['annotation.remove']({
93
- context,
94
- operation: operation,
95
- })
96
- break
97
- }
98
- case 'block.set': {
99
- behaviorOperationImplementations['block.set']({
100
- context,
101
- operation: operation,
102
- })
103
- break
104
- }
105
- case 'block.unset': {
106
- behaviorOperationImplementations['block.unset']({
107
- context,
108
- operation: operation,
109
- })
110
- break
111
- }
112
- case 'child.set': {
113
- behaviorOperationImplementations['child.set']({
114
- context,
115
- operation: operation,
116
- })
117
- break
118
- }
119
- case 'child.unset': {
120
- behaviorOperationImplementations['child.unset']({
121
- context,
122
- operation: operation,
123
- })
124
- break
125
- }
126
- case 'decorator.add': {
127
- behaviorOperationImplementations['decorator.add']({
128
- context,
129
- operation: operation,
130
- })
131
- break
132
- }
133
- case 'decorator.remove': {
134
- behaviorOperationImplementations['decorator.remove']({
135
- context,
136
- operation: operation,
137
- })
138
- break
139
- }
140
- case 'delete': {
141
- behaviorOperationImplementations.delete({
142
- context,
143
- operation: operation,
144
- })
145
- break
146
- }
147
- case 'history.redo': {
148
- behaviorOperationImplementations['history.redo']({
149
- context,
150
- operation: operation,
151
- })
152
- break
153
- }
154
- case 'history.undo': {
155
- behaviorOperationImplementations['history.undo']({
156
- context,
157
- operation: operation,
158
- })
159
- break
160
- }
161
- case 'insert.block': {
162
- behaviorOperationImplementations['insert.block']({
163
- context,
164
- operation: operation,
165
- })
166
- break
167
- }
168
- case 'insert.text': {
169
- behaviorOperationImplementations['insert.text']({
170
- context,
171
- operation: operation,
172
- })
173
- break
174
- }
175
- case 'move.backward': {
176
- behaviorOperationImplementations['move.backward']({
177
- context,
178
- operation: operation,
179
- })
180
- break
181
- }
182
- case 'move.block': {
183
- behaviorOperationImplementations['move.block']({
184
- context,
185
- operation: operation,
186
- })
187
- break
188
- }
189
- case 'move.forward': {
190
- behaviorOperationImplementations['move.forward']({
191
- context,
192
- operation: operation,
193
- })
194
- break
195
- }
196
- default: {
197
- behaviorOperationImplementations.select({
198
- context,
199
- operation: operation,
200
- })
201
- break
202
- }
83
+ Editor.withoutNormalizing(operation.editor, () => {
84
+ try {
85
+ switch (operation.type) {
86
+ case 'annotation.add': {
87
+ behaviorOperationImplementations['annotation.add']({
88
+ context,
89
+ operation: operation,
90
+ })
91
+ break
92
+ }
93
+ case 'annotation.remove': {
94
+ behaviorOperationImplementations['annotation.remove']({
95
+ context,
96
+ operation: operation,
97
+ })
98
+ break
99
+ }
100
+ case 'block.set': {
101
+ behaviorOperationImplementations['block.set']({
102
+ context,
103
+ operation: operation,
104
+ })
105
+ break
106
+ }
107
+ case 'block.unset': {
108
+ behaviorOperationImplementations['block.unset']({
109
+ context,
110
+ operation: operation,
111
+ })
112
+ break
113
+ }
114
+ case 'child.set': {
115
+ behaviorOperationImplementations['child.set']({
116
+ context,
117
+ operation: operation,
118
+ })
119
+ break
120
+ }
121
+ case 'child.unset': {
122
+ behaviorOperationImplementations['child.unset']({
123
+ context,
124
+ operation: operation,
125
+ })
126
+ break
127
+ }
128
+ case 'decorator.add': {
129
+ behaviorOperationImplementations['decorator.add']({
130
+ context,
131
+ operation: operation,
132
+ })
133
+ break
134
+ }
135
+ case 'decorator.remove': {
136
+ behaviorOperationImplementations['decorator.remove']({
137
+ context,
138
+ operation: operation,
139
+ })
140
+ break
141
+ }
142
+ case 'delete': {
143
+ behaviorOperationImplementations.delete({
144
+ context,
145
+ operation: operation,
146
+ })
147
+ break
148
+ }
149
+ case 'history.redo': {
150
+ behaviorOperationImplementations['history.redo']({
151
+ context,
152
+ operation: operation,
153
+ })
154
+ break
155
+ }
156
+ case 'history.undo': {
157
+ behaviorOperationImplementations['history.undo']({
158
+ context,
159
+ operation: operation,
160
+ })
161
+ break
162
+ }
163
+ case 'insert.block': {
164
+ behaviorOperationImplementations['insert.block']({
165
+ context,
166
+ operation: operation,
167
+ })
168
+ break
169
+ }
170
+ case 'insert.text': {
171
+ behaviorOperationImplementations['insert.text']({
172
+ context,
173
+ operation: operation,
174
+ })
175
+ break
176
+ }
177
+ case 'move.backward': {
178
+ behaviorOperationImplementations['move.backward']({
179
+ context,
180
+ operation: operation,
181
+ })
182
+ break
183
+ }
184
+ case 'move.block': {
185
+ behaviorOperationImplementations['move.block']({
186
+ context,
187
+ operation: operation,
188
+ })
189
+ break
190
+ }
191
+ case 'move.forward': {
192
+ behaviorOperationImplementations['move.forward']({
193
+ context,
194
+ operation: operation,
195
+ })
196
+ break
197
+ }
198
+ default: {
199
+ behaviorOperationImplementations.select({
200
+ context,
201
+ operation: operation,
202
+ })
203
+ break
204
+ }
205
+ }
206
+ } catch (error) {
207
+ console.error(
208
+ new Error(
209
+ `Executing "${operation.type}" failed due to: ${error.message}`,
210
+ ),
211
+ )
203
212
  }
204
- } catch (error) {
205
- console.error(
206
- new Error(
207
- `Executing "${operation.type}" failed due to: ${error.message}`,
208
- ),
209
- )
210
- }
213
+ })
211
214
  }
@@ -6,10 +6,16 @@ import {getNextSpan} from './selector.get-next-span'
6
6
  import {getPreviousSpan} from './selector.get-previous-span'
7
7
  import {getSelectedSpans} from './selector.get-selected-spans'
8
8
 
9
- export type MarkState = {
10
- state: 'changed' | 'unchanged'
11
- marks: Array<string>
12
- }
9
+ export type MarkState =
10
+ | {
11
+ state: 'unchanged'
12
+ marks: Array<string>
13
+ }
14
+ | {
15
+ state: 'changed'
16
+ marks: Array<string>
17
+ previousMarks: Array<string>
18
+ }
13
19
 
14
20
  /**
15
21
  * Given that text is inserted at the current position, what marks should
@@ -106,21 +112,25 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
106
112
  if (previousSpanHasSameMarks) {
107
113
  return {
108
114
  state: 'changed',
115
+ previousMarks: marks,
109
116
  marks: previousSpan?.node.marks ?? [],
110
117
  }
111
118
  } else if (previousSpanHasSameAnnotations) {
112
119
  return {
113
120
  state: 'changed',
121
+ previousMarks: marks,
114
122
  marks: previousSpan?.node.marks ?? [],
115
123
  }
116
124
  } else if (previousSpanHasSameAnnotation) {
117
125
  return {
118
126
  state: 'unchanged',
127
+ previousMarks: marks,
119
128
  marks: focusSpan.node.marks ?? [],
120
129
  }
121
130
  } else if (!previousSpan) {
122
131
  return {
123
132
  state: 'changed',
133
+ previousMarks: marks,
124
134
  marks: [],
125
135
  }
126
136
  }
@@ -135,6 +145,7 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
135
145
  ) {
136
146
  return {
137
147
  state: 'changed',
148
+ previousMarks: marks,
138
149
  marks: nextSpan?.node.marks ?? [],
139
150
  }
140
151
  }
@@ -142,6 +153,7 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
142
153
  if (!nextSpan) {
143
154
  return {
144
155
  state: 'changed',
156
+ previousMarks: marks,
145
157
  marks: [],
146
158
  }
147
159
  }
@@ -152,11 +164,13 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
152
164
  if (previousSpanHasAnnotations) {
153
165
  return {
154
166
  state: 'changed',
167
+ previousMarks: marks,
155
168
  marks: [],
156
169
  }
157
170
  } else {
158
171
  return {
159
172
  state: 'changed',
173
+ previousMarks: marks,
160
174
  marks: (previousSpan?.node.marks ?? []).filter((mark) =>
161
175
  decorators.includes(mark),
162
176
  ),
@@ -166,6 +180,6 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
166
180
 
167
181
  return {
168
182
  state: 'unchanged',
169
- marks: focusSpan.node.marks ?? [],
183
+ marks,
170
184
  }
171
185
  }
@@ -1,18 +0,0 @@
1
- import {Editor} from 'slate'
2
- import {defaultKeyGenerator} from './key-generator'
3
-
4
- const CURRENT_OPERATION_ID: WeakMap<Editor, string | undefined> = new WeakMap()
5
-
6
- export function withApplyingBehaviorOperations(editor: Editor, fn: () => void) {
7
- CURRENT_OPERATION_ID.set(editor, defaultKeyGenerator())
8
- Editor.withoutNormalizing(editor, fn)
9
- CURRENT_OPERATION_ID.set(editor, undefined)
10
- }
11
-
12
- export function getCurrentOperationId(editor: Editor) {
13
- return CURRENT_OPERATION_ID.get(editor)
14
- }
15
-
16
- export function isApplyingBehaviorOperations(editor: Editor) {
17
- return getCurrentOperationId(editor) !== undefined
18
- }