Skip to content

Commit

Permalink
feat: config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Saber2pr committed Jul 27, 2022
1 parent 2adc16d commit ac43352
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/core/createDiffEditor.ts
Expand Up @@ -12,8 +12,11 @@ export async function createDiffEditor(
container: HTMLElement,
original: string,
modified: string,
language: string = 'text/plain'
language: string = 'text/plain',
loaderConfig?: Parameters<typeof loader.config>[0]
) {
loaderConfig && loader.config(loaderConfig)

const monaco = await loader.init()
const originalModel = monaco.editor.createModel(original, language)
const modifiedModel = monaco.editor.createModel(modified, language)
Expand Down
5 changes: 4 additions & 1 deletion src/core/createEditor.ts
Expand Up @@ -33,8 +33,11 @@ export interface EditorData {
export async function createEditor(
editorContainer: HTMLElement,
modalFiles: ModalFiles,
options: EditorOptions = {}
options: EditorOptions = {},
loaderConfig?: Parameters<typeof loader.config>[0]
) {
loaderConfig && loader.config(loaderConfig)

const monaco = await loader.init()
const models = monaco.editor.getModels()
const fileMap = models.reduce((acc, m) => {
Expand Down
5 changes: 4 additions & 1 deletion src/react/diff-editor.tsx
@@ -1,3 +1,4 @@
import loader from '@monaco-editor/loader'
import React, { CSSProperties, useEffect, useRef } from 'react'

import { createDiffEditor, DiffEditorAPI } from '../core'
Expand All @@ -9,6 +10,7 @@ export interface DiffEditorProps {
onInit?: (editor: DiffEditorAPI) => any
deps?: any[]
style?: CSSProperties
loaderConfig?: Parameters<typeof loader.config>[0]
}

export const DiffEditor: React.FC<DiffEditorProps> = ({
Expand All @@ -18,12 +20,13 @@ export const DiffEditor: React.FC<DiffEditorProps> = ({
language,
deps,
style,
loaderConfig
}) => {
const ref = useRef<HTMLDivElement>()

useEffect(() => {
if (ref.current) {
createDiffEditor(ref.current, original, modified, language).then(
createDiffEditor(ref.current, original, modified, language, loaderConfig).then(
editor => {
if (onInit) {
onInit(editor)
Expand Down
5 changes: 4 additions & 1 deletion src/react/editor.tsx
@@ -1,3 +1,4 @@
import loader from '@monaco-editor/loader'
import React, {
CSSProperties,
useEffect,
Expand Down Expand Up @@ -26,6 +27,7 @@ export interface EditorProps {
types?: Record<string, string>
tsconfig?: CompilerOptions
theme?: ThemeNames
loaderConfig?: Parameters<typeof loader.config>[0]
}

export const Editor = React.forwardRef<EditorAPI, EditorProps>(
Expand All @@ -40,6 +42,7 @@ export const Editor = React.forwardRef<EditorAPI, EditorProps>(
tsconfig = {},
theme,
className,
loaderConfig
},
parentRef
) => {
Expand All @@ -50,7 +53,7 @@ export const Editor = React.forwardRef<EditorAPI, EditorProps>(

useEffect(() => {
if (ref.current) {
createEditor(ref.current, modalFiles, options).then(editor => {
createEditor(ref.current, modalFiles, options, loaderConfig).then(editor => {
apiRef.current = editor
if (onInit) {
onInit(editor)
Expand Down

0 comments on commit ac43352

Please sign in to comment.