-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrades and fix cursor selection after changing editor options
- Loading branch information
1 parent
fc972a3
commit ce14c2d
Showing
15 changed files
with
341 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
services/app/apps/codebattle/assets/js/widgets/initEditor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { loader } from '@monaco-editor/react'; | ||
import * as monacoLib from 'monaco-editor'; | ||
|
||
import haskellProvider from './config/editor/haskell'; | ||
import sassProvider from './config/editor/sass'; | ||
import stylusProvider from './config/editor/stylus'; | ||
|
||
// const monacoVersion = '0.52.0'; | ||
|
||
loader.config({ | ||
monaco: monacoLib, | ||
// paths: { | ||
// vs: `https://cdn.jsdelivr.net/npm/monaco-editor@${monacoVersion}/min/vs`, | ||
// }, | ||
}); | ||
|
||
loader.init().then(monaco => { | ||
monaco.languages.register({ id: 'haskell', aliases: ['haskell'] }); | ||
monaco.languages.setMonarchTokensProvider('haskell', haskellProvider); | ||
|
||
monaco.languages.register({ id: 'stylus', aliases: ['stylus'] }); | ||
monaco.languages.setMonarchTokensProvider('stylus', stylusProvider); | ||
|
||
monaco.languages.register({ id: 'scss', aliases: ['scss'] }); | ||
monaco.languages.setMonarchTokensProvider('scss', sassProvider); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
services/app/apps/codebattle/assets/js/widgets/utils/useCursorUpdates.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
useMemo, useState, useEffect, useCallback, | ||
} from 'react'; | ||
|
||
import pick from 'lodash/pick'; | ||
|
||
import editorUserTypes from '../config/editorUserTypes'; | ||
import * as RoomActions from '../middlewares/Room'; | ||
|
||
const useCursorUpdates = (editor, monaco, props) => { | ||
const params = useMemo( | ||
() => pick(props, ['userId', 'roomMode']), | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[props.userId, props.roomMode], | ||
); | ||
const [, setRemoteKeys] = useState([]); | ||
const [remote, setRemote] = useState({ | ||
cursor: {}, | ||
selection: {}, | ||
}); | ||
|
||
const updateRemoteCursorPosition = useCallback(offset => { | ||
const { readOnly, userType } = editor.getRawOptions(); | ||
|
||
const position = editor.getModel().getPositionAt(offset); | ||
const userClassName = userType === editorUserTypes.opponent | ||
? 'cb-remote-opponent' | ||
: 'cb-remote-player'; | ||
|
||
if (readOnly) { | ||
const cursor = { | ||
range: new monaco.Range( | ||
position.lineNumber, | ||
position.column, | ||
position.lineNumber, | ||
position.column, | ||
), | ||
options: { className: `cb-editor-remote-cursor ${userClassName}` }, | ||
}; | ||
|
||
setRemote(oldRemote => ({ | ||
...oldRemote, | ||
cursor, | ||
})); | ||
} | ||
}, [setRemote, editor, monaco]); | ||
|
||
const updateRemoteCursorSelection = useCallback((startOffset, endOffset) => { | ||
const { readOnly, userType } = editor.getRawOptions(); | ||
|
||
const userClassName = userType === editorUserTypes.opponent | ||
? 'cb-remote-opponent' | ||
: 'cb-remote-player'; | ||
|
||
if (readOnly) { | ||
const startPosition = editor.getModel().getPositionAt(startOffset); | ||
const endPosition = editor.getModel().getPositionAt(endOffset); | ||
|
||
const startColumn = startPosition.column; | ||
const startLineNumber = startPosition.lineNumber; | ||
const endColumn = endPosition.column; | ||
const endLineNumber = endPosition.lineNumber; | ||
|
||
const selection = { | ||
range: new monaco.Range( | ||
startLineNumber, | ||
startColumn, | ||
endLineNumber, | ||
endColumn, | ||
), | ||
options: { className: `cb-editor-remote-selection ${userClassName}` }, | ||
}; | ||
|
||
setRemote(prevRemote => ({ | ||
...prevRemote, | ||
selection, | ||
})); | ||
} | ||
}, [setRemote, editor, monaco]); | ||
|
||
useEffect(() => { | ||
if (remote.cursor.range && remote.selection.range) { | ||
setRemoteKeys(oldRemoteKeys => ( | ||
editor.deltaDecorations(oldRemoteKeys, Object.values(remote)) | ||
)); | ||
} | ||
}, [editor, remote, setRemoteKeys]); | ||
|
||
useEffect(() => { | ||
const clearCursorListeners = RoomActions.addCursorListeners( | ||
params, | ||
updateRemoteCursorPosition, | ||
updateRemoteCursorSelection, | ||
); | ||
|
||
return clearCursorListeners; | ||
}, [ | ||
params, | ||
updateRemoteCursorPosition, | ||
updateRemoteCursorSelection, | ||
]); | ||
}; | ||
|
||
export default useCursorUpdates; |
Oops, something went wrong.