-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Selection formatting toolbar (#10027)
* Selection formatting toolbar * Icons * Review * Fix bold+italic rendering * Preparing for top bar * Refactor
- Loading branch information
Showing
12 changed files
with
261 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,37 @@ | ||
<script setup lang="ts"> | ||
import { useSelectionBounds } from '@/composables/domSelection' | ||
import { flip, offset, useFloating } from '@floating-ui/vue' | ||
import type { MaybeElement } from '@vueuse/core' | ||
import { computed, shallowRef, toRef } from 'vue' | ||
const props = defineProps<{ selectionElement: MaybeElement }>() | ||
const rootElement = shallowRef<HTMLElement>() | ||
const { bounds: selectionBounds } = useSelectionBounds(toRef(props, 'selectionElement')) | ||
const virtualElement = computed(() => { | ||
const rect = selectionBounds.value?.toDomRect() | ||
return rect ? { getBoundingClientRect: () => rect } : undefined | ||
}) | ||
const { floatingStyles } = useFloating(virtualElement, rootElement, { | ||
placement: 'top-start', | ||
middleware: [ | ||
offset({ | ||
mainAxis: 4, | ||
alignmentAxis: -2, | ||
}), | ||
flip(), | ||
], | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
v-if="selectionBounds" | ||
ref="rootElement" | ||
:style="floatingStyles" | ||
class="FloatingSelectionMenu" | ||
> | ||
<slot /> | ||
</div> | ||
</template> |
This file contains 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
<template> | ||
<div ref="lexicalElement" class="lexical" spellcheck="false" contenteditable="true" /> | ||
<div ref="lexicalElement" class="LexicalContent" spellcheck="false" contenteditable="true" /> | ||
</template> | ||
|
||
<style scoped> | ||
.lexical { | ||
.LexicalContent { | ||
outline-style: none; | ||
} | ||
</style> |
This file contains 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
26 changes: 26 additions & 0 deletions
26
app/gui2/src/components/lexical/SelectionFormattingToolbar.vue
This file contains 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,26 @@ | ||
<script setup lang="ts"> | ||
import ToggleIcon from '@/components/ToggleIcon.vue' | ||
import { type UseFormatting } from '@/components/lexical/formatting' | ||
const props = defineProps<{ formatting: UseFormatting }>() | ||
const { bold, italic, strikethrough } = props.formatting | ||
</script> | ||
|
||
<template> | ||
<div class="SelectionFormattingToolbar"> | ||
<ToggleIcon v-model="bold" icon="bold" title="Bold" /> | ||
<ToggleIcon v-model="italic" icon="italic" title="Italic" /> | ||
<ToggleIcon v-model="strikethrough" icon="strike-through" title="Strikethrough" /> | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
.SelectionFormattingToolbar { | ||
display: flex; | ||
background-color: white; | ||
border-radius: var(--radius-full); | ||
padding: 4px; | ||
gap: 4px; | ||
} | ||
</style> |
This file contains 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,58 @@ | ||
import type { LexicalEditor, RangeSelection, TextFormatType } from 'lexical' | ||
import { | ||
$getSelection, | ||
$isRangeSelection, | ||
COMMAND_PRIORITY_LOW, | ||
FORMAT_TEXT_COMMAND, | ||
SELECTION_CHANGE_COMMAND, | ||
} from 'lexical' | ||
import { computed, ref } from 'vue' | ||
|
||
export function useFormatting(editor: LexicalEditor) { | ||
const selectionReaders = new Array<(selection: RangeSelection) => void>() | ||
function onReadSelection(reader: (selection: RangeSelection) => void) { | ||
selectionReaders.push(reader) | ||
} | ||
function $readState() { | ||
const selection = $getSelection() | ||
if ($isRangeSelection(selection)) { | ||
for (const reader of selectionReaders) { | ||
reader(selection) | ||
} | ||
} | ||
} | ||
editor.registerUpdateListener(({ editorState }) => { | ||
editorState.read($readState) | ||
}) | ||
editor.registerCommand( | ||
SELECTION_CHANGE_COMMAND, | ||
(_payload, _newEditor) => { | ||
$readState() | ||
return false | ||
}, | ||
COMMAND_PRIORITY_LOW, | ||
) | ||
return { | ||
bold: useFormatProperty(editor, 'bold', onReadSelection), | ||
italic: useFormatProperty(editor, 'italic', onReadSelection), | ||
strikethrough: useFormatProperty(editor, 'strikethrough', onReadSelection), | ||
} | ||
} | ||
export type UseFormatting = ReturnType<typeof useFormatting> | ||
|
||
function useFormatProperty( | ||
editor: LexicalEditor, | ||
property: TextFormatType, | ||
onReadSelection: ($readSelection: (selection: RangeSelection) => void) => void, | ||
) { | ||
const state = ref(false) | ||
|
||
onReadSelection((selection) => (state.value = selection.hasFormat(property))) | ||
|
||
return computed({ | ||
get: () => state.value, | ||
set: (value) => { | ||
if (value !== state.value) editor.dispatchCommand(FORMAT_TEXT_COMMAND, property) | ||
}, | ||
}) | ||
} |
This file contains 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
Oops, something went wrong.