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 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: { Component.onCompleted: {
RootStore.isWalletEnabled = true RootStore.isWalletEnabled = true
RootStore.gifUnfurlingEnabled = true RootStore.gifUnfurlingEnabled = true
RootStore.getSelectedTextWithFormationChars = rootStoreMock.getSelectedTextWithFormationChars
RootStore.gifColumnA = rootStoreMock.gifColumnA RootStore.gifColumnA = rootStoreMock.gifColumnA
rootStoreMock.ready = true rootStoreMock.ready = true
} }

View File

@ -703,31 +703,11 @@ Item {
property ListModel gifColumnA: ListModel {} property ListModel gifColumnA: ListModel {}
readonly property var formationChars: (["*", "`", "~"])
property bool gifUnfurlingEnabled: true 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: { Component.onCompleted: {
RootStore.isWalletEnabled = true RootStore.isWalletEnabled = true
RootStore.gifUnfurlingEnabled = rootStoreMock.gifUnfurlingEnabled RootStore.gifUnfurlingEnabled = rootStoreMock.gifUnfurlingEnabled
RootStore.getSelectedTextWithFormationChars = rootStoreMock.getSelectedTextWithFormationChars
RootStore.gifColumnA = rootStoreMock.gifColumnA RootStore.gifColumnA = rootStoreMock.gifColumnA
Global.dragArea = root Global.dragArea = root

View File

@ -4,7 +4,6 @@ QtObject {
property var userProfileInst property var userProfileInst
property bool gifUnfurlingEnabled property bool gifUnfurlingEnabled
property bool isWalletEnabled property bool isWalletEnabled
property var getSelectedTextWithFormationChars
property var gifColumnA property var gifColumnA
property var currentCurrency property var currentCurrency
property bool neverAskAboutUnfurlingAgain: false property bool neverAskAboutUnfurlingAgain: false

View File

@ -295,6 +295,26 @@ Rectangle {
function getMentionAtPosition(position: int) { function getMentionAtPosition(position: int) {
return mentionsPos.find(mention => mention.leftIndex < position && mention.rightIndex > position) 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) { function insertInTextInput(start, text) {
@ -1089,37 +1109,37 @@ Rectangle {
wrapper: "**" wrapper: "**"
icon.name: "bold" icon.name: "bold"
text: qsTr("Bold") text: qsTr("Bold")
selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField)
onActionTriggered: checked ? onActionTriggered: checked ?
unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) :
wrapSelection(wrapper) wrapSelection(wrapper)
} }
StatusChatInputTextFormationAction { StatusChatInputTextFormationAction {
wrapper: "*" wrapper: "*"
icon.name: "italic" icon.name: "italic"
text: qsTr("Italic") text: qsTr("Italic")
selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField)
checked: (surroundedBy("*") && !surroundedBy("**")) || surroundedBy("***") checked: (surroundedBy("*") && !surroundedBy("**")) || surroundedBy("***")
onActionTriggered: checked ? onActionTriggered: checked ?
unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) :
wrapSelection(wrapper) wrapSelection(wrapper)
} }
StatusChatInputTextFormationAction { StatusChatInputTextFormationAction {
wrapper: "~~" wrapper: "~~"
icon.name: "strikethrough" icon.name: "strikethrough"
text: qsTr("Strikethrough") text: qsTr("Strikethrough")
selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField)
onActionTriggered: checked ? onActionTriggered: checked ?
unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) :
wrapSelection(wrapper) wrapSelection(wrapper)
} }
StatusChatInputTextFormationAction { StatusChatInputTextFormationAction {
wrapper: "`" wrapper: "`"
icon.name: "code" icon.name: "code"
text: qsTr("Code") text: qsTr("Code")
selectedTextWithFormationChars: RootStore.getSelectedTextWithFormationChars(messageInputField) selectedTextWithFormationChars: d.getSelectedTextWithFormationChars(messageInputField)
onActionTriggered: checked ? onActionTriggered: checked ?
unwrapSelection(wrapper, RootStore.getSelectedTextWithFormationChars(messageInputField)) : unwrapSelection(wrapper, d.getSelectedTextWithFormationChars(messageInputField)) :
wrapSelection(wrapper) wrapSelection(wrapper)
} }
StatusChatInputTextFormationAction { StatusChatInputTextFormationAction {

View File

@ -37,26 +37,6 @@ QtObject {
property var flatNetworks: networksModule.flatNetworks 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) { function setNeverAskAboutUnfurlingAgain(value) {
localAccountSensitiveSettings.neverAskAboutUnfurlingAgain = value; localAccountSensitiveSettings.neverAskAboutUnfurlingAgain = value;
} }