scoop:scoop.ui:0.1.0
src/text/editor/drag_edit.scoop
Selected production source · src/text/editor/drag_edit.scoop
1
package scoop.ui.text3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.window.*7
/** One prepared source, passed by reference only to destinations in the same process. */8
internal class EditorTextTransfer(9
val editor: TextEditorState,10
val range: Utf8Range,11
val effects: DragEffects,12
private val sourceReady: () -> Boolean13
) {14
private val revision = editor.contentRevision15
val text = editor.text.utf8Slice(range)16
var moved = false17
val canMove: Boolean get() =18
!moved && editor.contentRevision == revision && !editor.isComposing && sourceReady()19
}21
internal fun prepareEditorMove(22
source: EditorTextTransfer,23
position: Long,24
policy: TextEditPolicy,25
singleLine: Boolean = false26
): EditorPreparedEdit? {27
if (!source.canMove)28
return None29
val editor = source.editor30
requireUi(editorScalar(editor.text.toByteArray(), position), "invalid text drop position")31
val range = source.range32
if (position >= range.start && position <= range.end)33
return Some(34
EditorPreparedEdit(35
editor.storage.value,36
None,37
EditorHistoryChange.Append([], false, false),38
true39
)40
)41
val destination =42
if (position > range.end)43
position - (range.end - range.start)44
else45
position46
val raw =47
prepareEditorInput(48
editor,49
TextInputBatch(50
Some(editor.revision),51
[52
TextInputOperation.Commit("", TextReplacement.Range(range)),53
TextInputOperation.Commit(54
source.text,55
TextReplacement.Range(Utf8Range(destination, destination))56
)57
]58
)59
)!!60
val edit =61
finishEditorInput(62
raw,63
policy,64
TextEditSource.DragDrop,65
singleLine,66
EditorEditKind.Separate,67
Some(68
EditorSelection(69
TextPosition(destination),70
TextPosition(destination + source.text.byteLength)71
)72
)73
)74
return completeEditorTransfer(raw, edit)75
}77
/** Removal applies command policy once; a transformation cannot delete unrelated content. */78
internal fun prepareEditorDragRemoval(79
source: EditorTextTransfer,80
policy: TextEditPolicy,81
singleLine: Boolean = false82
): EditorPreparedEdit? {83
if (!source.canMove)84
return None85
val editor = source.editor86
val raw =87
prepareEditorInput(88
editor,89
TextInputBatch(90
Some(editor.revision),91
[TextInputOperation.Commit("", TextReplacement.Range(source.range))]92
)93
)!!94
return completeEditorTransfer(95
raw,96
finishEditorInput(97
raw,98
policy,99
TextEditSource.EditCommand,100
singleLine,101
EditorEditKind.Separate102
)103
)104
}106
internal fun completeEditorTransfer(107
raw: EditorPreparedInput,108
edit: EditorPreparedEdit109
): EditorPreparedEdit? =110
if (edit.accepted && edit.value.input.surroundingText == raw.current.input.surroundingText)111
Some(edit)112
else113
None