Skip to content

Commit

Permalink
Merge pull request #11060 from owncloud/feat/lazy-load-toast-editor-p…
Browse files Browse the repository at this point in the history
…arts

perf: lazy load toast-ui editor dependencies
  • Loading branch information
JammingBen authored Jun 19, 2024
2 parents b2f88f8 + c1b9ead commit 430aebd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 43 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/enhancement-text-editor-loading-times
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Decrease text editor loading times

We've decreased the loading times of the text and markdown editor by loading the necessary parts only.

https://github.com/owncloud/web/pull/11060
https://github.com/owncloud/web/issues/10982
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
<template>
<div id="text-editor-container" ref="toastUiEditorRef" :data-markdown-mode="isMarkdown" />
<app-loading-spinner v-if="loading" />
<div v-else id="text-editor-container" ref="toastUiEditorRef" :data-markdown-mode="isMarkdown" />
</template>

<script lang="ts">
import { computed, defineComponent, unref, PropType, ref, onMounted } from 'vue'
import { computed, defineComponent, unref, PropType, ref, onMounted, nextTick } from 'vue'
import { Resource } from '@ownclouders/web-client'
import '@toast-ui/editor/dist/toastui-editor.css'
import '@toast-ui/editor/dist/theme/toastui-editor-dark.css'
import '@toast-ui/editor/dist/i18n/ar'
import '@toast-ui/editor/dist/i18n/cs-cz'
import '@toast-ui/editor/dist/i18n/de-de'
import '@toast-ui/editor/dist/i18n/en-us'
import '@toast-ui/editor/dist/i18n/es-es'
import '@toast-ui/editor/dist/i18n/fi-fi'
import '@toast-ui/editor/dist/i18n/fr-fr'
import '@toast-ui/editor/dist/i18n/gl-es'
import '@toast-ui/editor/dist/i18n/hr-hr'
import '@toast-ui/editor/dist/i18n/it-it'
import '@toast-ui/editor/dist/i18n/ja-jp'
import '@toast-ui/editor/dist/i18n/ko-kr'
import '@toast-ui/editor/dist/i18n/nb-no'
import '@toast-ui/editor/dist/i18n/nl-nl'
import '@toast-ui/editor/dist/i18n/pl-pl'
import '@toast-ui/editor/dist/i18n/ru-ru'
import '@toast-ui/editor/dist/i18n/sv-se'
import '@toast-ui/editor/dist/i18n/tr-tr'
import '@toast-ui/editor/dist/i18n/uk-ua'
import '@toast-ui/editor/dist/i18n/zh-cn'
import '@toast-ui/editor/dist/i18n/zh-tw'
// @ts-ignore
import { Editor, EditorCore, EditorOptions } from '@toast-ui/editor'
// @ts-ignore
import codeSyntaxHighlight from '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js'
import 'prismjs/themes/prism.css'
import '@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight.css'
import { useGettext } from 'vue3-gettext'
import { useThemeStore } from '../composables'
import { AppConfigObject } from '../apps'
import { useThemeStore } from '../../composables'
import { AppConfigObject } from '../../apps'
import AppLoadingSpinner from './../AppLoadingSpinner.vue'
export default defineComponent({
name: 'TextEditor',
components: { AppLoadingSpinner },
props: {
applicationConfig: { type: Object as PropType<AppConfigObject>, required: true },
currentContent: {
Expand All @@ -56,7 +33,9 @@ export default defineComponent({
setup(props, { emit }) {
const language = useGettext()
const themeStore = useThemeStore()
const toastUiEditorRef = ref()
const toastUiEditorRef = ref<HTMLElement>()
const loading = ref(true)
// Should not be a ref, otherwise functions like setMarkdown won't work
let toastUiEditor: EditorCore = null
const config = computed(() => {
Expand All @@ -71,7 +50,35 @@ export default defineComponent({
)
})
onMounted(() => {
const loadSyntaxHighlighting = async () => {
const [plugin] = await Promise.all([
import(
`@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight-all.js`
),
import(
// @ts-ignore
`@toast-ui/editor-plugin-code-syntax-highlight/dist/toastui-editor-plugin-code-syntax-highlight.css`
),
// @ts-ignore
import('prismjs/themes/prism.css')
])
return plugin.default
}
onMounted(async () => {
let codeSyntaxHighlight: unknown
if (unref(isMarkdown)) {
codeSyntaxHighlight = await loadSyntaxHighlighting()
if (!props.isReadOnly) {
await import('./l18n')
}
}
loading.value = false
await nextTick()
let config: EditorOptions = {
el: unref(toastUiEditorRef),
usageStatistics: false, // sends hostname to google analytics DISABLE
Expand All @@ -80,7 +87,7 @@ export default defineComponent({
hideModeSwitch: true,
language: language.current,
height: '100%',
plugins: [codeSyntaxHighlight],
plugins: [...((codeSyntaxHighlight && [codeSyntaxHighlight]) || [])],
viewer: props.isReadOnly,
events: {
change: () => {
Expand All @@ -103,7 +110,8 @@ export default defineComponent({
return {
toastUiEditorRef,
isMarkdown
isMarkdown,
loading
}
}
})
Expand Down
6 changes: 6 additions & 0 deletions packages/web-pkg/src/components/TextEditor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineAsyncComponent } from 'vue'

// async component to avoid loading the huge toastjs package on page load
export const TextEditor = defineAsyncComponent(
async () => (await import('./TextEditor.vue')).default
)
23 changes: 23 additions & 0 deletions packages/web-pkg/src/components/TextEditor/l18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import('@toast-ui/editor/dist/i18n/ar')
import('@toast-ui/editor/dist/i18n/cs-cz')
import('@toast-ui/editor/dist/i18n/de-de')
import('@toast-ui/editor/dist/i18n/en-us')
import('@toast-ui/editor/dist/i18n/es-es')
import('@toast-ui/editor/dist/i18n/fi-fi')
import('@toast-ui/editor/dist/i18n/fr-fr')
import('@toast-ui/editor/dist/i18n/gl-es')
import('@toast-ui/editor/dist/i18n/hr-hr')
import('@toast-ui/editor/dist/i18n/it-it')
import('@toast-ui/editor/dist/i18n/ja-jp')
import('@toast-ui/editor/dist/i18n/ko-kr')
import('@toast-ui/editor/dist/i18n/nb-no')
import('@toast-ui/editor/dist/i18n/nl-nl')
import('@toast-ui/editor/dist/i18n/pl-pl')
import('@toast-ui/editor/dist/i18n/ru-ru')
import('@toast-ui/editor/dist/i18n/sv-se')
import('@toast-ui/editor/dist/i18n/tr-tr')
import('@toast-ui/editor/dist/i18n/uk-ua')
import('@toast-ui/editor/dist/i18n/zh-cn')
import('@toast-ui/editor/dist/i18n/zh-tw')

export default {}
8 changes: 1 addition & 7 deletions packages/web-pkg/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { defineAsyncComponent } from 'vue'

export * from './AppBar'
export * from './AppTemplates'
export * from './ContextActions'
Expand All @@ -9,6 +7,7 @@ export * from './Modals'
export * from './SideBar'
export * from './Search'
export * from './Spaces'
export * from './TextEditor'

export { default as AppBanner } from './AppBanner.vue'
export { default as AppLoadingSpinner } from './AppLoadingSpinner.vue'
Expand All @@ -28,8 +27,3 @@ export { default as ViewOptions } from './ViewOptions.vue'
export { default as PortalTarget } from './PortalTarget.vue'
export { default as CreateShortcutModal } from './CreateShortcutModal.vue'
export { default as CreateLinkModal } from './CreateLinkModal.vue'

// async component to avoid loading the huge toastjs package on page load
export const TextEditor = defineAsyncComponent(
async () => (await import('./TextEditor.vue')).default
)
2 changes: 1 addition & 1 deletion packages/web-runtime/src/defaults/languages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Please also make language adjustments in packages/web-pkg/src/components/TextEditor.vue
//Please also make language adjustments in packages/web-pkg/src/components/TextEditor/l18n.ts

export const supportedLanguages = {
af: 'Afrikaans - Afrikaans',
Expand Down

0 comments on commit 430aebd

Please sign in to comment.