Skip to content

Commit

Permalink
Add compatibility with CodeMirror6
Browse files Browse the repository at this point in the history
  • Loading branch information
ALONELUR committed Jan 5, 2022
1 parent 6dd17cc commit ab34dfb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
59 changes: 53 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { App, Plugin, PluginSettingTab, Setting, MarkdownView } from 'obsidian';

interface VimImPluginSettings {
defaultIM: string;
Expand All @@ -46,15 +46,33 @@ export default class VimImPlugin extends Plugin {
private currentInsertIM = '';
private isWinPlatform = false;

private initialized = false;
private editorMode: 'cm5' | 'cm6' = null;

async onload() {
await this.loadSettings();
this.app.workspace.on('codemirror', (cm: CodeMirror.Editor) => {
cm.on('vim-mode-change', (modeObj: any) => {
if (modeObj)
this.onVimModeChanged(modeObj);
});

// when open a file, to initialize current
// editor type CodeMirror5 or CodeMirror6
this.app.workspace.on('file-open', async (_file) => {
if (!this.initialized)
await this.initialize();

let view = this.getActiveView();
if (view) {
var editor = this.getCodeMirror(view);

if (editor) {
editor.on('vim-mode-change', (modeObj: any) => {
if (modeObj) {
this.onVimModeChanged(modeObj);
}
});
}
}
});


// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this));

Expand All @@ -68,6 +86,35 @@ export default class VimImPlugin extends Plugin {
}
}

async initialize() {
if (this.initialized)
return;

// Determine if we have the legacy Obsidian editor (CM5) or the new one (CM6).
// This is only available after Obsidian is fully loaded, so we do it as part of the `file-open` event.
if ('editor:toggle-source' in (this.app as any).commands.editorCommands) {
this.editorMode = 'cm6';
console.log('Vimrc plugin: using CodeMirror 6 mode');
} else {
this.editorMode = 'cm5';
console.log('Vimrc plugin: using CodeMirror 5 mode');
}

this.initialized = true;
}

private getActiveView(): MarkdownView {
return this.app.workspace.getActiveViewOfType(MarkdownView);
}

private getCodeMirror(view: MarkdownView): CodeMirror.Editor {
// For CM6 this actually returns an instance of the object named CodeMirror from cm_adapter of codemirror_vim
if (this.editorMode == 'cm6')
return (view as any).sourceMode?.cmEditor?.cm?.cm;
else
return (view as any).sourceMode?.cmEditor;
}

onVimModeChanged(modeObj: any) {
const { exec } = require('child_process');
let switchToInsert: string;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "vim-im-select",
"name": "Vim IM Select",
"version": "0.0.2",
"version": "0.1.0",
"minAppVersion": "0.12.0",
"description": "Support auto select the apposite input method in different vim mode",
"author": "Alonelur",
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.0.1": "0.12.0",
"0.0.2": "0.12.0"
"0.0.2": "0.12.0",
"0.1.0": "0.12.0"
}

0 comments on commit ab34dfb

Please sign in to comment.