Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markdown): add image button [KHCP-12213] #88

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(markdown): fix autoselect [KHCP-12213]
DariaYeremina committed Jun 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 71b7e0419bfc10b2a19c068a854f090a4aded68e
2 changes: 1 addition & 1 deletion src/components/MarkdownUi.spec.ts
Original file line number Diff line number Diff line change
@@ -641,7 +641,7 @@ describe('<MarkdownUi />', () => {
await waitForEmittedEvent(wrapper, eventName)

expect(wrapper.emitted(eventName) || []).toHaveLength(1)
expect(wrapper.emitted(eventName)![0][0]).toContain([MARKDOWN_TEMPLATE_LINK.replace(/text/, '').replace(/url/, linkUrl)])
expect(wrapper.emitted(eventName)![0][0]).toContain([MARKDOWN_TEMPLATE_LINK.replace(/url/, linkUrl)])
})
})
})
17 changes: 11 additions & 6 deletions src/composables/useMarkdownActions.ts
Original file line number Diff line number Diff line change
@@ -441,6 +441,8 @@ export default function useMarkdownActions(
const startText = rawMarkdown.value.substring(0, selectedText.start)
const endText = rawMarkdown.value.substring(selectedText.end)
let newContent: string = ''
const urlTextLength = 3
const textTextLength = 4

// If text is selected, check the type of selected text and insert the link template around it
if (selectedText.text.length !== 0) {
@@ -453,7 +455,7 @@ export default function useMarkdownActions(
// Check if the selected text is a URL
const isUrl = /^http(s)?:\/\//.test(selectedText.text)
// Prepare the content
newContent = isUrl ? MARKDOWN_TEMPLATE_LINK.replace(/text/, '').replace(/url/, selectedText.text) : MARKDOWN_TEMPLATE_LINK.replace(/text/, selectedText.text)
newContent = isUrl ? MARKDOWN_TEMPLATE_LINK.replace(/url/, selectedText.text) : MARKDOWN_TEMPLATE_LINK.replace(/text/, selectedText.text)

// Update the markdown
rawMarkdown.value = startText + newContent + endText
@@ -468,6 +470,8 @@ export default function useMarkdownActions(
textarea.selectionStart = startText.length + selectedText.text.length + 3
textarea.selectionEnd = startText.length + selectedText.text.length + 6
}

selectText(textarea.selectionStart, textarea.selectionStart + textTextLength)
} else {
// No text is selected

@@ -499,8 +503,6 @@ export default function useMarkdownActions(
// Set the cursor position
textarea.selectionEnd = selectedText.start + cursorPosition

const urlTextLength = 3

selectText(textarea.selectionEnd, textarea.selectionEnd + urlTextLength)
}
} catch (err) {
@@ -527,6 +529,8 @@ export default function useMarkdownActions(
const startText = rawMarkdown.value.substring(0, selectedText.start)
const endText = rawMarkdown.value.substring(selectedText.end)
let newContent: string = ''
const urlTextLength = 3
const altTextLength = 3

// If text is selected, check the type of selected text and insert the link template around it
if (selectedText.text.length !== 0) {
@@ -539,7 +543,8 @@ export default function useMarkdownActions(
// Check if the selected text is a URL
const isUrl = /^http(s)?:\/\//.test(selectedText.text)
// Prepare the content
newContent = isUrl ? MARKDOWN_TEMPLATE_IMAGE.replace(/alt/, '').replace(/url/, selectedText.text) : MARKDOWN_TEMPLATE_IMAGE.replace(/alt/, selectedText.text)
newContent = isUrl ? MARKDOWN_TEMPLATE_IMAGE.replace(/url/, selectedText.text) : MARKDOWN_TEMPLATE_IMAGE.replace(/alt/, selectedText.text)


// Update the markdown
rawMarkdown.value = startText + newContent + endText
@@ -554,6 +559,8 @@ export default function useMarkdownActions(
textarea.selectionStart = startText.length + selectedText.text.length + 4
textarea.selectionEnd = startText.length + selectedText.text.length + 7
}

selectText(textarea.selectionStart, textarea.selectionStart + altTextLength)
} else {
// No text is selected

@@ -585,8 +592,6 @@ export default function useMarkdownActions(
// Set the cursor position
textarea.selectionEnd = selectedText.start + cursorPosition

const urlTextLength = 3

selectText(textarea.selectionEnd, textarea.selectionEnd + urlTextLength)
}
} catch (err) {