Marco Bomfim f921244661 fix(editor): Fix hashtags not working after embeded content
Up to this point, once you've inserted a given URL to the editor, and it renders a new embeded content, if you hit Return, and try using a tag, or mention, it would not work since the paragraph was never closed, causing a sintatic error that rendered the mentions unable to be used unless you explicitly hit return again, to escape the given text block you were in. See the PR/Issue for further info.
2019-11-06 16:24:48 -03:00

94 lines
2.1 KiB
JavaScript

import { Node } from 'tiptap'
import pasteRule from '../commands/pasteRule'
import { compileToFunctions } from 'vue-template-compiler'
import Vue from 'vue'
import EmbedComponent from '~/components/Embed/EmbedComponent'
Vue.component(EmbedComponent)
const template = `<component :dataEmbedUrl="dataEmbedUrl" :embedData="embedData" :is="componentType" />`
const compiledTemplate = compileToFunctions(template)
export default class Embed extends Node {
get name() {
return 'embed'
}
get defaultOptions() {
return {
onEmbed: () => ({}),
}
}
pasteRules({ type, schema }) {
return [
pasteRule(
// source: https://stackoverflow.com/a/3809435
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/g,
type,
url => ({ dataEmbedUrl: url }),
),
]
}
get schema() {
return {
attrs: {
dataEmbedUrl: {
default: null,
},
},
group: 'block',
inline: false,
parseDOM: [
{
tag: 'a[href].embed',
getAttrs: dom => ({
dataEmbedUrl: dom.getAttribute('href'),
}),
},
],
toDOM: node => [
'a',
{
href: node.attrs.dataEmbedUrl,
class: 'embed',
target: '_blank',
},
],
}
}
get view() {
return {
props: ['node', 'updateAttrs', 'options'],
data: () => ({
embedData: {},
}),
async created() {
if (this.options) {
this.embedData = await this.options.onEmbed({ url: this.dataEmbedUrl })
}
},
computed: {
componentType() {
return EmbedComponent
},
dataEmbedUrl: {
get() {
return this.node.attrs.dataEmbedUrl
},
set(dataEmbedUrl) {
this.updateAttrs({
dataEmbedUrl,
})
},
},
},
render(createElement) {
return compiledTemplate.render.call(this, createElement)
},
}
}
}