Skip to content

Commit

Permalink
Merge pull request #171 from MohamedRejeb/1.x
Browse files Browse the repository at this point in the history
Save code span to html
  • Loading branch information
MohamedRejeb authored Dec 31, 2023
2 parents f0d3d5e + 78f0527 commit ab2df3b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 45 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ val isLink = richTextState.isLink

#### Add Code Blocks

To add code blocks, `RichTextState` provides `toggleCode` method:
To add code blocks, `RichTextState` provides `toggleCodeSpan` method:

```kotlin
// Toggle code block.
richTextState.toggleCode()
// Toggle code span.
richTextState.toggleCodeSpan()
```

To get if the current selection is a code block, use `RichTextState.isCode`:
To get if the current selection is a code block, use `RichTextState.isCodeSpan`:

```kotlin
// Get if the current selection is a code block.
val isCode = richTextState.isCode
// Get if the current selection is a code span.
val isCodeSpan = richTextState.isCodeSpan
```

#### Ordered and Unordered Lists
Expand Down
12 changes: 6 additions & 6 deletions docs/code_blocks.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Code Blocks

To add code blocks, `RichTextState` provides `toggleCode` method:
To add code blocks, `RichTextState` provides `toggleCodeSpan` method:

```kotlin
// Toggle code block.
richTextState.toggleCode()
// Toggle code span.
richTextState.toggleCodeSpan()
```

To get if the current selection is a code block, use `RichTextState.isCode`:
To get if the current selection is a code block, use `RichTextState.isCodeSpan`:

```kotlin
// Get if the current selection is a code block.
val isCode = richTextState.isCode
// Get if the current selection is a code span.
val isCodeSpan = richTextState.isCodeSpan
```
12 changes: 6 additions & 6 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ val isLink = richTextState.isLink

#### Add Code Blocks

To add code blocks, `RichTextState` provides `toggleCode` method:
To add code blocks, `RichTextState` provides `toggleCodeSpan` method:

```kotlin
// Toggle code block.
richTextState.toggleCode()
// Toggle code span.
richTextState.toggleCodeSpan()
```

To get if the current selection is a code block, use `RichTextState.isCode`:
To get if the current selection is a code block, use `RichTextState.isCodeSpan`:

```kotlin
// Get if the current selection is a code block.
val isCode = richTextState.isCode
// Get if the current selection is a code span.
val isCodeSpan = richTextState.isCodeSpan
```

#### Ordered and Unordered Lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ class RichTextState internal constructor(
)

val isLink get() = currentRichSpanStyle is RichSpanStyle.Link
val isCode get() = (

@Deprecated(
message = "Use isCodeSpan instead",
replaceWith = ReplaceWith("isCodeSpan"),
level = DeprecationLevel.ERROR,
)
val isCode get() = isCodeSpan

val isCodeSpan get() = (
currentRichSpanStyle is RichSpanStyle.Code ||
toAddRichSpanStyle is RichSpanStyle.Code) &&
toRemoveRichSpanStyle !is RichSpanStyle.Code
Expand Down Expand Up @@ -131,7 +139,6 @@ class RichTextState internal constructor(
getRichParagraphByTextIndex(textIndex = selection.min - 1)?.type
?: RichParagraph.Type.Default
)
private set

val isUnorderedList get() = currentRichParagraphType is RichParagraph.Type.UnorderedList
val isOrderedList get() = currentRichParagraphType is RichParagraph.Type.OrderedList
Expand Down Expand Up @@ -264,14 +271,28 @@ class RichTextState internal constructor(
)
}

fun toggleCode() {
if (isCode)
removeCode()
@Deprecated(
message = "Use toggleCodeSpan instead",
replaceWith = ReplaceWith("toggleCodeSpan()"),
level = DeprecationLevel.ERROR,
)
fun toggleCode() = toggleCodeSpan()

fun toggleCodeSpan() {
if (isCodeSpan)
removeCodeSpan()
else
addCode()
addCodeSpan()
}

fun addCode() {
@Deprecated(
message = "Use addCodeSpan instead",
replaceWith = ReplaceWith("addCodeSpan()"),
level = DeprecationLevel.ERROR,
)
fun addCode() = addCodeSpan()

fun addCodeSpan() {
if (toRemoveRichSpanStyle is RichSpanStyle.Code)
toRemoveRichSpanStyle = RichSpanStyle.Default
toAddRichSpanStyle = RichSpanStyle.Code()
Expand All @@ -280,7 +301,14 @@ class RichTextState internal constructor(
handleAddingStyleToSelectedText()
}

fun removeCode() {
@Deprecated(
message = "Use removeCodeSpan instead",
replaceWith = ReplaceWith("removeCodeSpan()"),
level = DeprecationLevel.ERROR,
)
fun removeCode() = removeCodeSpan()

fun removeCodeSpan() {
if (toAddRichSpanStyle is RichSpanStyle.Code)
toAddRichSpanStyle = RichSpanStyle.Default
toRemoveRichSpanStyle = RichSpanStyle.Code()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,38 +320,41 @@ internal object RichTextStateHtmlParser : RichTextStateParser<String> {
H6SPanStyle to "h6",
)

private const val CodeSpanTagName = "code-span"

/**
* Encodes HTML elements to [RichSpanStyle].
*/
private fun encodeHtmlElementToRichSpanStyle(
tagName: String,
attributes: Map<String, String>,
): RichSpanStyle {
return when (tagName) {
"a" -> {
val href = attributes["href"] ?: ""
return RichSpanStyle.Link(url = href)
}
else -> RichSpanStyle.Default
): RichSpanStyle =
when (tagName) {
"a" ->
RichSpanStyle.Link(url = attributes["href"].orEmpty())
CodeSpanTagName ->
RichSpanStyle.Code()
else ->
RichSpanStyle.Default
}
}

/**
* Decodes HTML elements from [RichSpanStyle].
*/
private fun decodeHtmlElementFromRichSpanStyle(
richSpanStyle: RichSpanStyle,
): Pair<String, Map<String, String>> {
return when (richSpanStyle) {
is RichSpanStyle.Link -> {
return "a" to mapOf(
): Pair<String, Map<String, String>> =
when (richSpanStyle) {
is RichSpanStyle.Link ->
"a" to mapOf(
"href" to richSpanStyle.url,
"target" to "_blank"
)
}
else -> "span" to emptyMap()
is RichSpanStyle.Code ->
CodeSpanTagName to emptyMap()
else ->
"span" to emptyMap()
}
}

/**
* Encodes HTML elements to [RichParagraph.Type].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ fun RichTextStyleRow(
item {
RichTextStyleButton(
onClick = {
state.toggleCode()
state.toggleCodeSpan()
},
isSelected = state.isCode,
isSelected = state.isCodeSpan,
icon = Icons.Outlined.Code,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ fun SlackDemoPanel(
item {
SlackDemoPanelButton(
onClick = {
state.toggleCode()
state.toggleCodeSpan()
},
isSelected = state.isCode,
isSelected = state.isCodeSpan,
icon = Icons.Outlined.Code,
)
}
Expand Down

0 comments on commit ab2df3b

Please sign in to comment.