2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
2024-02-13 15:59:27 +00:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-11-25 12:31:34 +00:00
|
|
|
<template>
|
2023-05-27 02:35:26 +00:00
|
|
|
<div>
|
|
|
|
<div :class="$style.label" @click="focus"><slot name="label"></slot></div>
|
|
|
|
<div :class="{ [$style.disabled]: disabled, [$style.focused]: focused, [$style.tall]: tall, [$style.pre]: pre }" style="position: relative;">
|
2022-07-20 13:24:26 +00:00
|
|
|
<textarea
|
|
|
|
ref="inputEl"
|
2021-09-29 15:50:45 +00:00
|
|
|
v-model="v"
|
2021-12-25 16:42:50 +00:00
|
|
|
v-adaptive-border
|
2023-06-01 08:19:46 +00:00
|
|
|
:class="[$style.textarea, { _monospace: code }]"
|
2021-09-29 15:50:45 +00:00
|
|
|
:disabled="disabled"
|
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:pattern="pattern"
|
2024-01-30 10:53:53 +00:00
|
|
|
:autocomplete="autocomplete"
|
2021-09-29 15:50:45 +00:00
|
|
|
:spellcheck="spellcheck"
|
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
|
|
|
@keydown="onKeydown($event)"
|
|
|
|
@input="onInput"
|
|
|
|
></textarea>
|
2020-11-25 12:31:34 +00:00
|
|
|
</div>
|
2023-05-27 02:35:26 +00:00
|
|
|
<div :class="$style.caption"><slot name="caption"></slot></div>
|
2023-12-15 08:57:31 +00:00
|
|
|
<button v-if="mfmPreview" style="font-size: 0.85em;" class="_textButton" type="button" @click="preview = !preview">{{ i18n.ts.preview }}</button>
|
|
|
|
<div v-if="mfmPreview" v-show="preview" v-panel :class="$style.mfmPreview">
|
2023-12-14 04:11:23 +00:00
|
|
|
<Mfm :text="v"/>
|
|
|
|
</div>
|
2021-02-13 03:28:26 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
<MkButton v-if="manualSave && changed" primary :class="$style.save" @click="updated"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
|
2021-09-29 15:50:45 +00:00
|
|
|
</div>
|
2020-11-25 12:31:34 +00:00
|
|
|
</template>
|
|
|
|
|
2023-05-14 02:43:56 +00:00
|
|
|
<script lang="ts" setup>
|
2023-12-14 04:11:23 +00:00
|
|
|
import { onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs, shallowRef } from 'vue';
|
2021-09-29 15:50:45 +00:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { i18n } from '@/i18n.js';
|
2023-12-14 04:11:23 +00:00
|
|
|
import { Autocomplete, SuggestionType } from '@/scripts/autocomplete.js';
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-05-14 02:43:56 +00:00
|
|
|
const props = defineProps<{
|
|
|
|
modelValue: string | null;
|
|
|
|
required?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
pattern?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
autofocus?: boolean;
|
|
|
|
autocomplete?: string;
|
2023-12-14 04:11:23 +00:00
|
|
|
mfmAutocomplete?: boolean | SuggestionType[],
|
|
|
|
mfmPreview?: boolean;
|
2023-05-14 02:43:56 +00:00
|
|
|
spellcheck?: boolean;
|
|
|
|
debounce?: boolean;
|
|
|
|
manualSave?: boolean;
|
|
|
|
code?: boolean;
|
|
|
|
tall?: boolean;
|
|
|
|
pre?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'change', _ev: KeyboardEvent): void;
|
|
|
|
(ev: 'keydown', _ev: KeyboardEvent): void;
|
|
|
|
(ev: 'enter'): void;
|
|
|
|
(ev: 'update:modelValue', value: string): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const { modelValue, autofocus } = toRefs(props);
|
|
|
|
const v = ref<string>(modelValue.value ?? '');
|
|
|
|
const focused = ref(false);
|
|
|
|
const changed = ref(false);
|
|
|
|
const invalid = ref(false);
|
|
|
|
const filled = computed(() => v.value !== '' && v.value != null);
|
|
|
|
const inputEl = shallowRef<HTMLTextAreaElement>();
|
2023-12-14 04:11:23 +00:00
|
|
|
const preview = ref(false);
|
2024-01-30 10:53:53 +00:00
|
|
|
let autocompleteWorker: Autocomplete | null = null;
|
2023-05-14 02:43:56 +00:00
|
|
|
|
2024-01-30 10:53:53 +00:00
|
|
|
const focus = () => inputEl.value?.focus();
|
2023-05-14 02:43:56 +00:00
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
emit('change', ev);
|
|
|
|
};
|
|
|
|
const onKeydown = (ev: KeyboardEvent) => {
|
|
|
|
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
|
|
|
|
|
|
|
|
emit('keydown', ev);
|
|
|
|
|
|
|
|
if (ev.code === 'Enter') {
|
|
|
|
emit('enter');
|
|
|
|
}
|
2023-12-15 08:18:31 +00:00
|
|
|
|
|
|
|
if (props.code && ev.key === 'Tab') {
|
|
|
|
const pos = inputEl.value?.selectionStart ?? 0;
|
|
|
|
const posEnd = inputEl.value?.selectionEnd ?? v.value.length;
|
|
|
|
v.value = v.value.slice(0, pos) + '\t' + v.value.slice(posEnd);
|
|
|
|
nextTick(() => {
|
|
|
|
inputEl.value?.setSelectionRange(pos + 1, pos + 1);
|
|
|
|
});
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
2023-05-14 02:43:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
emit('update:modelValue', v.value ?? '');
|
|
|
|
};
|
|
|
|
|
|
|
|
const debouncedUpdated = debounce(1000, updated);
|
|
|
|
|
|
|
|
watch(modelValue, newValue => {
|
2024-01-30 10:53:53 +00:00
|
|
|
v.value = newValue ?? '';
|
2023-05-14 02:43:56 +00:00
|
|
|
});
|
|
|
|
|
2024-01-30 10:53:53 +00:00
|
|
|
watch(v, () => {
|
2023-05-14 02:43:56 +00:00
|
|
|
if (!props.manualSave) {
|
|
|
|
if (props.debounce) {
|
|
|
|
debouncedUpdated();
|
|
|
|
} else {
|
|
|
|
updated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-30 10:53:53 +00:00
|
|
|
invalid.value = inputEl.value?.validity.badInput ?? true;
|
2023-05-14 02:43:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
});
|
2023-12-14 04:11:23 +00:00
|
|
|
|
2024-01-30 10:53:53 +00:00
|
|
|
if (props.mfmAutocomplete && inputEl.value) {
|
|
|
|
autocompleteWorker = new Autocomplete(inputEl.value, v, props.mfmAutocomplete === true ? undefined : props.mfmAutocomplete);
|
2023-12-14 04:11:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
2024-01-30 10:53:53 +00:00
|
|
|
if (autocompleteWorker) {
|
|
|
|
autocompleteWorker.detach();
|
2023-12-14 04:11:23 +00:00
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.label {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 0 0 8px 0;
|
|
|
|
user-select: none;
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2021-09-29 15:50:45 +00:00
|
|
|
}
|
2023-05-27 02:35:26 +00:00
|
|
|
}
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.caption {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 8px 0 0 0;
|
2024-10-09 09:08:14 +00:00
|
|
|
color: var(--MI_THEME-fgTransparentWeak);
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2021-09-29 15:50:45 +00:00
|
|
|
}
|
2023-05-27 02:35:26 +00:00
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.textarea {
|
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
display: block;
|
|
|
|
width: 100%;
|
|
|
|
min-width: 100%;
|
|
|
|
max-width: 100%;
|
|
|
|
min-height: 130px;
|
|
|
|
margin: 0;
|
|
|
|
padding: 12px;
|
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
|
|
|
font-size: 1em;
|
2024-10-09 09:08:14 +00:00
|
|
|
color: var(--MI_THEME-fg);
|
|
|
|
background: var(--MI_THEME-panel);
|
|
|
|
border: solid 1px var(--MI_THEME-panel);
|
2023-05-27 02:35:26 +00:00
|
|
|
border-radius: 6px;
|
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
|
|
|
transition: border-color 0.1s ease-out;
|
|
|
|
|
|
|
|
&:hover {
|
2024-10-09 09:08:14 +00:00
|
|
|
border-color: var(--MI_THEME-inputBorderHover) !important;
|
2023-05-27 02:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.focused {
|
|
|
|
> .textarea {
|
2024-10-09 09:08:14 +00:00
|
|
|
border-color: var(--MI_THEME-accent) !important;
|
2023-05-27 02:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.disabled {
|
|
|
|
opacity: 0.7;
|
|
|
|
cursor: not-allowed !important;
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
> .textarea {
|
|
|
|
cursor: not-allowed !important;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.tall {
|
|
|
|
> .textarea {
|
|
|
|
min-height: 200px;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
2023-05-27 02:35:26 +00:00
|
|
|
}
|
2021-11-28 11:07:37 +00:00
|
|
|
|
2023-05-27 02:35:26 +00:00
|
|
|
.pre {
|
|
|
|
> .textarea {
|
|
|
|
white-space: pre;
|
2021-11-28 11:07:37 +00:00
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
2023-05-27 02:35:26 +00:00
|
|
|
|
|
|
|
.save {
|
|
|
|
margin: 8px 0 0 0;
|
|
|
|
}
|
2023-12-14 04:11:23 +00:00
|
|
|
|
|
|
|
.mfmPreview {
|
|
|
|
padding: 12px;
|
|
|
|
border-radius: var(--radius);
|
|
|
|
box-sizing: border-box;
|
|
|
|
min-height: 130px;
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
</style>
|