Skip to content

Commit

Permalink
Merge branch 'handle-link-click-in-editor' into spell-check-test
Browse files Browse the repository at this point in the history
# Conflicts:
#	richeditor-compose/src/commonMain/kotlin/com/mohamedrejeb/richeditor/ui/BasicRichTextEditor.kt
  • Loading branch information
Wavesonics committed Nov 29, 2024
2 parents 0891bf1 + 16966e7 commit 47a9c60
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 67 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ kotlin = "2.0.21"
compose = "1.7.1"
dokka = "1.9.10"

kotlinxDatetime = "0.6.1"
ksoup = "0.4.0"
jetbrainsMarkdown = "0.7.3"
coil = "3.0.1"
Expand All @@ -24,7 +23,6 @@ android-minSdk = "26"
android-compileSdk = "34"

[libraries]
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinxDatetime" }
ksoup-html = { module = "com.mohamedrejeb.ksoup:ksoup-html", version.ref = "ksoup" }
ksoup-entities = { module = "com.mohamedrejeb.ksoup:ksoup-entities", version.ref = "ksoup" }
jetbrains-markdown = { module = "org.jetbrains:markdown", version.ref = "jetbrainsMarkdown" }
Expand Down
1 change: 0 additions & 1 deletion richeditor-compose/api/android/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ public final class com/mohamedrejeb/richeditor/ui/ComposableSingletons$BasicRich
public final class com/mohamedrejeb/richeditor/ui/InteractionType : java/lang/Enum {
public static final field DoubleTap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryDoubleClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field SecondaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field Tap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
Expand Down
1 change: 0 additions & 1 deletion richeditor-compose/api/desktop/richeditor-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ public final class com/mohamedrejeb/richeditor/ui/ComposableSingletons$BasicRich
public final class com/mohamedrejeb/richeditor/ui/InteractionType : java/lang/Enum {
public static final field DoubleTap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field PrimaryDoubleClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field SecondaryClick Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static final field Tap Lcom/mohamedrejeb/richeditor/ui/InteractionType;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
Expand Down
1 change: 0 additions & 1 deletion richeditor-compose/api/richeditor-compose.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ open annotation class com.mohamedrejeb.richeditor.annotation/InternalRichTextApi
final enum class com.mohamedrejeb.richeditor.ui/InteractionType : kotlin/Enum<com.mohamedrejeb.richeditor.ui/InteractionType> { // com.mohamedrejeb.richeditor.ui/InteractionType|null[0]
enum entry DoubleTap // com.mohamedrejeb.richeditor.ui/InteractionType.DoubleTap|null[0]
enum entry PrimaryClick // com.mohamedrejeb.richeditor.ui/InteractionType.PrimaryClick|null[0]
enum entry PrimaryDoubleClick // com.mohamedrejeb.richeditor.ui/InteractionType.PrimaryDoubleClick|null[0]
enum entry SecondaryClick // com.mohamedrejeb.richeditor.ui/InteractionType.SecondaryClick|null[0]
enum entry Tap // com.mohamedrejeb.richeditor.ui/InteractionType.Tap|null[0]

Expand Down
2 changes: 0 additions & 2 deletions richeditor-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ kotlin {
implementation(compose.material)
implementation(compose.material3)

implementation(libs.kotlinx.datetime)

// HTML parsing library
implementation(libs.ksoup.html)
implementation(libs.ksoup.entities)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mohamedrejeb.richeditor.ui

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
Expand Down Expand Up @@ -37,9 +36,6 @@ import androidx.compose.ui.unit.LayoutDirection
import com.mohamedrejeb.richeditor.model.RichSpanStyle
import com.mohamedrejeb.richeditor.model.RichTextState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock
import kotlin.time.Duration.Companion.milliseconds


/**
Expand Down Expand Up @@ -348,60 +344,4 @@ internal suspend fun adjustTextIndicatorOffset(
)
}

public enum class InteractionType { PrimaryClick, SecondaryClick, Tap, DoubleTap }
public typealias RichSpanClickListener = (RichSpanStyle, TextRange, Offset, InteractionType) -> Boolean

/**
* Provide a unified callback for listening for different types of interactions
*/
private fun Modifier.handleInteractions(
enabled: Boolean = true,
onInteraction: ((InteractionType, Offset) -> Boolean)? = null
): Modifier = composed {
this
.pointerInput(enabled) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent(PointerEventPass.Main)
if (!enabled) continue

if (event.type == PointerEventType.Press) {
val position = event.changes.first().position

when (event.changes.first().type) {
PointerType.Touch -> {
onInteraction?.invoke(
InteractionType.Tap,
event.changes.first().position
)
}

PointerType.Mouse -> {
if (event.buttons.isPrimaryPressed) {
val consumed =
onInteraction?.invoke(
InteractionType.PrimaryClick,
position
)
?: false
if (consumed) {
event.changes.forEach { it.consume() }
}
} else if (event.buttons.isSecondaryPressed) {
val consumed =
onInteraction?.invoke(
InteractionType.SecondaryClick,
position
)
?: false
if (consumed) {
event.changes.forEach { it.consume() }
}
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.mohamedrejeb.richeditor.ui

import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.isPrimaryPressed
import androidx.compose.ui.input.pointer.isSecondaryPressed
import androidx.compose.ui.input.pointer.pointerInput

public enum class InteractionType { PrimaryClick, SecondaryClick, Tap, DoubleTap }

/**
* Provide a unified callback for listening for different types of interactions
*/
internal fun Modifier.handleInteractions(
enabled: Boolean = true,
onInteraction: ((InteractionType, Offset) -> Boolean)? = null
): Modifier = composed {
this
.pointerInput(enabled) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent(PointerEventPass.Main)
if (!enabled) continue

if (event.type == PointerEventType.Press) {
val eventChange = event.changes.first()
val position = eventChange.position

when (eventChange.type) {
PointerType.Touch -> {
onInteraction?.invoke(
InteractionType.Tap,
eventChange.position
)
}

PointerType.Mouse -> {
if (event.buttons.isPrimaryPressed) {
val consumed =
onInteraction?.invoke(
InteractionType.PrimaryClick,
position
)
?: false
if (consumed) {
event.changes.forEach { it.consume() }
}
} else if (event.buttons.isSecondaryPressed) {
val consumed =
onInteraction?.invoke(
InteractionType.SecondaryClick,
position
)
?: false
if (consumed) {
event.changes.forEach { it.consume() }
}
}
}
}
}
}
}
}
}

0 comments on commit 47a9c60

Please sign in to comment.