| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <script lang="ts" setup>
- import { computed, ref, watch } from 'vue'
- import { defaultStickyProps } from './constants'
- import type { StickyProps } from './types'
- import { ElInput } from 'element-plus'
- import Markdown from '../markdown/Markdown.vue'
- const props = withDefaults(defineProps<StickyProps>(), defaultStickyProps)
- const emit = defineEmits<{
- edit: [editing: boolean]
- 'update:modelValue': [value: string]
- 'markdown-click': [link: HTMLAnchorElement, e: MouseEvent]
- }>()
- const isResizing = ref(false)
- const input = ref<HTMLTextAreaElement | undefined>(undefined)
- const resHeight = computed((): number => {
- return props.height < props.minHeight ? props.minHeight : props.height
- })
- const resWidth = computed((): number => {
- return props.width < props.minWidth ? props.minWidth : props.width
- })
- const inputName = computed(() => (props.id ? `${props.id}-input` : undefined))
- const styles = computed((): { height: string; width: string; backgroundColor: string } => ({
- height: `${resHeight.value}px`,
- width: `${resWidth.value}px`,
- backgroundColor: props.backgroundColor
- }))
- const shouldShowFooter = computed((): boolean => resHeight.value > 100 && resWidth.value > 155)
- watch(
- () => props.editMode,
- (newMode, prevMode) => {
- setTimeout(() => {
- if (newMode && !prevMode && input.value) {
- if (props.defaultText === props.modelValue) {
- input.value.select()
- }
- input.value.focus()
- }
- }, 100)
- }
- )
- const onDoubleClick = () => {
- if (!props.readOnly) emit('edit', true)
- }
- const onInputBlur = () => {
- if (!isResizing.value) emit('edit', false)
- }
- const onUpdateModelValue = (value: string) => {
- emit('update:modelValue', value)
- }
- const onMarkdownClick = (link: HTMLAnchorElement, event: MouseEvent) => {
- emit('markdown-click', link, event)
- }
- const onInputScroll = (event: WheelEvent) => {
- // Pass through zoom events but hold regular scrolling
- if (!event.ctrlKey && !event.metaKey) {
- event.stopPropagation()
- }
- }
- </script>
- <template>
- <div
- :class="{
- 'sticky-note': true,
- [$style.sticky]: true,
- [$style.clickable]: !isResizing
- }"
- :style="styles"
- @keydown.prevent
- >
- <div v-show="!editMode" :class="$style.wrapper" @dblclick.stop="onDoubleClick">
- <Markdown
- theme="sticky"
- :content="modelValue"
- :with-multi-breaks="true"
- @markdown-click="onMarkdownClick"
- @update-content="onUpdateModelValue"
- />
- </div>
- <div
- v-show="editMode"
- :class="{ 'full-height': !shouldShowFooter, 'sticky-textarea': true }"
- @click.stop
- @mousedown.stop
- @mouseup.stop
- @keydown.esc="onInputBlur"
- @keydown.stop
- >
- <ElInput
- ref="input"
- :model-value="modelValue"
- :name="inputName"
- type="textarea"
- :rows="5"
- @blur="onInputBlur"
- @update:model-value="onUpdateModelValue"
- @wheel="onInputScroll"
- />
- </div>
- <div v-if="editMode && shouldShowFooter" :class="$style.footer">
- <span>{{ footerText }}</span>
- </div>
- </div>
- </template>
- <style lang="less" module>
- .sticky {
- position: relative;
- border-radius: 4px;
- overflow: hidden;
- border: 1px solid #eee;
- }
- .clickable {
- cursor: pointer;
- }
- .wrapper {
- width: 100%;
- height: 100%;
- position: absolute;
- padding: 0.5rem 0.75rem 0;
- overflow: hidden;
- box-sizing: border-box;
- }
- </style>
- <style lang="less">
- .sticky-textarea {
- height: 100%;
- box-sizing: border-box;
- padding: 0.5rem 0.75rem;
- cursor: default;
- .el-textarea {
- height: 100%;
- .el-textarea__inner {
- height: 100%;
- resize: unset;
- }
- }
- }
- .full-height {
- height: calc(100% - var(--spacing--2xs));
- }
- </style>
|