From 9f94940a70fd76917d3ed6be191a799b184ce112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Tue, 3 Sep 2024 17:02:02 +0200 Subject: [PATCH] getSelectedTextWithFormationChars moved from store to utils and deduplicated --- storybook/pages/StatusChatInputPage.qml | 22 ------------ .../qmlTests/tests/tst_StatusChatInput.qml | 20 ----------- storybook/stubs/shared/stores/RootStore.qml | 1 - ui/imports/shared/status/StatusChatInput.qml | 36 ++++++++++++++----- ui/imports/shared/stores/RootStore.qml | 20 ----------- 5 files changed, 28 insertions(+), 71 deletions(-) diff --git a/storybook/pages/StatusChatInputPage.qml b/storybook/pages/StatusChatInputPage.qml index 533536f9d9..3f4cc6fddf 100644 --- a/storybook/pages/StatusChatInputPage.qml +++ b/storybook/pages/StatusChatInputPage.qml @@ -44,31 +44,9 @@ SplitView { readonly property ListModel gifColumnA: ListModel {} - readonly property var formationChars: (["*", "`", "~"]) - - function getSelectedTextWithFormationChars(messageInputField) { - let i = 1 - let text = "" - while (true) { - if (messageInputField.selectionStart - i < 0 && messageInputField.selectionEnd + i > messageInputField.length) { - break - } - - text = messageInputField.getText(messageInputField.selectionStart - i, messageInputField.selectionEnd + i) - - if (!formationChars.includes(text.charAt(0)) || - !formationChars.includes(text.charAt(text.length - 1))) { - break - } - i++ - } - return text - } - Component.onCompleted: { RootStore.isWalletEnabled = true RootStore.gifUnfurlingEnabled = true - RootStore.getSelectedTextWithFormationChars = rootStoreMock.getSelectedTextWithFormationChars RootStore.gifColumnA = rootStoreMock.gifColumnA rootStoreMock.ready = true } diff --git a/storybook/qmlTests/tests/tst_StatusChatInput.qml b/storybook/qmlTests/tests/tst_StatusChatInput.qml index 9cfe4b087f..47fdbd3bd4 100644 --- a/storybook/qmlTests/tests/tst_StatusChatInput.qml +++ b/storybook/qmlTests/tests/tst_StatusChatInput.qml @@ -703,31 +703,11 @@ Item { property ListModel gifColumnA: ListModel {} - readonly property var formationChars: (["*", "`", "~"]) property bool gifUnfurlingEnabled: true - function getSelectedTextWithFormationChars(messageInputField) { - let i = 1 - let text = "" - while (true) { - if (messageInputField.selectionStart - i < 0 && messageInputField.selectionEnd + i > messageInputField.length) { - break - } - - text = messageInputField.getText(messageInputField.selectionStart - i, messageInputField.selectionEnd + i) - - if (!formationChars.includes(text.charAt(0)) || - !formationChars.includes(text.charAt(text.length - 1))) { - break - } - i++ - } - return text - } Component.onCompleted: { RootStore.isWalletEnabled = true RootStore.gifUnfurlingEnabled = rootStoreMock.gifUnfurlingEnabled - RootStore.getSelectedTextWithFormationChars = rootStoreMock.getSelectedTextWithFormationChars RootStore.gifColumnA = rootStoreMock.gifColumnA Global.dragArea = root diff --git a/storybook/stubs/shared/stores/RootStore.qml b/storybook/stubs/shared/stores/RootStore.qml index ad321785ed..84f235c642 100644 --- a/storybook/stubs/shared/stores/RootStore.qml +++ b/storybook/stubs/shared/stores/RootStore.qml @@ -4,7 +4,6 @@ QtObject { property var userProfileInst property bool gifUnfurlingEnabled property bool isWalletEnabled - property var getSelectedTextWithFormationChars property var gifColumnA property var currentCurrency property bool neverAskAboutUnfurlingAgain: false diff --git a/ui/imports/shared/status/StatusChatInput.qml b/ui/imports/shared/status/StatusChatInput.qml index 0e8295dedf..8319d90d6b 100644 --- a/ui/imports/shared/status/StatusChatInput.qml +++ b/ui/imports/shared/status/StatusChatInput.qml @@ -295,6 +295,26 @@ Rectangle { function getMentionAtPosition(position: int) { return mentionsPos.find(mention => mention.leftIndex < position && mention.rightIndex > position) } + + function getSelectedTextWithFormationChars(messageInputField) { + const formationChars = ["*", "`", "~"] + let i = 1 + let text = "" + while (true) { + if (messageInputField.selectionStart - i < 0 && messageInputField.selectionEnd + i > messageInputField.length) { + break + } + + text = messageInputField.getText(messageInputField.selectionStart - i, messageInputField.selectionEnd + i) + + if (!formationChars.includes(text.charAt(0)) || + !formationChars.includes(text.charAt(text.length - 1))) { + break + } + i++ + } + return text + } } function insertInTextInput(start, text) { @@ -1089,37 +1109,37 @@ Rectangle { wrapper: "**" icon.name: "bold" text: qsTr("Bold") - selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) + selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField) onActionTriggered: checked ? - unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : + unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) : wrapSelection(wrapper) } StatusChatInputTextFormationAction { wrapper: "*" icon.name: "italic" text: qsTr("Italic") - selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) + selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField) checked: (surroundedBy("*") && !surroundedBy("**")) || surroundedBy("***") onActionTriggered: checked ? - unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : + unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) : wrapSelection(wrapper) } StatusChatInputTextFormationAction { wrapper: "~~" icon.name: "strikethrough" text: qsTr("Strikethrough") - selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) + selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField) onActionTriggered: checked ? - unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : + unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) : wrapSelection(wrapper) } StatusChatInputTextFormationAction { wrapper: "`" icon.name: "code" text: qsTr("Code") - selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) + selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField) onActionTriggered: checked ? - unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : + unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) : wrapSelection(wrapper) } StatusChatInputTextFormationAction { diff --git a/ui/imports/shared/stores/RootStore.qml b/ui/imports/shared/stores/RootStore.qml index 1a9ff827e9..9e897e5db3 100644 --- a/ui/imports/shared/stores/RootStore.qml +++ b/ui/imports/shared/stores/RootStore.qml @@ -37,26 +37,6 @@ QtObject { property var flatNetworks: networksModule.flatNetworks - readonly property var formationChars: (["*", "`", "~"]) - function getSelectedTextWithFormationChars(messageInputField) { - let i = 1 - let text = "" - while (true) { - if (messageInputField.selectionStart - i < 0 && messageInputField.selectionEnd + i > messageInputField.length) { - break - } - - text = messageInputField.getText(messageInputField.selectionStart - i, messageInputField.selectionEnd + i) - - if (!formationChars.includes(text.charAt(0)) || - !formationChars.includes(text.charAt(text.length - 1))) { - break - } - i++ - } - return text - } - function setNeverAskAboutUnfurlingAgain(value) { localAccountSensitiveSettings.neverAskAboutUnfurlingAgain = value; }