2020-05-20 14:21:08 +00:00
|
|
|
import {
|
|
|
|
defineComponent,
|
|
|
|
reactive,
|
|
|
|
ref,
|
2020-05-24 15:33:25 +00:00
|
|
|
Ref,
|
2020-05-20 14:21:08 +00:00
|
|
|
onMounted,
|
2020-05-24 15:33:25 +00:00
|
|
|
watch,
|
2020-05-24 20:00:14 +00:00
|
|
|
PropType,
|
2020-05-20 14:21:08 +00:00
|
|
|
} from '@vue/composition-api';
|
|
|
|
import {
|
|
|
|
model,
|
2020-05-24 15:33:25 +00:00
|
|
|
blockProps,
|
|
|
|
useActivation,
|
2020-05-20 14:21:08 +00:00
|
|
|
} from '@components/TreeElement';
|
|
|
|
|
2020-05-24 20:00:14 +00:00
|
|
|
import {
|
|
|
|
getDefaultData,
|
|
|
|
ParagraphData,
|
|
|
|
ParagraphProps,
|
|
|
|
} from './util.ts';
|
|
|
|
|
2020-05-20 14:21:08 +00:00
|
|
|
import SbToolbar from '@internal/Toolbar';
|
|
|
|
|
2020-05-24 20:00:14 +00:00
|
|
|
import './style.scss';
|
2020-05-20 14:21:08 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2020-05-24 20:00:14 +00:00
|
|
|
name: 'sb-paragraph-edit',
|
2020-05-20 14:21:08 +00:00
|
|
|
|
|
|
|
model,
|
|
|
|
|
|
|
|
props: {
|
2020-05-24 15:33:25 +00:00
|
|
|
...blockProps,
|
2020-05-24 20:00:14 +00:00
|
|
|
data: {
|
|
|
|
type: (null as unknown) as PropType<ParagraphData>,
|
|
|
|
default: getDefaultData,
|
|
|
|
},
|
2020-05-20 14:21:08 +00:00
|
|
|
},
|
|
|
|
|
2020-05-24 20:00:14 +00:00
|
|
|
setup(props: ParagraphProps, context) {
|
2020-05-24 15:33:25 +00:00
|
|
|
const localData = reactive({
|
|
|
|
value: props.data.value,
|
|
|
|
focused: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const inputEl: Ref<null|HTMLElement> = ref(null);
|
|
|
|
|
|
|
|
const { isActive, activate } = useActivation(props.blockId);
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (inputEl.value) {
|
|
|
|
inputEl.value.innerHTML = localData.value;
|
|
|
|
|
|
|
|
if (isActive) {
|
|
|
|
inputEl.value.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(() => props.data, () => {
|
|
|
|
localData.value = props.data.value;
|
|
|
|
if (inputEl.value) {
|
|
|
|
inputEl.value.innerHTML = localData.value;
|
|
|
|
}
|
|
|
|
});
|
2020-05-20 14:21:08 +00:00
|
|
|
|
|
|
|
const onTextUpdate = ($event: InputEvent) => {
|
2020-05-24 15:33:25 +00:00
|
|
|
localData.value = $event.target.innerHTML;
|
2020-05-20 14:21:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const classes = reactive({
|
|
|
|
'sb-paragraph': true,
|
2020-05-24 15:33:25 +00:00
|
|
|
'sb-paragraph_focused': localData.focused,
|
2020-05-20 14:21:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const onFocus = () => {
|
2020-05-24 15:33:25 +00:00
|
|
|
localData.focused = true;
|
2020-05-20 14:21:08 +00:00
|
|
|
};
|
2020-05-24 15:33:25 +00:00
|
|
|
|
2020-05-20 14:21:08 +00:00
|
|
|
const onBlur = () => {
|
2020-05-24 15:33:25 +00:00
|
|
|
localData.focused = false;
|
|
|
|
context.emit('update', {
|
|
|
|
value: localData.value,
|
2020-05-20 14:21:08 +00:00
|
|
|
});
|
2020-05-24 15:33:25 +00:00
|
|
|
activate(null);
|
2020-05-20 14:21:08 +00:00
|
|
|
};
|
|
|
|
|
2020-05-24 15:33:25 +00:00
|
|
|
const onKeypress = ($event: KeyboardEvent) => {
|
|
|
|
if ($event.key === 'Enter') {
|
|
|
|
const blockId = +(new Date());
|
|
|
|
context.emit('insert-block', {
|
|
|
|
blockId,
|
|
|
|
name: 'sb-paragraph',
|
2020-05-24 20:00:14 +00:00
|
|
|
data: getDefaultData(),
|
2020-05-24 15:33:25 +00:00
|
|
|
});
|
2020-05-20 14:21:08 +00:00
|
|
|
|
2020-05-24 15:33:25 +00:00
|
|
|
activate(blockId);
|
|
|
|
|
|
|
|
$event.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
2020-05-20 14:21:08 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
classes,
|
2020-05-24 15:33:25 +00:00
|
|
|
localData,
|
2020-05-20 14:21:08 +00:00
|
|
|
onTextUpdate,
|
|
|
|
onFocus,
|
|
|
|
onBlur,
|
2020-05-24 15:33:25 +00:00
|
|
|
onKeypress,
|
2020-05-20 14:21:08 +00:00
|
|
|
inputEl,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
render() {
|
2020-05-24 15:33:25 +00:00
|
|
|
console.log('render paragraph');
|
2020-05-20 14:21:08 +00:00
|
|
|
return (
|
2020-05-24 15:33:25 +00:00
|
|
|
<div class="sb-paragraph">
|
2020-05-20 14:21:08 +00:00
|
|
|
<SbToolbar>Paragraph editing</SbToolbar>
|
|
|
|
<p
|
|
|
|
class={this.classes}
|
|
|
|
ref="inputEl"
|
|
|
|
contenteditable
|
|
|
|
{...{
|
|
|
|
on: {
|
|
|
|
input: this.onTextUpdate,
|
|
|
|
focus: this.onFocus,
|
|
|
|
blur: this.onBlur,
|
2020-05-24 15:33:25 +00:00
|
|
|
keypress: this.onKeypress,
|
2020-05-20 14:21:08 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
></p>
|
2020-05-24 15:33:25 +00:00
|
|
|
</div>
|
2020-05-20 14:21:08 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|