mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 05:51:44 +03:00
1 parent
66e2ac5df2
commit
be4d22587f
1 file changed
+60
-55
@@ -5,14 +5,8 @@ import { useCopy } from '@/composable/copy';
|
|||||||
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
|
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
|
||||||
import { translate as t } from '@/plugins/i18n.plugin';
|
import { translate as t } from '@/plugins/i18n.plugin';
|
||||||
|
|
||||||
import {
|
import { EditorView, basicSetup } from 'codemirror';
|
||||||
EditorView,
|
import { Compartment, EditorState } from '@codemirror/state';
|
||||||
basicSetup,
|
|
||||||
} from 'codemirror';
|
|
||||||
import {
|
|
||||||
Compartment,
|
|
||||||
EditorState,
|
|
||||||
} from '@codemirror/state';
|
|
||||||
|
|
||||||
import { javascript } from '@codemirror/lang-javascript';
|
import { javascript } from '@codemirror/lang-javascript';
|
||||||
import { html } from '@codemirror/lang-html';
|
import { html } from '@codemirror/lang-html';
|
||||||
@@ -34,16 +28,15 @@ import { useStyleStore } from '@/stores/style.store';
|
|||||||
|
|
||||||
const props = withDefaults(
|
const props = withDefaults(
|
||||||
defineProps<{
|
defineProps<{
|
||||||
value: string
|
value: string;
|
||||||
language?: string
|
language?: string;
|
||||||
copyPlacement?: 'top-right' | 'bottom-right' | 'outside' | 'none'
|
copyPlacement?: 'top-right' | 'bottom-right' | 'outside' | 'none';
|
||||||
copyMessage?: string
|
copyMessage?: string;
|
||||||
wordWrap?: boolean
|
downloadFileName?: string;
|
||||||
downloadFileName?: string
|
downloadButtonText?: string;
|
||||||
downloadButtonText?: string
|
maxHeight?: string;
|
||||||
maxHeight?: string
|
showLineNumbers?: boolean;
|
||||||
showLineNumbers?: boolean
|
showFoldGutter?: boolean;
|
||||||
showFoldGutter?: boolean
|
|
||||||
}>(),
|
}>(),
|
||||||
{
|
{
|
||||||
language: 'txt',
|
language: 'txt',
|
||||||
@@ -55,7 +48,17 @@ const props = withDefaults(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const { value, maxHeight, language, showLineNumbers, showFoldGutter, copyPlacement, copyMessage, downloadFileName, downloadButtonText } = toRefs(props);
|
const {
|
||||||
|
value,
|
||||||
|
maxHeight,
|
||||||
|
language,
|
||||||
|
showLineNumbers,
|
||||||
|
showFoldGutter,
|
||||||
|
copyPlacement,
|
||||||
|
copyMessage,
|
||||||
|
downloadFileName,
|
||||||
|
downloadButtonText,
|
||||||
|
} = toRefs(props);
|
||||||
|
|
||||||
const styleStore = useStyleStore();
|
const styleStore = useStyleStore();
|
||||||
const isDarkTheme = computed(() => styleStore.isDarkTheme);
|
const isDarkTheme = computed(() => styleStore.isDarkTheme);
|
||||||
@@ -68,11 +71,7 @@ const themeCompartment = new Compartment();
|
|||||||
const languageCompartment = new Compartment();
|
const languageCompartment = new Compartment();
|
||||||
|
|
||||||
const maxHeightStyle = computed(() =>
|
const maxHeightStyle = computed(() =>
|
||||||
maxHeight.value
|
maxHeight.value ? (typeof maxHeight.value === 'number' ? `${maxHeight.value}px` : maxHeight.value) : '400px',
|
||||||
? typeof maxHeight.value === 'number'
|
|
||||||
? `${maxHeight.value}px`
|
|
||||||
: maxHeight.value
|
|
||||||
: '400px',
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
@@ -143,13 +142,12 @@ function createExtensions() {
|
|||||||
basicSetup,
|
basicSetup,
|
||||||
EditorView.editable.of(false),
|
EditorView.editable.of(false),
|
||||||
EditorState.readOnly.of(true),
|
EditorState.readOnly.of(true),
|
||||||
|
EditorView.lineWrapping,
|
||||||
keymap.of(foldKeymap),
|
keymap.of(foldKeymap),
|
||||||
themeCompartment.of(themeExtension()),
|
themeCompartment.of(themeExtension()),
|
||||||
languageCompartment.of(resolveLanguage(language.value ?? 'plain')),
|
languageCompartment.of(resolveLanguage(language.value ?? 'plain')),
|
||||||
(showLineNumbers.value ?? true) ? lineNumbers() : [],
|
(showLineNumbers.value ?? true) ? lineNumbers() : [],
|
||||||
(showFoldGutter.value ?? true)
|
(showFoldGutter.value ?? true) ? foldGutter({ openText: '▾', closedText: '▸' }) : [],
|
||||||
? foldGutter({ openText: '▾', closedText: '▸' })
|
|
||||||
: [],
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,52 +189,59 @@ watch(
|
|||||||
() => {
|
() => {
|
||||||
if (view) {
|
if (view) {
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
effects: languageCompartment.reconfigure(
|
effects: languageCompartment.reconfigure(resolveLanguage(props.language ?? 'plain')),
|
||||||
resolveLanguage(props.language ?? 'plain'),
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(() => props.value, (newVal) => {
|
watch(
|
||||||
if (!view) {
|
() => props.value,
|
||||||
return;
|
(newVal) => {
|
||||||
}
|
if (!view) {
|
||||||
const current = view.state.doc.toString();
|
return;
|
||||||
if (newVal !== current) {
|
}
|
||||||
view.dispatch({
|
const current = view.state.doc.toString();
|
||||||
changes: {
|
if (newVal !== current) {
|
||||||
from: 0,
|
view.dispatch({
|
||||||
to: view.state.doc.length,
|
changes: {
|
||||||
insert: newVal,
|
from: 0,
|
||||||
},
|
to: view.state.doc.length,
|
||||||
});
|
insert: newVal,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const { copy, isJustCopied } = useCopy({ source: value, createToast: false });
|
const { copy, isJustCopied } = useCopy({ source: value, createToast: false });
|
||||||
const tooltipText = computed(() => isJustCopied.value ? t('textareaCopyable.copied') : copyMessage.value);
|
const tooltipText = computed(() => (isJustCopied.value ? t('textareaCopyable.copied') : copyMessage.value));
|
||||||
|
|
||||||
const valueBase64 = computed(() => Base64.encode(value.value));
|
const valueBase64 = computed(() => Base64.encode(value.value));
|
||||||
const { download } = useDownloadFileFromBase64(
|
const { download } = useDownloadFileFromBase64({
|
||||||
{
|
source: valueBase64,
|
||||||
source: valueBase64,
|
filename: downloadFileName,
|
||||||
filename: downloadFileName,
|
});
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div style="overflow-x: hidden; width: 100%">
|
<div style="overflow-x: hidden; width: 100%">
|
||||||
<c-card
|
<c-card
|
||||||
relative
|
relative
|
||||||
:style="copyPlacement === 'top-right' ? 'padding-top: 50px' : (copyPlacement === 'bottom-right' ? 'padding-bottom: 50px' : '')"
|
:style="
|
||||||
|
copyPlacement === 'top-right'
|
||||||
|
? 'padding-top: 50px'
|
||||||
|
: copyPlacement === 'bottom-right'
|
||||||
|
? 'padding-bottom: 50px'
|
||||||
|
: ''
|
||||||
|
"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
v-if="value && copyPlacement !== 'none'"
|
v-if="value && copyPlacement !== 'none'"
|
||||||
absolute right-10px
|
absolute
|
||||||
:class="copyPlacement === 'top-right' ? 'top-10px' : (copyPlacement === 'bottom-right' ? 'bottom-10px' : '')"
|
right-10px
|
||||||
style="z-index: 10; background: var(--bg-color); border-radius: 50%; padding: 2px;"
|
:class="copyPlacement === 'top-right' ? 'top-10px' : copyPlacement === 'bottom-right' ? 'bottom-10px' : ''"
|
||||||
|
style="z-index: 10; background: var(--bg-color); border-radius: 50%; padding: 2px"
|
||||||
>
|
>
|
||||||
<c-tooltip v-if="value && copyPlacement !== 'outside'" :tooltip="tooltipText" position="left">
|
<c-tooltip v-if="value && copyPlacement !== 'outside'" :tooltip="tooltipText" position="left">
|
||||||
<c-button circle important:h-10 important:w-10 @click="copy()">
|
<c-button circle important:h-10 important:w-10 @click="copy()">
|
||||||
|
|||||||
Reference in new issue
Block a user