scoop:scoop.ui:0.1.0
src/text/editor/state.scoop
Selected production source · src/text/editor/state.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.*6
import scoop.window.accessibility.SemanticTextDocument8
/** Observable plain text and edit history, independent of any mounted editor. */9
public class TextEditorState public constructor(10
text: String = "",11
selection: Utf8Selection? = None12
) {13
internal val storage = state(editorInitial(text, selection))14
internal val history = EditorHistory()15
internal var composition: EditorComposition? = None16
internal var contentRevision = 0L17
private var activeHost: TextEditHost? = None18
internal var inputStarter: TextInputStarter? = None19
internal val semanticDocuments = ArrayList<SemanticTextDocument>()21
internal fun checkHost(host: TextEditHost) {22
storage.checkMutation()23
when (activeHost) {24
case Some(val value) -> requireUi(25
value === host,26
"text state already has an active editor"27
)28
case None -> {}29
}30
}31
internal fun attachHost(host: TextEditHost) {32
checkHost(host)33
inputStarter?.cancel()34
activeHost = Some(host)35
}36
internal fun ownsHost(host: TextEditHost): Boolean =37
when (activeHost) {38
case Some(val value) -> value === host39
case None -> false40
}41
internal fun detachHost(host: TextEditHost): Boolean {42
storage.checkMutation()43
if (!ownsHost(host))44
return false45
activeHost = None46
return true47
}49
/** Observes the document while excluding an active composition's temporary text. */50
public val committedText: String get() {51
val current = storage.value52
return (composition?.baseline ?: current).input.surroundingText53
}55
public val inputState: TextInputState get() = storage.value.input56
public val text: String get() = inputState.surroundingText57
public val selection: Utf8Selection get() = inputState.selection58
public val markedRange: Utf8Range? get() = inputState.markedRange59
public val revision: Long get() = inputState.revision60
public val isComposing: Boolean get() = markedRange != None61
public val anchor: TextPosition get() = storage.value.endpoints.anchor62
public val cursor: TextPosition get() = storage.value.endpoints.cursor63
public val canUndo: Boolean get() {64
storage.value65
return isComposing || history.undoDelta != None66
}67
public val canRedo: Boolean get() {68
storage.value69
return history.redoDelta != None70
}72
public fun setSelection(value: Utf8Selection): Boolean =73
setSelection(TextPosition(value.anchor), TextPosition(value.cursor))75
public fun setSelection(anchor: TextPosition, cursor: TextPosition): Boolean {76
storage.checkMutation()77
val previous = storage.value78
val input =79
TextInputState(80
previous.input.surroundingText,81
Utf8Selection(anchor.byteOffset, cursor.byteOffset)82
)83
input.validate()84
val edits = endingComposition(previous)85
return publish(86
EditorValue(input, anchor.affinity, cursor.affinity),87
None,88
EditorHistoryChange.Append(edits.toArray(), true, true)89
)90
}92
public fun replace(range: Utf8Range, text: String): Boolean =93
edit(range, text, EditorEditKind.Separate)95
internal fun edit(range: Utf8Range, text: String, kind: EditorEditKind): Boolean {96
storage.checkMutation()97
val previous = storage.value98
val next =99
EditorValue(100
editorVersion(previous, 0L).input.apply(101
TextInputBatch(102
None,103
[TextInputOperation.Commit(text, TextReplacement.Range(range))]104
)105
)106
)107
val edits = endingComposition(previous)108
appendEditorDelta(edits, previous, next, kind)109
val boundary = isComposing || kind == EditorEditKind.Separate110
return publish(next, None, EditorHistoryChange.Append(edits.toArray(), boundary, boundary))111
}113
public fun replaceAll(text: String, selection: Utf8Selection? = None): Boolean {114
storage.checkMutation()115
return publish(editorInitial(text, selection), None, EditorHistoryChange.Reset)116
}118
public fun applyInput(119
batch: TextInputBatch,120
policy: TextEditPolicy = textEditPolicy(),121
source: TextEditSource = TextEditSource.NativeInput122
): Boolean = applyEditorInput(this, batch, policy = policy, source = source)124
public fun applyEdit(125
range: Utf8Range,126
text: String,127
policy: TextEditPolicy,128
source: TextEditSource = TextEditSource.Programmatic129
): Boolean = prepareEditorEdit(this, range, text, policy, source).publish(this, false)131
public fun cancelComposition(): Boolean {132
storage.checkMutation()133
val baseline = composition?.baseline ?: return false134
return publish(baseline, None, EditorHistoryChange.Break)135
}137
public fun undo(): Boolean {138
storage.checkMutation()139
val value = composition?.baseline ?: storage.value140
when (history.undoDelta) {141
case Some(val delta) -> return publish(142
replayEditorDelta(value, delta, true),143
None,144
EditorHistoryChange.Undo145
)146
case None -> return if (isComposing)147
publish(value, None, EditorHistoryChange.Break)148
else149
false150
}151
}153
public fun redo(): Boolean {154
storage.checkMutation()155
val delta = history.redoDelta ?: return false156
return publish(157
replayEditorDelta(composition?.baseline ?: storage.value, delta, false),158
None,159
EditorHistoryChange.Redo160
)161
}163
public fun breakUndoGroup() {164
storage.checkMutation()165
history.breakGroup()166
}168
private fun endingComposition(previous: EditorValue): ArrayList<EditorDelta> {169
val edits = ArrayList<EditorDelta>()170
when (composition) {171
case Some(val baseline) -> appendEditorDelta(172
edits,173
baseline.baseline,174
previous,175
EditorEditKind.Separate176
)177
case None -> {}178
}179
return edits180
}182
/** Preparation validates first; publication reuses the transaction's mutation guard. */183
internal fun publish(184
next: EditorValue,185
baseline: EditorComposition?,186
change: EditorHistoryChange,187
forceRevision: Boolean = false,188
mapping: EditorOffsetMap? = None189
): Boolean {190
val previous = storage.value191
val a = previous.input192
val b = next.input193
activeHost?.checkDocument((baseline?.baseline ?: next).input.surroundingText)194
val revise =195
forceRevision ||196
a.surroundingText !=197
b.surroundingText ||198
a.selection !=199
b.selection ||200
a.markedRange !=201
b.markedRange202
requireUi(!revise || a.revision < 9223372036854775807L, "text revision overflow")203
val value =204
editorVersion(205
next,206
a.revision +207
if (revise)208
1L209
else210
0L211
)212
val compositionChanged =213
when (baseline) {214
case Some(val nextComposition) -> when (composition) {215
case Some(val previousComposition) -> nextComposition !== previousComposition216
case None -> true217
}218
case None -> when (composition) {219
case Some(_) -> true220
case None -> false221
}222
}223
val changed = value != previous || compositionChanged224
mapEditorSemanticDocuments(this, previous, value, change, mapping)225
val changedHistory = history.apply(change)226
composition = baseline227
if (a.surroundingText != b.surroundingText)228
contentRevision = value.input.revision229
if (changed || changedHistory)230
storage.publish(value)231
if (changed) {232
activeHost?.modelChanged()233
inputStarter?.modelChanged()234
}235
return changed || changedHistory236
}237
}