diff --git a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/App.kt b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/App.kt index b83b108..06ad88d 100644 --- a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/App.kt +++ b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/App.kt @@ -30,7 +30,9 @@ import corp_tularegion_extension.composeapp.generated.resources.round_open_in_ne import corp_tularegion_extension.composeapp.generated.resources.round_settings_24 import org.jetbrains.compose.resources.DrawableResource import org.jetbrains.compose.resources.painterResource -import ru.tularegion.corp.backend.getRedirectMode +import ru.tularegion.corp.backend.JsCallbacks +import ru.tularegion.corp.backend.setCopyMode +import ru.tularegion.corp.backend.setRedirectMode import ru.tularegion.corp.ui.components.CopyMode import ru.tularegion.corp.ui.components.DropdownSelect import ru.tularegion.corp.ui.components.RedirectMode @@ -76,13 +78,17 @@ fun App() { Column( horizontalAlignment = Alignment.CenterHorizontally, ) { + Spacer( + modifier = Modifier + .height(8.dp), + ) Image( painter = painterResource(Res.drawable.logo_48), contentDescription = null, ) Spacer( modifier = Modifier - .height(12.dp), + .height(24.dp), ) Screen.entries.forEach { NavigationRailItem( @@ -128,11 +134,19 @@ fun App() { val redirectModeState = remember { mutableStateOf(RedirectMode.NO_REDIRECT) } + JsCallbacks.redirectMode = { value -> + redirectModeState.value = RedirectMode.entries.find { + it.id == value + } ?: RedirectMode.NO_REDIRECT + } DropdownSelect( title = "Перенаправление между RU и LOCAL", caption = "Перенаправлять все страницы на RU, либо на LOCAL", items = RedirectMode.entries, valueState = redirectModeState, + onValueChange = { + setRedirectMode(it) + }, ) val copyModeState = remember { @@ -143,6 +157,9 @@ fun App() { caption = "Заменять все ссылки при копировании на RU, либо на LOCAL", items = CopyMode.entries, valueState = copyModeState, + onValueChange = { + setCopyMode(it) + }, ) } diff --git a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/backend/api.kt b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/backend/api.kt index 128ed66..815f346 100644 --- a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/backend/api.kt +++ b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/backend/api.kt @@ -1,42 +1,86 @@ package ru.tularegion.corp.backend +import kotlin.js.ExperimentalJsExport +import kotlin.js.JsExport import kotlin.js.JsName -@JsName("getRedirectMode") -external fun getRedirectMode(): Int +object JsCallbacks { + var redirectMode: ((Int) -> Unit)? = null + var copyMode: ((Int) -> Unit)? = null + var autoAuthMode: ((Boolean) -> Unit)? = null + var autoAuthLogin: ((String) -> Unit)? = null + var autoAuthLPassword: ((String) -> Unit)? = null +} +@OptIn(ExperimentalJsExport::class) +@JsExport +fun redirectModeCallback( + value: Int, +) { + JsCallbacks.redirectMode?.let { + it(value) + } +} @JsName("setRedirectMode") external fun setRedirectMode( value: Int, ) -@JsName("getCopyMode") -external fun getCopyMode(): Int +@OptIn(ExperimentalJsExport::class) +@JsExport +fun copyModeCallback( + value: Int, +) { + JsCallbacks.copyMode?.let { + it(value) + } +} @JsName("setCopyMode") external fun setCopyMode( value: Int, ) -@JsName("getAutoAuthMode") -external fun getAutoAuthMode(): Int +@OptIn(ExperimentalJsExport::class) +@JsExport +fun autoAuthModeCallback( + value: Boolean, +) { + JsCallbacks.autoAuthMode?.let { + it(value) + } +} @JsName("setAutoAuthMode") external fun setAutoAuthMode( value: Int, ) -@JsName("getAutoAuthLogin") -external fun getAutoAuthLogin(): Int +@OptIn(ExperimentalJsExport::class) +@JsExport +fun autoAuthLoginCallback( + value: String, +) { + JsCallbacks.autoAuthLogin?.let { + it(value) + } +} @JsName("setAutoAuthLogin") external fun setAutoAuthLogin( value: String, ) -@JsName("getAutoAuthLPassword") -external fun getAutoAuthLPassword(): Int +@OptIn(ExperimentalJsExport::class) +@JsExport +fun autoAuthLPasswordCallback( + value: String, +) { + JsCallbacks.autoAuthLPassword?.let { + it(value) + } +} @JsName("setAutoAuthLPassword") external fun setAutoAuthLPassword( diff --git a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/ui/components/DropdownSelect.kt b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/ui/components/DropdownSelect.kt index 47f52df..359d0ec 100644 --- a/composeApp/src/commonMain/kotlin/ru/tularegion/corp/ui/components/DropdownSelect.kt +++ b/composeApp/src/commonMain/kotlin/ru/tularegion/corp/ui/components/DropdownSelect.kt @@ -27,8 +27,8 @@ enum class RedirectMode( override val title: String, ) : DropdownSelect { NO_REDIRECT(0, "Отключить"), - RU_TO_LOCAL(1, "Перенаправлять LOCAL на RU"), - LOCAL_TO_RU(2, "Перенаправлять RU на LOCAL"), + RU_TO_LOCAL(1, "Перенаправлять RU на LOCAL"), + LOCAL_TO_RU(2, "Перенаправлять LOCAL на RU"), } enum class CopyMode( @@ -36,8 +36,8 @@ enum class CopyMode( override val title: String, ) : DropdownSelect { NO_REPLACE(0, "Отключить"), - RU_TO_LOCAL(1, "Заменять LOCAL на RU"), - LOCAL_TO_RU(2, "Заменять RU на LOCAL"), + LOCAL_TO_RU(1, "Заменять LOCAL на RU"), + RU_TO_LOCAL(2, "Заменять RU на LOCAL"), } @OptIn(ExperimentalMaterial3Api::class) @@ -47,6 +47,7 @@ fun DropdownSelect( caption: String, items: EnumEntries, valueState: MutableState, + onValueChange: (Int) -> Unit, ) where T : Enum, T : DropdownSelect { val dropdownMenuState = remember { mutableStateOf(false) @@ -98,6 +99,7 @@ fun DropdownSelect( ) }, onClick = { + onValueChange(it.id) valueState.value = it dropdownMenuState.value = false }, diff --git a/composeApp/src/wasmJsMain/resources/api.js b/composeApp/src/wasmJsMain/resources/api.js new file mode 100644 index 0000000..af7c0c3 --- /dev/null +++ b/composeApp/src/wasmJsMain/resources/api.js @@ -0,0 +1,27 @@ +function getValueByKey(key) { + return new Promise(resolve => { + chrome.storage.local.get([key]).then((data) => { + resolve(data[key]) + }) + }) +} + +function setRedirectMode(value) { + chrome.storage.local.set({ redirectMode: value }) +} + +function setCopyMode(value) { + chrome.storage.local.set({ copyMode: value }) +} + +function setAutoAuthMode(value) { + chrome.storage.local.set({ autoAuthMode: value }) +} + +function setAutoAuthLogin(value) { + chrome.storage.local.set({ autoAuthLogin: value }) +} + +function setAutoAuthLPassword(value) { + chrome.storage.local.set({ autoAuthLPassword: value }) +} diff --git a/composeApp/src/wasmJsMain/resources/styles.css b/composeApp/src/wasmJsMain/resources/index.css similarity index 73% rename from composeApp/src/wasmJsMain/resources/styles.css rename to composeApp/src/wasmJsMain/resources/index.css index 6492070..bab6052 100644 --- a/composeApp/src/wasmJsMain/resources/styles.css +++ b/composeApp/src/wasmJsMain/resources/index.css @@ -6,10 +6,8 @@ html, body { html { position: relative; - width: 100%; - height: 100%; - max-width: 48rem; - max-height: 32rem; + width: 48rem; + height: 32rem; } body { diff --git a/composeApp/src/wasmJsMain/resources/index.html b/composeApp/src/wasmJsMain/resources/index.html index b280034..367adb3 100644 --- a/composeApp/src/wasmJsMain/resources/index.html +++ b/composeApp/src/wasmJsMain/resources/index.html @@ -4,10 +4,11 @@ Расширение Корпоративного портала - - + + + diff --git a/composeApp/src/wasmJsMain/resources/index.js b/composeApp/src/wasmJsMain/resources/index.js new file mode 100644 index 0000000..7a85b92 --- /dev/null +++ b/composeApp/src/wasmJsMain/resources/index.js @@ -0,0 +1,7 @@ +composeApp.then(module => { + getValueByKey('redirectMode').then(value => module.redirectModeCallback(value)) + getValueByKey('copyMode').then(value => module.copyModeCallback(value)) + getValueByKey('autoAuthMode').then(value => module.autoAuthModeCallback(value)) + getValueByKey('autoAuthLogin').then(value => module.autoAuthLoginCallback(value)) + getValueByKey('autoAuthLPassword').then(value => module.autoAuthLPasswordCallback(value)) +}) diff --git a/composeApp/src/wasmJsMain/resources/scripts.js b/composeApp/src/wasmJsMain/resources/scripts.js deleted file mode 100644 index 07aeab0..0000000 --- a/composeApp/src/wasmJsMain/resources/scripts.js +++ /dev/null @@ -1,54 +0,0 @@ -function _getValueByKey(key) { - let complete = false - let value; - new Promise(resolve => { - chrome.storage.local.get([key]).then((data) => { - value = data[key] - complete = true - resolve() - }) - }) - while (true) { - if (value) return value - } -} - -function getRedirectMode() { - return _getValueByKey('redirectMode') -} - -function setRedirectMode(value) { - chrome.storage.local.set({ redirectMode: value }) -} - -function getCopyMode() { - return _getValueByKey('copyMode') -} - -function setCopyMode(value) { - chrome.storage.local.set({ copyMode: value }) -} - -function getAutoAuthMode() { - return _getValueByKey('autoAuthMode') -} - -function setAutoAuthMode(value) { - chrome.storage.local.set({ autoAuthMode: value }) -} - -function getAutoAuthLogin() { - return _getValueByKey('autoAuthLogin') -} - -function setAutoAuthLogin(value) { - chrome.storage.local.set({ autoAuthLogin: value }) -} - -function getAutoAuthLPassword() { - return _getValueByKey('autoAuthLPassword') -} - -function setAutoAuthLPassword(value) { - chrome.storage.local.set({ autoAuthLPassword: value }) -}