Skip to content

Commit

Permalink
feat: support vim mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kahosan committed Sep 4, 2023
1 parent 70c8781 commit 2e143c2
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 211 deletions.
1 change: 1 addition & 0 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@popperjs/core": "^2.11.8",
"foxact": "^0.2.20",
"jotai": "^2.3.1",
"monaco-vim": "^0.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
19 changes: 17 additions & 2 deletions core/src/components/EditorZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import React, {
useState
} from 'react'
import Editor, { useMonaco } from '@monaco-editor/react'
import { useAtom } from 'jotai'
import { useAtom, useAtomValue } from 'jotai'
import type * as monacoEditor from 'monaco-editor'
import { initVimMode } from 'monaco-vim'

import { ExtensionContext } from '../contextes/Extension'
import { MonacoScopeContext } from '../contextes/MonacoScope'
Expand All @@ -18,7 +19,7 @@ import { classnames, isMacOS } from '../utils'

import { Popover } from './base/Popover'
import { BottomStatus } from './BottomStatus'
import { displayLeftBarAtom } from './EditorZoneShareAtoms'
import { displayLeftBarAtom, isVimModeAtom } from './EditorZoneShareAtoms'
import { LeftBar } from './LeftBar'
import type { ResizableProps } from './Resizable'
import { Resizable } from './Resizable'
Expand Down Expand Up @@ -113,6 +114,9 @@ export default function EditorZone(props: EditorZoneProps) {

const [displayLeftBar, setDisplayLeftBar] = useAtom(displayLeftBarAtom)

const isVimMode = useAtomValue(isVimModeAtom)
const vimModeRef = useRef<ReturnType<typeof initVimMode> | null>(null)

const editorCursorPosition = useRef<monacoEditor.Position | null>(null)

useEffect(() => {
Expand Down Expand Up @@ -150,6 +154,17 @@ export default function EditorZone(props: EditorZoneProps) {
return () => dispose.forEach(func => func?.())
}, [monaco, editor, plugins])

useEffect(() => {
if (!editor) return

if (isVimMode)
vimModeRef.current = initVimMode(editor, null!)
else {
vimModeRef.current?.dispose()
vimModeRef.current = null
}
}, [editor, isVimMode])

useDocumentEventListener('keydown', e => {
if (e.key === '\\' && (e.metaKey || e.ctrlKey)) {
setDisplayLeftBar(!displayLeftBar)
Expand Down
2 changes: 2 additions & 0 deletions core/src/components/EditorZoneShareAtoms.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { atom } from 'jotai'
import { atomWithStorage } from 'jotai/utils'

export const displayLeftBarAtom = atom(false)
export const isVimModeAtom = atomWithStorage('IS_VIM_MODE', false)
8 changes: 8 additions & 0 deletions core/src/components/LeftBar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import './LeftBar.scss'

import { classnames, messenger } from '@power-playground/core'
import { useSetAtom } from 'jotai'

import PP from '../../../resources/PP_P.svg'

import { isVimModeAtom } from './EditorZoneShareAtoms'

const prefix = 'ppd-left-bar'

export interface LeftBarProps {
Expand All @@ -12,6 +15,7 @@ export interface LeftBarProps {
}

export function LeftBar(props: LeftBarProps) {
const toggleVimMode = useSetAtom(isVimModeAtom)
return <div className={classnames(prefix, props.className)}
style={props.style}>
<div className={`${prefix}__top`}>
Expand All @@ -37,6 +41,10 @@ export function LeftBar(props: LeftBarProps) {
</button>
</div>
<div className={`${prefix}__bottom`}>
{/* TODO: move to settings */}
<button onClick={() => toggleVimMode(mode => !mode)}>
Vim Mode
</button>
<button onClick={() => messenger.then(m => m.display('warning', <>Not implemented yet, it will come soon, <a href='https://github.com/Power-Playground/app/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22' target='_blank' rel='noreferrer'>help us</a></>))}>
<span className='cldr codicon codicon-account'></span>
</button>
Expand Down
21 changes: 21 additions & 0 deletions core/src/vim-mode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module "monaco-vim" {
export interface EditorVimMode {
dispose: () => void;
}

type initVimModeFn = (
editor: monaco.editor.IStandaloneCodeEditor,
statusElm: HTMLElement
) => EditorVimMode;

const initVimMode: initVimModeFn
export { initVimMode }

const VimMode: {
Vim: {
noremap: (from: string, to: string) => void;
map: (from: string, to: string, mode: string) => void;
};
}
export { VimMode }
}
Loading

0 comments on commit 2e143c2

Please sign in to comment.