Skip to content

Commit

Permalink
Merge pull request #16 from Infomaniak/undo-redo
Browse files Browse the repository at this point in the history
Support undo and redo
  • Loading branch information
LunarX authored Jun 20, 2024
2 parents b574df9 + fe66b3d commit ef8d811
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ internal class JsBridge(
execCommand(StatusCommand.FONT_SIZE, fontSize)
}

fun undo() = execCommand(OtherCommand.UNDO)

fun redo() = execCommand(OtherCommand.REDO)

fun createLink(displayText: String?, url: String) {
jsExecutor.executeImmediatelyAndRefreshToolbar(JsExecutableMethod("createLink", displayText, url))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class RichHtmlEditorWebView @JvmOverloads constructor(
* called with the argument `fontSize`.
*/
fun setFontSize(@IntRange(from = FONT_MIN_SIZE, to = FONT_MAX_SIZE) fontSize: Int) = jsBridge.setFontSize(fontSize)
fun undo() = jsBridge.undo()
fun redo() = jsBridge.redo()
fun createLink(displayText: String?, url: String) = jsBridge.createLink(displayText?.takeIf { it.isNotBlank() }, url)
fun unlink() = jsBridge.unlink()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ enum class OtherCommand(override val argumentName: String) : ExecCommand {
REMOVE_FORMAT("removeFormat"),
INDENT("indent"),
OUTDENT("outdent"),
UNDO("undo"),
REDO("redo"),
}

interface ExecCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class EditorSampleFragment : Fragment() {
fontSmallButton.setOnClickListener { editor.setFontSize(SMALL_FONT_SIZE) }
fontMediumButton.setOnClickListener { editor.setFontSize(MEDIUM_FONT_SIZE) }
fontBigButton.setOnClickListener { editor.setFontSize(BIG_FONT_SIZE) }
undoButton.setOnClickListener { editor.undo() }
redoButton.setOnClickListener { editor.redo() }
}

private fun observeEditorStatusUpdates() = with(binding) {
Expand Down
13 changes: 13 additions & 0 deletions sample/src/main/res/drawable/ic_redo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="#000000"
android:viewportWidth="960"
android:viewportHeight="960">

<path
android:fillColor="@android:color/white"
android:pathData="M648,400L396,400Q333,400 286.5,440Q240,480 240,540Q240,600 286.5,640Q333,680 396,680L640,680Q657,680 668.5,691.5Q680,703 680,720Q680,737 668.5,748.5Q657,760 640,760L396,760Q299,760 229.5,697Q160,634 160,540Q160,446 229.5,383Q299,320 396,320L648,320L572,244Q561,233 561,216Q561,199 572,188Q583,177 600,177Q617,177 628,188L772,332Q778,338 780.5,345Q783,352 783,360Q783,368 780.5,375Q778,382 772,388L628,532Q617,543 600,543Q583,543 572,532Q561,521 561,504Q561,487 572,476L648,400Z" />

</vector>
13 changes: 13 additions & 0 deletions sample/src/main/res/drawable/ic_undo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="#000000"
android:viewportWidth="960"
android:viewportHeight="960">

<path
android:fillColor="@android:color/white"
android:pathData="M320,760Q303,760 291.5,748.5Q280,737 280,720Q280,703 291.5,691.5Q303,680 320,680L564,680Q627,680 673.5,640Q720,600 720,540Q720,480 673.5,440Q627,400 564,400L312,400L388,476Q399,487 399,504Q399,521 388,532Q377,543 360,543Q343,543 332,532L188,388Q182,382 179.5,375Q177,368 177,360Q177,352 179.5,345Q182,338 188,332L332,188Q343,177 360,177Q377,177 388,188Q399,199 399,216Q399,233 388,244L312,320L564,320Q661,320 730.5,383Q800,446 800,540Q800,634 730.5,697Q661,760 564,760L320,760Z" />

</vector>
23 changes: 22 additions & 1 deletion sample/src/main/res/layout/fragment_editor_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@

<LinearLayout
android:id="@+id/fontLayout"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">

Expand Down Expand Up @@ -256,6 +256,27 @@
android:layout_gravity="end"
android:text="Big" />

<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />

<com.google.android.material.button.MaterialButton
android:id="@+id/undoButton"
style="@style/EditorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
app:icon="@drawable/ic_undo" />

<com.google.android.material.button.MaterialButton
android:id="@+id/redoButton"
style="@style/EditorButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
app:icon="@drawable/ic_redo" />

</LinearLayout>

<com.google.android.material.divider.MaterialDivider
Expand Down

0 comments on commit ef8d811

Please sign in to comment.