Skip to content

Commit

Permalink
Merge pull request #3562 from serlo/feat/editor-plugins-renderers-con…
Browse files Browse the repository at this point in the history
…fig-object

feat(editor): plugins renderers config object
  • Loading branch information
hejtful authored Apr 3, 2024
2 parents 3d7d75a + efdf1e2 commit d079e42
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 27 deletions.
18 changes: 10 additions & 8 deletions packages/editor/src/editor-integration/create-basic-plugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@ import { EditorPluginType } from '@editor/types/editor-plugin-type'
import { SupportedLanguage } from '@editor/types/language-data'
import { TemplatePluginType } from '@editor/types/template-plugin-type'

export interface CreateBasicPluginsConfig {
language?: SupportedLanguage
allowedChildPlugins?: string[]
exerciseVisibleInSuggestion: boolean
enableTextAreaExercise?: boolean
allowImageInTableCells: boolean
multimediaConfig?: MultimediaConfig
}

export function createBasicPlugins({
language = 'de',
enableTextAreaExercise = false,
exerciseVisibleInSuggestion,
allowedChildPlugins,
allowImageInTableCells,
multimediaConfig,
}: {
language?: SupportedLanguage
allowedChildPlugins?: string[]
exerciseVisibleInSuggestion: boolean
enableTextAreaExercise?: boolean
allowImageInTableCells: boolean
multimediaConfig?: MultimediaConfig
}) {
}: CreateBasicPluginsConfig) {
const editorStrings = editorData[language].loggedInData.strings.editor

return [
Expand Down
20 changes: 16 additions & 4 deletions packages/editor/src/editor-integration/create-renderers.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
import type {
InitRenderersArgs,
LinkRenderer,
PluginStaticRenderer,
} from '@editor/plugin/helpers/editor-renderer'
import { AnchorStaticRenderer } from '@editor/plugins/anchor/static'
import { ArticleStaticRenderer } from '@editor/plugins/article/static'
Expand Down Expand Up @@ -30,9 +31,10 @@ import { TemplatePluginType } from '@editor/types/template-plugin-type'
import { ComponentProps } from 'react'

import { Lazy } from '@/components/content/lazy'
import { Link } from '@/components/content/link'

export function createRenderers(): InitRenderersArgs {
export function createRenderers(
customPluginRenderers: PluginStaticRenderer[]
): InitRenderersArgs {
return {
pluginRenderers: [
// plugins
Expand Down Expand Up @@ -106,6 +108,7 @@ export function createRenderers(): InitRenderersArgs {
type: TemplatePluginType.GenericContent,
renderer: GenericContentTypeStaticRenderer,
},
...customPluginRenderers,
],
mathRenderer: (element: MathElement) =>
element.inline ? (
Expand All @@ -116,7 +119,16 @@ export function createRenderers(): InitRenderersArgs {
</Lazy>
),
linkRenderer: ({ href, children }: ComponentProps<LinkRenderer>) => {
return <Link href={href}>{children}</Link>
return (
<a
className="serlo-link cursor-pointer"
href={href}
target="_blank"
rel="noopener noreferrer"
>
{children}
</a>
)
},
}
}
44 changes: 35 additions & 9 deletions packages/editor/src/package/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { Editor, type EditorProps } from '@editor/core'
import {
type CreateBasicPluginsConfig,
createBasicPlugins,
} from '@editor/editor-integration/create-basic-plugins'
import { createRenderers } from '@editor/editor-integration/create-renderers'
import {
editorPlugins,
type PluginWithData,
} from '@editor/plugin/helpers/editor-plugins'
import {
editorRenderers,
type PluginStaticRenderer,
} from '@editor/plugin/helpers/editor-renderer'
import { EditorPluginType } from '@editor/types/editor-plugin-type'
import { SupportedLanguage } from '@editor/types/language-data'
import React from 'react'

import { editorData } from './editor-data'
import { EditorPluginType } from '../types/editor-plugin-type'
import { InstanceDataProvider } from '@/contexts/instance-context'
import { LoggedInDataProvider } from '@/contexts/logged-in-data-context'

import '@/assets-webkit/styles/serlo-tailwind.css'

// Custom plugins and renderers are an Edusharing specific feature,
// and will not be supported in the future
export interface PluginsConfig {
basicPluginsConfig: CreateBasicPluginsConfig
customPlugins?: Array<PluginWithData & PluginStaticRenderer>
}

export interface SerloEditorProps {
language?: SupportedLanguage
initialState?: EditorProps['initialState']
children: EditorProps['children']
pluginsConfig: PluginsConfig
initialState?: EditorProps['initialState']
language?: SupportedLanguage
}

const emptyState = {
plugin: EditorPluginType.Rows,
state: [
Expand All @@ -25,18 +47,22 @@ const emptyState = {
}

/** For exporting the editor */
export function SerloEditor({
language = 'de',
initialState,
...props
}: SerloEditorProps) {
export function SerloEditor(props: SerloEditorProps) {
const { children, pluginsConfig, initialState, language = 'de' } = props
const { basicPluginsConfig, customPlugins = [] } = pluginsConfig
const { instanceData, loggedInData } = editorData[language]

const basicPlugins = createBasicPlugins(basicPluginsConfig)
editorPlugins.init([...basicPlugins, ...customPlugins])

const basicRenderers = createRenderers(customPlugins)
editorRenderers.init(basicRenderers)

return (
<InstanceDataProvider value={instanceData}>
<LoggedInDataProvider value={loggedInData}>
<div className="serlo-editor-hacks">
<Editor initialState={initialState ?? emptyState} {...props} />
<Editor initialState={initialState ?? emptyState}>{children}</Editor>
</div>
</LoggedInDataProvider>
</InstanceDataProvider>
Expand Down
7 changes: 1 addition & 6 deletions packages/editor/src/package/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export { SerloEditor, type SerloEditorProps } from './editor'
export { SerloRenderer, type SerloRendererProps } from './serlo-renderer'

export { type BaseEditor } from '@editor/core'

export { editorPlugins } from '@editor/plugin/helpers/editor-plugins'
export { editorRenderers } from '@editor/plugin/helpers/editor-renderer'
export type { BaseEditor } from '@editor/core'

export { EditorPluginType } from '@editor/types/editor-plugin-type'

Expand All @@ -22,5 +19,3 @@ export { FaIcon } from '@/components/fa-icon'
export { EditorInput, PreviewOverlay } from '@editor/editor-ui'
export { PluginToolbar } from '@editor/editor-ui/plugin-toolbar'
export { PluginDefaultTools } from '@editor/editor-ui/plugin-toolbar/plugin-tool-menu/plugin-default-tools'
export { createRenderers } from '@editor/editor-integration/create-renderers'
export { createBasicPlugins } from '@editor/editor-integration/create-basic-plugins'
9 changes: 9 additions & 0 deletions packages/editor/src/package/serlo-renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { createRenderers } from '@editor/editor-integration/create-renderers'
import { editorRenderers } from '@editor/plugin/helpers/editor-renderer'
import { StaticRenderer } from '@editor/static-renderer/static-renderer'
import type { AnyEditorDocument } from '@editor/types/editor-plugins'
import type { SupportedLanguage } from '@editor/types/language-data'

import type { PluginsConfig } from './editor'
import { editorData } from './editor-data'
import { InstanceDataProvider } from '@/contexts/instance-context'
import { LoggedInDataProvider } from '@/contexts/logged-in-data-context'

export interface SerloRendererProps {
pluginsConfig: PluginsConfig
language?: SupportedLanguage
document?: AnyEditorDocument | AnyEditorDocument[]
}

export function SerloRenderer({
pluginsConfig,
language = 'de',
...props
}: SerloRendererProps) {
const { customPlugins = [] } = pluginsConfig
const { instanceData, loggedInData } = editorData[language]

const basicRenderers = createRenderers(customPlugins)
editorRenderers.init(basicRenderers)

return (
<InstanceDataProvider value={instanceData}>
<LoggedInDataProvider value={loggedInData}>
Expand Down

0 comments on commit d079e42

Please sign in to comment.