Skip to content

Commit 5641816

Browse files
committed
feat(c-input-text): handle paste htm
Add paste-html prop to allow pasting html directly
1 parent a07806c commit 5641816

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/ui/c-input-text/c-input-text.vue

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const props = withDefaults(
3131
autosize?: boolean
3232
autofocus?: boolean
3333
monospace?: boolean
34+
pasteHtml?: boolean
3435
}>(),
3536
{
3637
value: '',
@@ -58,13 +59,14 @@ const props = withDefaults(
5859
autosize: false,
5960
autofocus: false,
6061
monospace: false,
62+
pasteHtml: false,
6163
},
6264
);
6365
const emit = defineEmits(['update:value']);
6466
const value = useVModel(props, 'value', emit);
6567
const showPassword = ref(false);
6668
67-
const { id, placeholder, label, validationRules, labelPosition, labelWidth, labelAlign, autosize, readonly, disabled, clearable, type, multiline, rows, rawText, autofocus, monospace } = toRefs(props);
69+
const { id, placeholder, label, validationRules, labelPosition, labelWidth, labelAlign, autosize, readonly, disabled, clearable, type, multiline, rows, rawText, autofocus, monospace, pasteHtml } = toRefs(props);
6870
6971
const validation
7072
= props.validation
@@ -81,6 +83,28 @@ const textareaRef = ref<HTMLTextAreaElement>();
8183
const inputRef = ref<HTMLInputElement>();
8284
const inputWrapperRef = ref<HTMLElement>();
8385
86+
interface HTMLElementWithValue {
87+
value?: string
88+
}
89+
90+
function onPasteInputHtml(evt: ClipboardEvent) {
91+
if (!pasteHtml.value) {
92+
return false;
93+
}
94+
95+
const target = (evt.target as HTMLElementWithValue);
96+
if (!target) {
97+
return false;
98+
}
99+
evt.preventDefault();
100+
const textHtmlData = evt.clipboardData?.getData('text/html');
101+
if (textHtmlData && textHtmlData !== '') {
102+
value.value = textHtmlData;
103+
return true;
104+
}
105+
return false;
106+
}
107+
84108
watch(
85109
[value, autosize, multiline, inputWrapperRef, textareaRef],
86110
() => nextTick(() => {
@@ -171,6 +195,7 @@ defineExpose({
171195
:autocorrect="autocorrect ?? (rawText ? 'off' : undefined)"
172196
:spellcheck="spellcheck ?? (rawText ? false : undefined)"
173197
:rows="rows"
198+
@paste="onPasteInputHtml"
174199
/>
175200

176201
<input
@@ -192,6 +217,7 @@ defineExpose({
192217
:autocomplete="autocomplete ?? (rawText ? 'off' : undefined)"
193218
:autocorrect="autocorrect ?? (rawText ? 'off' : undefined)"
194219
:spellcheck="spellcheck ?? (rawText ? false : undefined)"
220+
@paste="onPasteInputHtml"
195221
>
196222

197223
<c-button v-if="clearable && value" variant="text" circle size="small" @click="value = ''">

0 commit comments

Comments
 (0)