Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,40 @@ class WriteopiaStateManager(
keyboardEventFlow
.onEach { delay(60) }
.collect { event ->
when (event) {
KeyboardEvent.DELETE -> {
if (_positionsOnEdit.value.isNotEmpty()) {
deleteSelection()
if (isEditable) {
when (event) {
KeyboardEvent.DELETE -> {
if (_positionsOnEdit.value.isNotEmpty()) {
deleteSelection()
}
}
}

KeyboardEvent.SELECT_ALL -> {
selectAll()
}
KeyboardEvent.SELECT_ALL -> {
selectAll()
}

KeyboardEvent.BOLD -> {
toggleSpan(Span.BOLD)
}
KeyboardEvent.BOLD -> {
toggleSpan(Span.BOLD)
}

KeyboardEvent.ITALIC -> {
toggleSpan(Span.ITALIC)
}
KeyboardEvent.ITALIC -> {
toggleSpan(Span.ITALIC)
}

KeyboardEvent.UNDERLINE -> {
toggleSpan(Span.UNDERLINE)
}
KeyboardEvent.UNDERLINE -> {
toggleSpan(Span.UNDERLINE)
}

KeyboardEvent.LINK -> {
addLinkToDocument()
}
KeyboardEvent.LINK -> {
addLinkToDocument()
}

KeyboardEvent.BOX -> {
toggleHighLightBlock()
}
KeyboardEvent.BOX -> {
toggleHighLightBlock()
}

else -> {}
else -> {}
}
}
}
}
Expand Down Expand Up @@ -161,6 +163,9 @@ class WriteopiaStateManager(
private val _documentInfo: MutableStateFlow<DocumentInfo> =
MutableStateFlow(DocumentInfo.empty())

private val isEditable: Boolean
get() = !_documentInfo.value.isLocked

val documentInfo: StateFlow<DocumentInfo> = _documentInfo.asStateFlow()

private val _positionsOnEdit = MutableStateFlow(setOf<Int>())
Expand Down Expand Up @@ -288,6 +293,8 @@ class WriteopiaStateManager(
* @param info [Action.Merge]
*/
fun mergeRequest(info: Action.Merge) {
if (!isEditable) return

if (isOnSelection) {
clearSelection()
}
Expand All @@ -303,6 +310,7 @@ class WriteopiaStateManager(
* @param move [Action.Move]
*/
fun moveRequest(move: Action.Move) {
if (!isEditable) return
val fixedMove = move.fixMove()

backStackManager.addState(_currentStory.value)
Expand Down Expand Up @@ -330,6 +338,7 @@ class WriteopiaStateManager(
* @param stateChange [Action.StoryStateChange]
*/
fun changeStoryState(stateChange: Action.StoryStateChange, trackIt: Boolean = true) {
if (!isEditable) return
changeStoryStateAndTrackIt(stateChange, trackIt)
}

Expand All @@ -341,6 +350,7 @@ class WriteopiaStateManager(
* Click lister when user clicks in the menu to add a check item
*/
fun onCheckItemClicked() {
if (!isEditable) return
val onEdit = _positionsOnEdit.value

if (onEdit.isNotEmpty()) {
Expand All @@ -354,6 +364,7 @@ class WriteopiaStateManager(
* Click lister when user clicks in the menu to add a list item
*/
fun onListItemClicked() {
if (!isEditable) return
val onEdit = _positionsOnEdit.value

if (onEdit.isNotEmpty()) {
Expand All @@ -364,6 +375,7 @@ class WriteopiaStateManager(
}

fun toggleHighLightBlock() {
if (!isEditable) return
val onEdit = _positionsOnEdit.value

if (onEdit.isNotEmpty()) {
Expand All @@ -376,6 +388,7 @@ class WriteopiaStateManager(
}

fun toggleTagForPosition(position: Int, tag: TagInfo, commandInfo: CommandInfo? = null) {
if (!isEditable) return
val story = getStory(position)
val storyText = story?.text

Expand Down Expand Up @@ -430,6 +443,7 @@ class WriteopiaStateManager(
* @param stateChange [Action.StoryStateChange]
*/
private fun changeStoryText(stateChange: Action.StoryStateChange) {
if (!isEditable) return
backStackManager.addTextState(_currentStory.value, stateChange.position)

changeStoryStateAndTrackIt(stateChange, trackIt = false)
Expand All @@ -445,6 +459,7 @@ class WriteopiaStateManager(
* @param commandInfo [CommandInfo]
*/
fun changeStoryType(position: Int, typeInfo: TypeInfo, commandInfo: CommandInfo?) {
if (!isEditable) return
if (isOnSelection) {
clearSelection()
}
Expand All @@ -454,6 +469,7 @@ class WriteopiaStateManager(
}

fun removeTags(position: Int) {
if (!isEditable) return
_currentStory.value =
writeopiaManager.removeTags(position, _currentStory.value)
}
Expand All @@ -465,6 +481,7 @@ class WriteopiaStateManager(
* @param lineBreak [Action.LineBreak]
*/
fun onLineBreak(lineBreak: Action.LineBreak) {
if (!isEditable) return
val lastBreak = lastLineBreak

val now = Clock.System.now().toEpochMilliseconds()
Expand Down Expand Up @@ -514,6 +531,7 @@ class WriteopiaStateManager(
}

private fun selected(isSelected: Boolean, position: Int) {
if (!isEditable) return
if (_currentStory.value.stories[position] != null) {
if (isSelected) {
_positionsOnEdit.value += position
Expand All @@ -528,10 +546,12 @@ class WriteopiaStateManager(
* perform bulk actions, like bulk edition and bulk deletion.
*/
fun onSelected(isSelected: Boolean, position: Int) {
if (!isEditable) return
selectionBuffer.send(isSelected to position)
}

fun onSectionSelected(position: Int) {
if (!isEditable) return
val isSelected = _positionsOnEdit.value.contains(position)
val stories = getStories()

Expand All @@ -555,6 +575,7 @@ class WriteopiaStateManager(
}

fun toggleSelection(position: Int) {
if (!isEditable) return
onSelected(!_positionsOnEdit.value.contains(position), position)
}

Expand Down Expand Up @@ -604,6 +625,7 @@ class WriteopiaStateManager(
* Undo the last action.
*/
override fun undo() {
if (!isEditable) return
if (!backStackManager.canUndo.value) return

coroutineScope.launch(dispatcher) {
Expand All @@ -622,6 +644,7 @@ class WriteopiaStateManager(
* Redo the last undone action.
*/
override fun redo() {
if (!isEditable) return
if (!backStackManager.canRedo.value) return

coroutineScope.launch(dispatcher) {
Expand All @@ -642,6 +665,7 @@ class WriteopiaStateManager(
* @param deleteStory [Action.DeleteStory]
*/
fun onDelete(deleteStory: Action.DeleteStory) {
if (!isEditable) return
backStackManager.addState(_currentStory.value)

coroutineScope.launch(dispatcher) {
Expand All @@ -652,6 +676,7 @@ class WriteopiaStateManager(
}

fun onErase(eraseStory: Action.EraseStory) {
if (!isEditable) return
coroutineScope.launch(dispatcher) {
val previousInfo = writeopiaManager.previousTextStory(
_currentStory.value.stories,
Expand Down Expand Up @@ -682,6 +707,7 @@ class WriteopiaStateManager(
* Deletes the whole selection. All [StoryStep] in the selection will be deleted.
*/
fun deleteSelection() {
if (!isEditable) return
coroutineScope.launch(dispatcher) {
val (newStories, _) = writeopiaManager.bulkDelete(
_positionsOnEdit.value,
Expand Down Expand Up @@ -751,25 +777,28 @@ class WriteopiaStateManager(
}

fun toggleSpan(span: Span) {
val onEdit = _positionsOnEdit.value
if (isEditable) {
val onEdit = _positionsOnEdit.value

if (onEdit.isNotEmpty()) {
_currentStory.value =
writeopiaManager.addSpanToStories(_currentStory.value, onEdit, span)
} else {
val selection = currentStory.value.selection
if (onEdit.isNotEmpty()) {
_currentStory.value =
writeopiaManager.addSpanToStories(_currentStory.value, onEdit, span)
} else {
val selection = currentStory.value.selection

val (start, end) = selection.sortedPositions()
val (start, end) = selection.sortedPositions()

_currentStory.value = writeopiaManager.addSpan(
_currentStory.value,
selection.position,
SpanInfo.create(start, end, span)
)
_currentStory.value = writeopiaManager.addSpan(
_currentStory.value,
selection.position,
SpanInfo.create(start, end, span)
)
}
}
}

fun addImage(imagePath: String, position: Int? = null) {
if (!isEditable) return
(position ?: currentPosition())?.let { pos ->
val story = getStory(pos)

Expand All @@ -792,6 +821,7 @@ class WriteopiaStateManager(
* Adds a story in a position.
*/
fun addAtPosition(storyStep: StoryStep, position: Int) {
if (!isEditable) return
_currentStory.value = writeopiaManager.addAtPosition(
_currentStory.value,
storyStep,
Expand All @@ -805,6 +835,7 @@ class WriteopiaStateManager(
lineBreakByContent: Boolean,
trackIt: Boolean = true
) {
if (!isEditable) return
val text = input.text
val step = _currentStory.value.stories[position] ?: return

Expand Down Expand Up @@ -884,6 +915,7 @@ class WriteopiaStateManager(
fun lastPosition(): Int = getStories().size

suspend fun addLinkToDocument() {
if (!isEditable) return
if (documentRepository == null) return

val lastSelection = _positionsOnEdit.value.max()
Expand Down Expand Up @@ -972,6 +1004,7 @@ class WriteopiaStateManager(
* @param cursor Int
*/
private fun nextFocusOrCreate(position: Int, cursor: Int) {
if (!isEditable) return
coroutineScope.launch(dispatcher) {
_currentStory.value =
writeopiaManager.nextFocus(position, cursor, _currentStory.value)
Expand Down Expand Up @@ -1021,6 +1054,7 @@ class WriteopiaStateManager(
}

private fun toggleStateForStories(onEdit: Set<Int>, storyTypes: StoryTypes) {
if (!isEditable) return
val currentStories = currentStory.value.stories

trackState()
Expand All @@ -1046,6 +1080,7 @@ class WriteopiaStateManager(
tag: TagInfo,
currentStories: Map<Int, StoryStep> = currentStory.value.stories
) {
if (!isEditable) return
trackState()

val change = onEdit.map { position -> position to currentStories[position] }
Expand All @@ -1065,6 +1100,7 @@ class WriteopiaStateManager(
}

private fun changeCurrentStoryType(storyTypes: StoryTypes) {
if (!isEditable) return
changeCurrentStoryState { storyStep ->
val newType = if (storyStep.type == storyTypes.type) {
StoryTypes.TEXT.type
Expand All @@ -1081,6 +1117,7 @@ class WriteopiaStateManager(
}

private fun changeCurrentStoryState(stateChange: (StoryStep) -> StoryStep) {
if (!isEditable) return
val currentStepEntry = currentFocus()

if (currentStepEntry != null) {
Expand Down Expand Up @@ -1109,6 +1146,7 @@ class WriteopiaStateManager(
stateChange: Action.StoryStateChange,
trackIt: Boolean = true
) {
if (!isEditable) return
if (lastStateChange == stateChange) return
lastStateChange = stateChange

Expand Down