Commit a75b8f9c authored by yann300's avatar yann300

fix monaco and editor reference

parent 7c763777
export interface Action { export interface Action {
type: string; type: string;
payload: Record<string, any> payload: Record<string, any>
monaco: any monaco: any,
editor: any
} }
export const initialState = {} export const initialState = {}
export const reducerActions = (models = initialState, action: Action) => { export const reducerActions = (models = initialState, action: Action) => {
const monaco = action.monaco const monaco = action.monaco
const editor = action.editor
switch (action.type) { switch (action.type) {
case 'ADD_MODEL': { case 'ADD_MODEL': {
if (!monaco) return models if (!editor) return models
const uri = action.payload.uri const uri = action.payload.uri
const value = action.payload.value const value = action.payload.value
const language = action.payload.language const language = action.payload.language
...@@ -29,7 +31,7 @@ export const reducerActions = (models = initialState, action: Action) => { ...@@ -29,7 +31,7 @@ export const reducerActions = (models = initialState, action: Action) => {
return models return models
} }
case 'SET_VALUE': { case 'SET_VALUE': {
if (!monaco.editor) return models if (!editor) return models
const uri = action.payload.uri const uri = action.payload.uri
const value = action.payload.value const value = action.payload.value
const model = models[uri]?.model const model = models[uri]?.model
...@@ -39,39 +41,40 @@ export const reducerActions = (models = initialState, action: Action) => { ...@@ -39,39 +41,40 @@ export const reducerActions = (models = initialState, action: Action) => {
return models return models
} }
case 'REVEAL_LINE': { case 'REVEAL_LINE': {
if (!monaco.editor) return models if (!editor) return models
const line = action.payload.line const line = action.payload.line
const column = action.payload.column const column = action.payload.column
monaco.editor.revealLine(line) editor.revealLine(line)
monaco.editor.setPosition({ column, lineNumber: line }) editor.setPosition({ column, lineNumber: line })
return models return models
} }
case 'FOCUS': { case 'FOCUS': {
if (!monaco.editor) return models if (!editor) return models
monaco.editor.focus() editor.focus()
return models return models
} }
case 'SET_FONTSIZE': { case 'SET_FONTSIZE': {
if (!monaco.editor) return models if (!editor) return models
const size = action.payload.size const size = action.payload.size
monaco.editor.updateOptions({ fontSize: size }) editor.updateOptions({ fontSize: size })
return models return models
} }
case 'SET_WORDWRAP': { case 'SET_WORDWRAP': {
if (!monaco.editor) return models if (!editor) return models
const wrap = action.payload.wrap const wrap = action.payload.wrap
monaco.editor.updateOptions({ wordWrap: wrap ? 'on' : 'off' }) editor.updateOptions({ wordWrap: wrap ? 'on' : 'off' })
return models return models
} }
} }
} }
export const reducerListener = (plugin, dispatch, monaco, events) => { export const reducerListener = (plugin, dispatch, monaco, editor, events) => {
plugin.on('editor', 'addModel', (value, language, uri, readOnly) => { plugin.on('editor', 'addModel', (value, language, uri, readOnly) => {
dispatch({ dispatch({
type: 'ADD_MODEL', type: 'ADD_MODEL',
payload: { uri, value, language, readOnly, events }, payload: { uri, value, language, readOnly, events },
monaco monaco,
editor
}) })
}) })
...@@ -79,7 +82,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -79,7 +82,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'DISPOSE_MODEL', type: 'DISPOSE_MODEL',
payload: { uri }, payload: { uri },
monaco monaco,
editor
}) })
}) })
...@@ -87,7 +91,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -87,7 +91,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'SET_VALUE', type: 'SET_VALUE',
payload: { uri, value }, payload: { uri, value },
monaco monaco,
editor
}) })
}) })
...@@ -95,7 +100,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -95,7 +100,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'REVEAL_LINE', type: 'REVEAL_LINE',
payload: { line, column }, payload: { line, column },
monaco monaco,
editor
}) })
}) })
...@@ -103,7 +109,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -103,7 +109,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'FOCUS', type: 'FOCUS',
payload: {}, payload: {},
monaco monaco,
editor
}) })
}) })
...@@ -111,7 +118,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -111,7 +118,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'SET_FONTSIZE', type: 'SET_FONTSIZE',
payload: { size }, payload: { size },
monaco monaco,
editor
}) })
}) })
...@@ -119,7 +127,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => { ...@@ -119,7 +127,8 @@ export const reducerListener = (plugin, dispatch, monaco, events) => {
dispatch({ dispatch({
type: 'SET_WORDWRAP', type: 'SET_WORDWRAP',
payload: { wrap }, payload: { wrap },
monaco monaco,
editor
}) })
}) })
} }
...@@ -206,6 +206,7 @@ export const EditorUI = (props: EditorUIProps) => { ...@@ -206,6 +206,7 @@ export const EditorUI = (props: EditorUIProps) => {
function handleEditorDidMount (editor) { function handleEditorDidMount (editor) {
editorRef.current = editor editorRef.current = editor
monacoRef.current.editor.setTheme(props.theme) monacoRef.current.editor.setTheme(props.theme)
reducerListener(props.plugin, dispatch, monacoRef.current, editorRef.current, props.events)
props.events.onEditorMounted() props.events.onEditorMounted()
editor.onMouseUp((e) => { editor.onMouseUp((e) => {
if (e && e.target && e.target.toString().startsWith('GUTTER')) { if (e && e.target && e.target.toString().startsWith('GUTTER')) {
...@@ -216,7 +217,6 @@ export const EditorUI = (props: EditorUIProps) => { ...@@ -216,7 +217,6 @@ export const EditorUI = (props: EditorUIProps) => {
function handleEditorWillMount (monaco) { function handleEditorWillMount (monaco) {
monacoRef.current = monaco monacoRef.current = monaco
reducerListener(props.plugin, dispatch, monacoRef.current, props.events)
// see https://microsoft.github.io/monaco-editor/playground.html#customizing-the-appearence-exposed-colors // see https://microsoft.github.io/monaco-editor/playground.html#customizing-the-appearence-exposed-colors
const backgroundColor = window.getComputedStyle(document.documentElement).getPropertyValue('--light').trim() const backgroundColor = window.getComputedStyle(document.documentElement).getPropertyValue('--light').trim()
const infoColor = window.getComputedStyle(document.documentElement).getPropertyValue('--info').trim() const infoColor = window.getComputedStyle(document.documentElement).getPropertyValue('--info').trim()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment