Skip to content

Commit

Permalink
refactor: BaseReader for shared code between Readers
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jun 7, 2022
1 parent 9a8f761 commit 36730c2
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 81 deletions.
77 changes: 77 additions & 0 deletions src/components/BaseReader.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!--
- @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
-
- @author Julius Härtl <[email protected]>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->

<template>
<EditorContent v-if="$editor" id="read-only-editor" :editor="$editor" />
</template>

<script>
import { Editor } from '@tiptap/core'
import { EditorContent } from '@tiptap/vue-2'

export default {
name: 'BaseReader',
components: { EditorContent },
inject: ['renderHtml', 'extensions'],

props: {
content: {
type: String,
required: true,
},
},

computed: {
htmlContent() {
return this.renderHtml(this.content)
},
},

watch: {
content() {
this.updateContent()
},
},

created() {
this.$editor = this.createEditor()
this.$editor.setOptions({ editable: false })
},

beforeDestroy() {
this.$editor.destroy()
},

methods: {
createEditor() {
return new Editor({
content: this.htmlContent,
extensions: this.extensions,
})
},

updateContent() {
this.$editor.commands.setContent(this.htmlContent)
},
},
}
</script>
49 changes: 11 additions & 38 deletions src/components/PlainTextReader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,29 @@
-->

<template>
<EditorContent v-if="$editor" id="read-only-editor" :editor="$editor" />
<BaseReader :content="content" />
</template>

<script>
import { Editor } from '@tiptap/core'
import BaseReader from './BaseReader.vue'
import { PlainText } from './../extensions/index.js'
import { EditorContent } from '@tiptap/vue-2'
import escapeHtml from 'escape-html'

export default {
name: 'PlainTextReader',
components: { EditorContent },
props: {
content: {
type: String,
required: true,
},
},
components: { BaseReader },

computed: {
htmlContent() {
return '<pre>' + escapeHtml(this.content) + '</pre>'
provide: {
renderHtml(content) {
return '<pre>' + escapeHtml(content) + '</pre>'
},
extensions: [PlainText],
},

watch: {
content() {
this.updateContent()
},
},

created() {
this.$editor = this.createEditor()
this.$editor.setOptions({ editable: false })
},

beforeDestroy() {
this.$editor.destroy()
},

methods: {
createEditor() {
return new Editor({
content: this.htmlContent,
extensions: [PlainText],
})
},

updateContent() {
this.$editor.commands.setContent(this.htmlContent)
props: {
content: {
type: String,
required: true,
},
},

Expand Down
60 changes: 17 additions & 43 deletions src/components/RichTextReader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,38 @@
-->

<template>
<EditorContent v-if="$editor" id="read-only-editor" :editor="$editor" />
<BaseReader :content="content" />
</template>

<script>
import { Editor } from '@tiptap/core'
import BaseReader from './BaseReader.vue'
import RichText from './../extensions/RichText.js'
import { EditorContent } from '@tiptap/vue-2'
import markdownit from './../markdownit/index.js'

export default {
name: 'RichTextReader',
components: { EditorContent },
props: {
content: {
type: String,
required: true,
},
},
components: { BaseReader },

computed: {
htmlContent() {
return markdownit.render(this.content)
provide: {
renderHtml(content) {
return markdownit.render(content)
},
extensions: [
RichText.configure({
link: {
onClick: (event, attrs) => this.$emit('click-link', event, attrs),
},
}),
],
},

watch: {
content() {
this.updateContent()
props: {
content: {
type: String,
required: true,
},
},

created() {
this.$editor = this.createEditor()
this.$editor.setOptions({ editable: false })
},

beforeDestroy() {
this.$editor.destroy()
},

methods: {
createEditor() {
return new Editor({
content: this.htmlContent,
extensions: [
RichText.configure({
link: {
onClick: (event, attrs) => this.$emit('click-link', event, attrs),
},
}),
],
})
},

updateContent() {
this.$editor.commands.setContent(this.htmlContent)
},
},
}
</script>

Expand Down

0 comments on commit 36730c2

Please sign in to comment.