feat: hover placeholder transition for smartenvinput

This commit is contained in:
nivedin
2024-04-24 14:37:13 +05:30
parent 844eee0fa4
commit cf37fbd610

View File

@@ -73,7 +73,12 @@ import {
keymap, keymap,
tooltips, tooltips,
} from "@codemirror/view" } from "@codemirror/view"
import { EditorSelection, EditorState, Extension } from "@codemirror/state" import {
Compartment,
EditorSelection,
EditorState,
Extension,
} from "@codemirror/state"
import { clone } from "lodash-es" import { clone } from "lodash-es"
import { history, historyKeymap } from "@codemirror/commands" import { history, historyKeymap } from "@codemirror/commands"
import { inputTheme } from "~/helpers/editor/themes/baseTheme" import { inputTheme } from "~/helpers/editor/themes/baseTheme"
@@ -109,6 +114,7 @@ const props = withDefaults(
contextMenuEnabled?: boolean contextMenuEnabled?: boolean
secret?: boolean secret?: boolean
autoCompleteEnv?: boolean autoCompleteEnv?: boolean
placeholderHoverString: string
}>(), }>(),
{ {
modelValue: "", modelValue: "",
@@ -124,6 +130,7 @@ const props = withDefaults(
contextMenuEnabled: true, contextMenuEnabled: true,
secret: false, secret: false,
autoCompleteEnvSource: false, autoCompleteEnvSource: false,
placeholderHoverString: "",
} }
) )
@@ -137,6 +144,8 @@ const emit = defineEmits<{
(e: "click", ev: any): void (e: "click", ev: any): void
}>() }>()
const placeholderString = ref(props.placeholder)
const cachedValue = ref(props.modelValue) const cachedValue = ref(props.modelValue)
const view = ref<EditorView>() const view = ref<EditorView>()
@@ -441,6 +450,8 @@ function handleTextSelection() {
} }
} }
const placeholderCompt = new Compartment()
// Debounce to prevent double click from selecting the word // Debounce to prevent double click from selecting the word
const debouncedTextSelection = (time: number) => const debouncedTextSelection = (time: number) =>
useDebounceFn(() => { useDebounceFn(() => {
@@ -462,6 +473,21 @@ const initView = (el: any) => {
extensions, extensions,
}), }),
}) })
if (props.placeholderHoverString) {
if (props.modelValue === props.placeholder) {
// clear input
view.value?.dispatch({
changes: {
from: 0,
to: view.value.state.doc.length,
insert: "",
},
})
}
placeholderCompt.reconfigure(placeholderExt(placeholderString.value))
}
} }
const getExtensions = (readonly: boolean): Extension => { const getExtensions = (readonly: boolean): Extension => {
@@ -490,7 +516,8 @@ const getExtensions = (readonly: boolean): Extension => {
position: "absolute", position: "absolute",
}), }),
props.environmentHighlights ? envTooltipPlugin : [], props.environmentHighlights ? envTooltipPlugin : [],
placeholderExt(props.placeholder), placeholderCompt.of(placeholderExt(props.placeholder)),
EditorView.domEventHandlers({ EditorView.domEventHandlers({
paste(ev) { paste(ev) {
clipboardEv = ev clipboardEv = ev
@@ -505,6 +532,27 @@ const getExtensions = (readonly: boolean): Extension => {
debouncedTextSelection(30)() debouncedTextSelection(30)()
} }
}, },
mouseenter() {
//change placeholder to hover string if provided
if (props.placeholderHoverString) {
placeholderString.value = props.placeholderHoverString
view.value?.dispatch({
effects: placeholderCompt.reconfigure(
placeholderExt(props.placeholderHoverString)
),
})
}
},
mouseleave() {
//change placeholder back to original string
if (props.placeholderHoverString) {
view.value?.dispatch({
effects: placeholderCompt.reconfigure(
placeholderExt(props.placeholder)
),
})
}
},
}), }),
props.autoCompleteEnv props.autoCompleteEnv
? autocompletion({ ? autocompletion({