scinote-web/app/javascript/vue/shared/inline_edit.vue

251 lines
7.7 KiB
Vue
Raw Normal View History

<template>
<div class="w-full relative flex">
2023-09-11 21:58:05 +08:00
<template v-if="editing">
<input type="text"
v-if="singleLine"
ref="input"
2023-09-14 18:12:15 +08:00
class="inline-block leading-5 outline-none pl-0 py-1 border-0 border-solid border-y w-full border-t-transparent"
2023-09-11 21:58:05 +08:00
:class="{
'inline-edit-placeholder text-sn-grey caret-black': isBlank,
'border-b-sn-delete-red': error,
'border-b-sn-science-blue': !error,
}"
v-model="newValue"
2023-09-11 21:58:05 +08:00
@keydown="handleKeypress"
@blur="handleBlur"
@keyup.escape="cancelEdit"
@focus="setCaretAtEnd"/>
<textarea v-else
ref="input"
2023-09-22 01:24:58 +08:00
class="overflow-hidden leading-5 inline-block outline-none px-0 py-1 border-0 border-solid border-y w-full border-t-transparent mb-0.5"
2023-09-11 21:58:05 +08:00
:class="{
'inline-edit-placeholder text-sn-grey caret-black': isBlank,
'border-sn-delete-red': error,
'border-sn-science-blue': !error,
}"
:placeholder="placeholder"
v-model="newValue"
2023-09-11 21:58:05 +08:00
@keydown="handleKeypress"
@blur="handleBlur"
@keyup.escape="cancelEdit"
@focus="setCaretAtEnd"/>
</template>
<div
v-else
ref="view"
2023-09-19 19:42:56 +08:00
class="grid sci-cursor-edit leading-5 border-0 py-1 outline-none inline-block border-solid border-y border-transparent"
2023-09-24 01:15:35 +08:00
:class="{ 'text-sn-grey font-normal': isBlank, 'whitespace-pre-line': !singleLine }"
@click="enableEdit($event)"
>
<span class="list-item" :class="{'truncate': singleLine}" v-if="smartAnnotation" v-html="sa_value || placeholder" ></span>
2023-09-19 19:42:56 +08:00
<span :class="{'truncate': singleLine}" v-else>{{newValue || placeholder}}</span>
</div>
2023-09-11 21:58:05 +08:00
<div
2023-09-18 21:32:53 +08:00
class="mt-2 whitespace-nowrap truncate text-xs font-normal absolute bottom-[-1rem] w-full"
:title="editing && error ? error : timestamp"
:class="{'text-sn-delete-red': editing && error}"
>
{{ editing && error ? error : timestamp }}
</div>
</div>
</template>
<script>
import UtilsMixin from '../mixins/utils.js';
2022-08-04 17:06:25 +08:00
export default {
name: 'InlineEdit',
props: {
value: { type: String, default: '' },
sa_value: { type: String},
allowBlank: { type: Boolean, default: true },
attributeName: { type: String, required: true },
characterLimit: { type: Number },
characterMinLimit: { type: Number },
timestamp: { type: String},
placeholder: { type: String },
autofocus: { type: Boolean, default: false },
saveOnEnter: { type: Boolean, default: true },
allowNewLine: { type: Boolean, default: false },
multilinePaste: { type: Boolean, default: false },
smartAnnotation: { type: Boolean, default: false },
editOnload: { type: Boolean, default: false },
2023-09-11 21:58:05 +08:00
defaultValue: { type: String, default: '' },
singleLine: { type: Boolean, default: true }
},
data() {
return {
editing: false,
dirty: false,
newValue: ''
}
},
2022-08-04 17:06:25 +08:00
mixins: [UtilsMixin],
created() {
this.newValue = this.value || '';
},
mounted() {
this.handleAutofocus();
if (this.editOnload) {
this.enableEdit();
}
},
watch: {
2023-09-11 21:58:05 +08:00
editing() {
this.refreshTexareaHeight()
},
newValue() {
if (this.newValue.length === 0 && this.editing) {
this.focus();
}
this.refreshTexareaHeight();
},
autofocus() {
this.handleAutofocus();
}
},
computed: {
isBlank() {
2023-09-05 19:39:21 +08:00
return this.newValue.length === 0;
},
isContentDefault() {
2023-09-05 19:39:21 +08:00
return this.newValue === this.defaultValue;
},
error() {
if(!this.allowBlank && this.isBlank) {
return this.i18n.t('inline_edit.errors.blank', { attribute: this.attributeName })
}
if(this.characterLimit && this.newValue.length > this.characterLimit) {
return(
this.i18n.t('inline_edit.errors.over_limit',
2022-08-04 17:06:25 +08:00
{
attribute: this.attributeName,
limit: this.numberWithSpaces(this.characterLimit)
}
)
)
}
if (this.characterMinLimit && this.newValue.length < this.characterMinLimit) {
return (
this.i18n.t('inline_edit.errors.below_limit',
{
attribute: this.attributeName,
limit: this.numberWithSpaces(this.characterMinLimit)
}
)
)
}
return false
}
},
methods: {
handleAutofocus() {
if (this.autofocus || !this.placeholder && this.isBlank || this.editOnload && this.isBlank) {
this.enableEdit();
setTimeout(this.focus, 50);
}
},
handleBlur() {
2022-07-08 19:49:22 +08:00
if ($('.atwho-view:visible').length) return;
2023-09-22 01:24:58 +08:00
this.$emit('blur');
if (this.allowBlank || !this.isBlank) {
this.$nextTick(this.update);
} else {
this.$emit('delete');
}
},
focus() {
this.$nextTick(() => {
2022-06-09 20:40:45 +08:00
if (!this.$refs.input) return;
this.$refs.input.focus();
});
},
setCaretAtEnd() {
if (this.isBlank || this.isContentDefault) return;
const el = this.$refs.input;
el.focus();
},
2022-07-08 19:49:22 +08:00
enableEdit(e) {
if (e && $(e.target).hasClass('atwho-user-popover')) return;
if (e && $(e.target).hasClass('sa-link')) return;
if (e && $(e.target).parent().hasClass('atwho-inserted')) return;
this.editing = true;
this.$nextTick(() => {
this.focus();
2023-09-11 21:58:05 +08:00
this.$refs.input.value = this.newValue;
// Select whole content if it is default
if (this.isContentDefault) {
let range = document.createRange();
range.selectNodeContents(this.$refs.input);
let selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
if (this.smartAnnotation) {
SmartAnnotation.init($(this.$refs.input), false);
}
})
this.$emit('editingEnabled');
},
cancelEdit() {
this.editing = false;
this.newValue = this.value || '';
this.$emit('editingDisabled');
},
handleInput(e) {
this.dirty = true;
const sel = document.getSelection();
const offset = sel.anchorOffset;
this.newValue = this.allowNewLine ? e.target.textContent : e.target.textContent.replace(/^[\n\r]+|[\n\r]+$/g, '');
sel.collapse(sel.anchorNode, offset);
},
handleKeypress(e) {
if (e.key == 'Escape') {
this.cancelEdit();
2023-09-22 01:24:58 +08:00
} else if (e.key == 'Enter' && this.saveOnEnter && e.shiftKey == false) {
e.preventDefault()
2023-09-22 01:24:58 +08:00
this.update(e.key);
} else {
if (!this.error) this.$emit('error-cleared');
this.dirty = true;
}
2023-09-22 01:24:58 +08:00
this.$emit('keypress', e)
},
2023-09-22 01:24:58 +08:00
update(withKey = null) {
this.refreshTexareaHeight();
if (!this.dirty && !this.isBlank) {
this.editing = false;
return;
}
if(this.error) return;
if(!this.$refs.input) return;
2023-09-11 21:58:05 +08:00
this.newValue = this.$refs.input.value.trim() // Fix for smart annotation
this.editing = false;
this.$emit('editingDisabled');
2023-09-22 01:24:58 +08:00
this.$emit('update', this.newValue, withKey);
2023-09-11 21:58:05 +08:00
},
refreshTexareaHeight() {
if (this.editing && !this.singleLine) {
this.$nextTick(() => {
2023-09-22 01:24:58 +08:00
if (!this.$refs.input) return;
this.$refs.input.style.height = '0px';
this.$refs.input.style.height = this.$refs.input.scrollHeight + 'px';
2023-09-11 21:58:05 +08:00
});
}
}
}
}
</script>