Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support vim mode #35

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 22 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 { createStore, Provider, useAtom } from 'jotai'
import { createStore, Provider, 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 @@ -19,7 +20,7 @@ import { classnames, isMacOS } from '../utils'
import { Popover } from './base/Popover'
import { BottomStatus } from './BottomStatus'
import { DrawerPanel } from './DrawerPanel'
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 @@ -114,6 +115,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 @@ -151,6 +155,22 @@ 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
}

return () => {
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)
7 changes: 7 additions & 0 deletions core/src/components/LeftBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import './LeftBar.scss'

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

import PP from '../../../resources/PP_P.svg'
import { ExtensionContext } from '../contextes/Extension'

import { Tooltip } from './base/Tooltip'
import { useDrawerPanelController } from './drawerPanelCreator'
import { isVimModeAtom } from './EditorZoneShareAtoms'
import { NotImplemented } from './NotImplemented'

const prefix = 'ppd-left-bar'
Expand Down Expand Up @@ -57,6 +59,7 @@ export function LeftBar(props: LeftBarProps) {
</Tooltip>
: btn
})
const toggleVimMode = useSetAtom(isVimModeAtom)

return <div className={classnames(prefix, props.className)}
style={props.style}>
Expand All @@ -80,6 +83,10 @@ export function LeftBar(props: LeftBarProps) {
</button>
</div>
<div className={`${prefix}__bottom`}>
{/* TODO: move to settings */}
<button onClick={() => toggleVimMode(mode => !mode)}>
Vim Mode
</button>
{buildElements(bottomItems)}
<button onClick={() => messenger.then(m => m.display('warning', <NotImplemented />))}>
<span className='cldr codicon codicon-account'></span>
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 }
}