Skip to content
Merged
Show file tree
Hide file tree
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package io.writeopia.sdk.models.command

data class Command(val commandId: Int, val commandText: String, val whereToFind: WhereToFind)
data class Command(val commandText: String, val whereToFind: WhereToFind)

enum class WhereToFind {
START,
ANYWHERE
}

object CommandFactory {
fun checkItem() = Command(1, "-[] ", WhereToFind.START)
fun checkItem() = Command("-[]", WhereToFind.START)

fun checkItem2() = Command(8, "[] ", WhereToFind.START)
fun checkItem2() = Command("[]", WhereToFind.START)

fun box() = Command(9, "/box ", WhereToFind.START)
fun box() = Command("/box", WhereToFind.START)

fun unOrderedList() = Command(2, "- ", WhereToFind.START)
fun unOrderedList() = Command("-", WhereToFind.START)

fun h1() = Command(3, "# ", WhereToFind.START)
fun h1() = Command("#", WhereToFind.START)

fun h2() = Command(4, "## ", WhereToFind.START)
fun h2() = Command("##", WhereToFind.START)

fun h3() = Command(5, "### ", WhereToFind.START)
fun h3() = Command("###", WhereToFind.START)

fun h4() = Command(6, "#### ", WhereToFind.START)
fun h4() = Command("####", WhereToFind.START)

fun codeBlock() = Command(7, "``` ", WhereToFind.START)
fun codeBlock() = Command("```", WhereToFind.START)

fun divider() = Command(7, "---", WhereToFind.START)
fun divider() = Command("---", WhereToFind.START)

fun defaultCommands(): Set<Command> =
setOf(checkItem(), unOrderedList(), h1(), h2(), h3(), h4(), codeBlock())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package io.writeopia.ui.edition

import io.writeopia.sdk.models.command.Command
import io.writeopia.sdk.models.command.CommandFactory
import io.writeopia.sdk.models.command.CommandInfo
import io.writeopia.sdk.models.command.CommandTrigger
import io.writeopia.sdk.models.command.TypeInfo
import io.writeopia.sdk.models.command.WhereToFind
import io.writeopia.sdk.models.story.Decoration
import io.writeopia.sdk.models.story.StoryStep
import io.writeopia.sdk.models.story.StoryTypes
Expand All @@ -14,25 +12,30 @@ import io.writeopia.sdk.models.story.TagInfo
import io.writeopia.ui.manager.WriteopiaStateManager

class TextCommandHandler(
private val commandsMap: Map<Command, (StoryStep, Int) -> Unit>,
private val excludeTypes: Set<Int> = setOf(StoryTypes.TITLE.type.number)
private val commandsMap: Map<String, (StoryStep, Int) -> Unit>,
private val excludeTypes: Set<Int> = setOf(StoryTypes.TITLE.type.number),
private val trie: Trie = Trie()
) {

init {
commandsMap.keys.forEach { trie.insert(it) }
}

fun handleCommand(text: String, step: StoryStep, position: Int): Boolean {
if (excludeTypes.contains(step.type.number)) return false
if (excludeTypes.contains(step.type.number) || text.lastOrNull() != ' ') return false

// Todo(Leandro): Using a reverse index would improve the speed a lot.
val command: Command = commandsMap.keys
.firstOrNull { command ->
when (command.whereToFind) {
WhereToFind.START -> text.startsWith(command.commandText)
WhereToFind.ANYWHERE -> text.contains(command.commandText)
}
} ?: return false
val textArray = text.split(" ")
if (textArray.isEmpty()) return false

commandsMap[command]!!.invoke(step.copy(text = text), position)
val command = textArray[0]
val hasCommand = trie.search(command)

return true
return if (hasCommand) {
commandsMap[command]!!.invoke(step.copy(text = text), position)
true
} else {
false
}
}

companion object {
Expand All @@ -41,7 +44,7 @@ class TextCommandHandler(
fun defaultCommands(manager: WriteopiaStateManager): TextCommandHandler {
return TextCommandHandler(
mapOf(
CommandFactory.checkItem() to { _, position ->
CommandFactory.checkItem().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(StoryTypes.CHECK_ITEM.type),
Expand All @@ -51,7 +54,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.checkItem2() to { _, position ->
CommandFactory.checkItem2().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(StoryTypes.CHECK_ITEM.type),
Expand All @@ -61,14 +64,14 @@ class TextCommandHandler(
)
)
},
CommandFactory.box() to { _, position ->
CommandFactory.box().commandText to { _, position ->
manager.toggleTagForPosition(
position,
TagInfo(Tag.HIGH_LIGHT_BLOCK),
CommandInfo(CommandFactory.box(), CommandTrigger.WRITTEN)
)
},
CommandFactory.unOrderedList() to { _, position ->
CommandFactory.unOrderedList().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(StoryTypes.UNORDERED_LIST_ITEM.type),
Expand All @@ -78,7 +81,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.h1() to { _, position ->
CommandFactory.h1().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -92,7 +95,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.h2() to { _, position ->
CommandFactory.h2().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -106,7 +109,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.h3() to { _, position ->
CommandFactory.h3().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -120,7 +123,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.h4() to { _, position ->
CommandFactory.h4().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -134,7 +137,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.codeBlock() to { _, position ->
CommandFactory.codeBlock().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -147,7 +150,7 @@ class TextCommandHandler(
)
)
},
CommandFactory.divider() to { _, position ->
CommandFactory.divider().commandText to { _, position ->
manager.changeStoryType(
position,
TypeInfo(
Expand All @@ -165,3 +168,31 @@ class TextCommandHandler(
}
}
}

class Trie {
private class TrieNode {
val children: MutableMap<Char, TrieNode> = mutableMapOf()
var isEndOfWord: Boolean = false
}

private val root = TrieNode()

fun insert(word: String) {
var node = root

word.forEach { char -> node = node.children.getOrPut(char, ::TrieNode) }
node.isEndOfWord = true
}

fun search(word: String): Boolean {
val node = findNode(word)
return node?.isEndOfWord == true
}

private fun findNode(prefix: String): TrieNode? {
var node = root

prefix.forEach { char -> node = node.children[char] ?: return null }
return node
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.writeopia.ui.edition

import io.writeopia.sdk.models.story.StoryStep
import io.writeopia.sdk.models.story.StoryTypes
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class TextCommandHandlerText {

@Test
fun `it should be possible to trigger a command`() {
val handler = TextCommandHandler(
commandsMap = mapOf(
"##" to { _, _ -> },
"command" to { _, _ -> },
"/box" to { _, _ -> },
)
)

assertTrue {
handler.handleCommand("## ", StoryStep(type = StoryTypes.TEXT.type), 0)
}
assertTrue {
handler.handleCommand("command ", StoryStep(type = StoryTypes.TEXT.type), 0)
}
assertFalse {
handler.handleCommand("notACommand ", StoryStep(type = StoryTypes.TEXT.type), 0)
}
}
}