-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add top panel #3
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
Merged
SerVitasik
merged 14 commits into
main
from
feature/AdminForth/1263/add-top-panel-with-buttons-of-
Mar 19, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
58dead0
fix: add top panel
b6725ad
fix: resolve copilot comment
6558f04
fix: resolve copilot comment
1d1807c
fix: change list logic
f69b76a
fix: change link and code block logic
7282a7b
fix: resolve copilot comment
9303ab1
fix: add logic for list
fe09bd2
fix: add variable btns
f67d92b
fix: resolve copilot comment
ae59004
fix: update spelling
cd1b435
fix: separate btns logic
cc3a9a8
fix: delete unused imports
3de45ba
chore: delete spelling
1553e5e
chore: delete spelling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| <script setup lang="ts"> | ||
| import { markRaw } from 'vue'; | ||
| import * as monaco from 'monaco-editor'; | ||
| import { toggleWrapSmart } from './utils/monacoMarkdownToggle'; | ||
|
|
||
| import { | ||
| IconLinkOutline, IconCodeOutline, IconRectangleListOutline, | ||
| IconOrderedListOutline, IconLetterBoldOutline, IconLetterUnderlineOutline, | ||
| IconLetterItalicOutline, IconTextSlashOutline | ||
| } from '@iconify-prerendered/vue-flowbite'; | ||
| import { IconH116Solid, IconH216Solid, IconH316Solid } from '@iconify-prerendered/vue-heroicons'; | ||
|
|
||
| const props = defineProps<{ | ||
| editor: monaco.editor.IStandaloneCodeEditor | null; | ||
| meta: any; | ||
| }>(); | ||
|
|
||
| const isBtnVisible = (btnKey: string) => { | ||
| const settings = props.meta?.topPanelSettings; | ||
| if (!settings || Object.keys(settings).length === 0) return true; | ||
| return settings[btnKey] !== undefined ? settings[btnKey] : true; | ||
| }; | ||
|
|
||
| const btnClass = 'flex items-center justify-center h-8 px-1 rounded hover:bg-gray-200 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-200 transition-colors duration-200'; | ||
|
|
||
| const fenceForCodeBlock = (text: string): string => { | ||
| let maxBackticks = 0; | ||
| let current = 0; | ||
| for (let i = 0; i < text.length; i++) { | ||
| if (text[i] === '`') { current++; if (current > maxBackticks) maxBackticks = current; } | ||
| else { current = 0; } | ||
| } | ||
| return '`'.repeat(Math.max(3, maxBackticks + 1)); | ||
| }; | ||
|
|
||
| const applyFormat = (type: string) => { | ||
| const editor = props.editor; | ||
| if (!editor) return; | ||
|
|
||
| const model = editor.getModel(); | ||
| if (!model) return; | ||
|
|
||
| editor.focus(); | ||
| const rawSelection = editor.getSelection(); | ||
| if (!rawSelection) return; | ||
|
|
||
| const selection = rawSelection.startLineNumber !== rawSelection.endLineNumber && rawSelection.endColumn === 1 | ||
| ? new monaco.Selection(rawSelection.startLineNumber, rawSelection.startColumn, rawSelection.endLineNumber - 1, model.getLineMaxColumn(rawSelection.endLineNumber - 1)) | ||
| : rawSelection; | ||
|
|
||
| const selectedText = model.getValueInRange(selection); | ||
|
|
||
| const applyEdits = (id: string, edits: monaco.editor.IIdentifiedSingleEditOperation[]) => { | ||
| editor.executeEdits(id, edits); | ||
| }; | ||
|
|
||
| switch (type) { | ||
| case 'bold': toggleWrapSmart(editor, '**'); break; | ||
| case 'italic': toggleWrapSmart(editor, '*'); break; | ||
| case 'strike': toggleWrapSmart(editor, '~~'); break; | ||
| case 'underline': toggleWrapSmart(editor, '<u>', '</u>'); break; | ||
| case 'codeBlock': { | ||
| const trimmed = selectedText.trim(); | ||
| const match = trimmed.match(/^(`{3,})[^\n]*\n([\s\S]*)\n\1$/); | ||
| if (match) { | ||
| applyEdits('unwrap-code', [{ range: selection, text: match[2], forceMoveMarkers: true }]); | ||
| } else { | ||
| const fence = fenceForCodeBlock(selectedText); | ||
| applyEdits('wrap-code', [{ range: selection, text: `\n${fence}\n${selectedText}\n${fence}\n`, forceMoveMarkers: true }]); | ||
| } | ||
| break; | ||
| } | ||
| case 'link': { | ||
| const match = selectedText.trim().match(/^\[(.*?)\]\(.*?\)$/); | ||
| if (match) { | ||
| applyEdits('unlink', [{ range: selection, text: match[1], forceMoveMarkers: true }]); | ||
| } else { | ||
| applyEdits('insert-link', [{ range: selection, text: `[${selectedText}](url)`, forceMoveMarkers: true }]); | ||
| } | ||
| break; | ||
| } | ||
| case 'h1': case 'h2': case 'h3': case 'ul': case 'ol': { | ||
| const prefixMap: any = { h1: '# ', h2: '## ', h3: '### ', ul: '* ' }; | ||
| const edits: any[] = []; | ||
| for (let i = selection.startLineNumber; i <= selection.endLineNumber; i++) { | ||
| const line = model.getLineContent(i); | ||
| const targetPrefix = type === 'ol' ? `${i - selection.startLineNumber + 1}. ` : prefixMap[type]; | ||
| const match = line.match(/^(#{1,6}\s+|[*+-]\s+|\d+[.)]\s+)/); | ||
| if (match) { | ||
| edits.push({ range: new monaco.Range(i, 1, i, match[0].length + 1), text: match[0].trim() === targetPrefix.trim() ? '' : targetPrefix }); | ||
| } else { | ||
| edits.push({ range: new monaco.Range(i, 1, i, 1), text: targetPrefix }); | ||
| } | ||
| } | ||
| applyEdits('format-block', edits); | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const buttons = [ | ||
| { id: 'bold', title: 'Bold', icon: markRaw(IconLetterBoldOutline), group: 1 }, | ||
| { id: 'italic', title: 'Italic', icon: markRaw(IconLetterItalicOutline), group: 1 }, | ||
| { id: 'underline', title: 'Underline', icon: markRaw(IconLetterUnderlineOutline), group: 1 }, | ||
| { id: 'strike', title: 'Strike', icon: markRaw(IconTextSlashOutline), group: 1, separator: true }, | ||
| { id: 'h1', title: 'H1', icon: markRaw(IconH116Solid), group: 2 }, | ||
| { id: 'h2', title: 'H2', icon: markRaw(IconH216Solid), group: 2 }, | ||
| { id: 'h3', title: 'H3', icon: markRaw(IconH316Solid), group: 2, separator: true }, | ||
| { id: 'ul', title: 'UL', icon: markRaw(IconRectangleListOutline), group: 3 }, | ||
| { id: 'ol', title: 'OL', icon: markRaw(IconOrderedListOutline), group: 3 }, | ||
| { id: 'link', title: 'Link', icon: markRaw(IconLinkOutline), group: 3 }, | ||
| { id: 'codeBlock', title: 'Code', icon: markRaw(IconCodeOutline), group: 3 }, | ||
| ]; | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="flex flex-wrap items-center gap-3 p-1.5 border border-gray-300 dark:border-gray-600 rounded-t-lg bg-gray-50 dark:bg-gray-800 w-full box-border"> | ||
| <template v-for="btn in buttons" :key="btn.id"> | ||
| <button | ||
| v-if="isBtnVisible(btn.id)" | ||
| type="button" | ||
| @click="applyFormat(btn.id)" | ||
| :class="btnClass" | ||
| :title="btn.title" | ||
| > | ||
| <component :is="btn.icon" class="w-5 h-5" /> | ||
| </button> | ||
| <div v-if="btn.separator && isBtnVisible(btn.id)" class="w-px h-4 bg-gray-300 dark:bg-gray-600 mx-1"></div> | ||
| </template> | ||
| </div> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.