getSelectedTextWithFormationChars moved from store to utils and deduplicated

This commit is contained in:
Michał Cieślak 2024-09-03 17:02:02 +02:00 committed by Michał
parent 3f4715ccdd
commit 9f94940a70
5 changed files with 28 additions and 71 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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;
}