feat: implement paste handling and value sync on new env input

This commit is contained in:
Andrew Bastin
2022-02-07 04:08:19 +05:30
parent 4da3410963
commit 6d394b6e0e
2 changed files with 54 additions and 13 deletions

View File

@@ -361,20 +361,15 @@ const ensureMethodInEndpoint = () => {
} }
} }
const onPasteUrl = (e: { event: ClipboardEvent; previousValue: string }) => { const onPasteUrl = (e: { pastedValue: string; prevValue: string }) => {
if (!e) return if (!e) return
const clipboardData = e.event.clipboardData const pastedData = e.pastedValue
const pastedData = clipboardData?.getData("Text")
if (!pastedData) return
if (isCURL(pastedData)) { if (isCURL(pastedData)) {
e.event.preventDefault()
showCurlImportModal.value = true showCurlImportModal.value = true
curlText.value = pastedData curlText.value = pastedData
newEndpoint.value = e.previousValue newEndpoint.value = e.prevValue
} }
} }

View File

@@ -19,9 +19,15 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, watch } from "@nuxtjs/composition-api" import { ref, onMounted, watch, nextTick } from "@nuxtjs/composition-api"
import { EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view" import {
EditorView,
placeholder as placeholderExt,
ViewPlugin,
ViewUpdate,
} from "@codemirror/view"
import { EditorState, Extension } from "@codemirror/state" import { EditorState, Extension } from "@codemirror/state"
import clone from "lodash/clone"
import { baseTheme } from "~/helpers/editor/themes/baseTheme" import { baseTheme } from "~/helpers/editor/themes/baseTheme"
import { HoppEnvironmentPlugin } from "~/helpers/editor/extensions/HoppEnvironment" import { HoppEnvironmentPlugin } from "~/helpers/editor/extensions/HoppEnvironment"
import { useStreamSubscriber } from "~/helpers/utils/composables" import { useStreamSubscriber } from "~/helpers/utils/composables"
@@ -41,18 +47,41 @@ const props = withDefaults(
const emit = defineEmits<{ const emit = defineEmits<{
(e: "input", data: string): void (e: "input", data: string): void
(e: "change", data: string): void
(e: "paste", data: { prevValue: string; pastedValue: string }): void
(e: "enter", ev: any): void (e: "enter", ev: any): void
(e: "keyup", ev: any): void (e: "keyup", ev: any): void
(e: "keydown", ev: any): void (e: "keydown", ev: any): void
(e: "click", ev: any): void (e: "click", ev: any): void
}>() }>()
const editor = ref<any | null>(null)
const cachedValue = ref(props.value) const cachedValue = ref(props.value)
const view = ref<EditorView>() const view = ref<EditorView>()
const editor = ref<any | null>(null)
watch(
() => props.value,
(newVal) => {
if (cachedValue.value !== newVal) {
cachedValue.value = newVal
view.value?.dispatch({
filter: false,
changes: {
from: 0,
to: view.value.state.doc.length,
insert: newVal,
},
})
}
},
{
immediate: true,
}
)
const { subscribeToStream } = useStreamSubscriber() const { subscribeToStream } = useStreamSubscriber()
const envTooltipPlugin = new HoppEnvironmentPlugin(subscribeToStream, view) const envTooltipPlugin = new HoppEnvironmentPlugin(subscribeToStream, view)
@@ -61,15 +90,32 @@ const initView = (el: any) => {
const extensions: Extension = [ const extensions: Extension = [
baseTheme, baseTheme,
envTooltipPlugin, envTooltipPlugin,
placeholderExt(props.placeholder),
ViewPlugin.fromClass( ViewPlugin.fromClass(
class { class {
update(update: ViewUpdate) { update(update: ViewUpdate) {
const pasted = !!update.transactions.find((txn) =>
txn.isUserEvent("input.paste")
)
if (update.docChanged) { if (update.docChanged) {
const prevValue = clone(cachedValue.value)
cachedValue.value = update.state.doc cachedValue.value = update.state.doc
.toJSON() .toJSON()
.join(update.state.lineBreak) .join(update.state.lineBreak)
emit("input", cachedValue.value) const value = clone(cachedValue.value)
emit("input", value)
emit("change", value)
if (pasted) {
nextTick().then(() => {
emit("paste", {
pastedValue: value,
prevValue,
})
})
}
} }
} }
} }