refactor: add option to disable context menu (#3717)

This commit is contained in:
Nivedin
2024-01-23 22:05:05 +05:30
committed by GitHub
parent c0dbcc901f
commit aca96dd5f2
4 changed files with 32 additions and 7 deletions

View File

@@ -99,6 +99,7 @@ useCodemirror(
linter, linter,
completer, completer,
environmentHighlights: false, environmentHighlights: false,
contextMenuEnabled: false,
}) })
) )

View File

@@ -95,6 +95,7 @@ useCodemirror(
linter, linter,
completer, completer,
environmentHighlights: false, environmentHighlights: false,
contextMenuEnabled: false,
}) })
) )

View File

@@ -79,6 +79,7 @@ const props = withDefaults(
readonly?: boolean readonly?: boolean
autoCompleteSource?: string[] autoCompleteSource?: string[]
inspectionResults?: InspectorResult[] | undefined inspectionResults?: InspectorResult[] | undefined
contextMenuEnabled?: boolean
}>(), }>(),
{ {
modelValue: "", modelValue: "",
@@ -91,6 +92,7 @@ const props = withDefaults(
autoCompleteSource: undefined, autoCompleteSource: undefined,
inspectionResult: undefined, inspectionResult: undefined,
inspectionResults: undefined, inspectionResults: undefined,
contextMenuEnabled: true,
} }
) )
@@ -359,8 +361,11 @@ const initView = (el: any) => {
handleTextSelection() handleTextSelection()
}, 140) }, 140)
el.addEventListener("mouseup", debounceFn) // Only add event listeners if context menu is enabled in the component
el.addEventListener("keyup", debounceFn) if (props.contextMenuEnabled) {
el.addEventListener("mouseup", debounceFn)
el.addEventListener("keyup", debounceFn)
}
const extensions: Extension = [ const extensions: Extension = [
EditorView.contentAttributes.of({ "aria-label": props.placeholder }), EditorView.contentAttributes.of({ "aria-label": props.placeholder }),
@@ -396,7 +401,7 @@ const initView = (el: any) => {
ev.preventDefault() ev.preventDefault()
}, },
scroll(event) { scroll(event) {
if (event.target) { if (event.target && props.contextMenuEnabled) {
handleTextSelection() handleTextSelection()
} }
}, },
@@ -405,7 +410,6 @@ const initView = (el: any) => {
class { class {
update(update: ViewUpdate) { update(update: ViewUpdate) {
if (props.readonly) return if (props.readonly) return
if (update.docChanged) { if (update.docChanged) {
const prevValue = clone(cachedValue.value) const prevValue = clone(cachedValue.value)
@@ -436,6 +440,17 @@ const initView = (el: any) => {
clipboardEv = null clipboardEv = null
pastedValue = null pastedValue = null
} }
if (props.contextMenuEnabled) {
// close the context menu if text is being updated in the editor
invokeAction("contextmenu.open", {
position: {
top: 0,
left: 0,
},
text: null,
})
}
} }
} }
} }

View File

@@ -63,6 +63,8 @@ type CodeMirrorOptions = {
additionalExts?: Extension[] additionalExts?: Extension[]
contextMenuEnabled?: boolean
// callback on editor update // callback on editor update
onUpdate?: (view: ViewUpdate) => void onUpdate?: (view: ViewUpdate) => void
} }
@@ -208,6 +210,9 @@ export function useCodemirror(
): { cursor: Ref<{ line: number; ch: number }> } { ): { cursor: Ref<{ line: number; ch: number }> } {
const { subscribeToStream } = useStreamSubscriber() const { subscribeToStream } = useStreamSubscriber()
// Set default value for contextMenuEnabled if not provided
options.contextMenuEnabled = options.contextMenuEnabled ?? true
const additionalExts = new Compartment() const additionalExts = new Compartment()
const language = new Compartment() const language = new Compartment()
const lineWrapping = new Compartment() const lineWrapping = new Compartment()
@@ -272,8 +277,11 @@ export function useCodemirror(
handleTextSelection() handleTextSelection()
}, 140) }, 140)
el.addEventListener("mouseup", debounceFn) // Only add event listeners if context menu is enabled in the editor
el.addEventListener("keyup", debounceFn) if (options.contextMenuEnabled) {
el.addEventListener("mouseup", debounceFn)
el.addEventListener("keyup", debounceFn)
}
if (options.onUpdate) { if (options.onUpdate) {
options.onUpdate(update) options.onUpdate(update)
@@ -312,7 +320,7 @@ export function useCodemirror(
), ),
EditorView.domEventHandlers({ EditorView.domEventHandlers({
scroll(event) { scroll(event) {
if (event.target) { if (event.target && options.contextMenuEnabled) {
handleTextSelection() handleTextSelection()
} }
}, },