-
Notifications
You must be signed in to change notification settings - Fork 61
/
mark_step.js
115 lines (98 loc) · 3.67 KB
/
mark_step.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {Fragment, Slice} from "prosemirror-model"
import {Step, StepResult} from "./step"
function mapFragment(fragment, f, parent) {
let mapped = []
for (let i = 0; i < fragment.childCount; i++) {
let child = fragment.child(i)
if (child.content.size) child = child.copy(mapFragment(child.content, f, child))
if (child.isInline) child = f(child, parent, i)
mapped.push(child)
}
return Fragment.fromArray(mapped)
}
// ::- Add a mark to all inline content between two positions.
export class AddMarkStep extends Step {
// :: (number, number, Mark)
constructor(from, to, mark) {
super()
this.from = from
this.to = to
this.mark = mark
}
apply(doc) {
let oldSlice = doc.slice(this.from, this.to), $from = doc.resolve(this.from)
let parent = $from.node($from.sharedDepth(this.to))
let slice = new Slice(mapFragment(oldSlice.content, (node, parent) => {
if (!parent.type.allowsMarkType(this.mark.type)) return node
return node.mark(this.mark.addToSet(node.marks))
}, parent), oldSlice.openStart, oldSlice.openEnd)
return StepResult.fromReplace(doc, this.from, this.to, slice)
}
invert() {
return new RemoveMarkStep(this.from, this.to, this.mark)
}
map(mapping) {
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
if (from.deleted && to.deleted || from.pos >= to.pos) return null
return new AddMarkStep(from.pos, to.pos, this.mark)
}
merge(other) {
if (other instanceof AddMarkStep &&
other.mark.eq(this.mark) &&
this.from <= other.to && this.to >= other.from)
return new AddMarkStep(Math.min(this.from, other.from),
Math.max(this.to, other.to), this.mark)
}
toJSON() {
return {stepType: "addMark", mark: this.mark.toJSON(),
from: this.from, to: this.to}
}
static fromJSON(schema, json) {
if (typeof json.from != "number" || typeof json.to != "number")
throw new RangeError("Invalid input for AddMarkStep.fromJSON")
return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark))
}
}
Step.jsonID("addMark", AddMarkStep)
// ::- Remove a mark from all inline content between two positions.
export class RemoveMarkStep extends Step {
// :: (number, number, Mark)
constructor(from, to, mark) {
super()
this.from = from
this.to = to
this.mark = mark
}
apply(doc) {
let oldSlice = doc.slice(this.from, this.to)
let slice = new Slice(mapFragment(oldSlice.content, node => {
return node.mark(this.mark.removeFromSet(node.marks))
}), oldSlice.openStart, oldSlice.openEnd)
return StepResult.fromReplace(doc, this.from, this.to, slice)
}
invert() {
return new AddMarkStep(this.from, this.to, this.mark)
}
map(mapping) {
let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
if (from.deleted && to.deleted || from.pos >= to.pos) return null
return new RemoveMarkStep(from.pos, to.pos, this.mark)
}
merge(other) {
if (other instanceof RemoveMarkStep &&
other.mark.eq(this.mark) &&
this.from <= other.to && this.to >= other.from)
return new RemoveMarkStep(Math.min(this.from, other.from),
Math.max(this.to, other.to), this.mark)
}
toJSON() {
return {stepType: "removeMark", mark: this.mark.toJSON(),
from: this.from, to: this.to}
}
static fromJSON(schema, json) {
if (typeof json.from != "number" || typeof json.to != "number")
throw new RangeError("Invalid input for RemoveMarkStep.fromJSON")
return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark))
}
}
Step.jsonID("removeMark", RemoveMarkStep)