// ==UserScript==
// @name Overleaf Editor Custom VIM Keybindings (Code Mirror v6)
// @namespace http://tampermonkey.net/
// @version 0.0.3
// @match https://www.overleaf.com/project/*
// @grant none
// @description Configure a list of shortcuts for Vim-mode + user-defined :commands for toggling panels on Overleaf.
// @license MIT
// ==/UserScript==
(function() {
'use strict';
window.addEventListener("UNSTABLE_editor:extensions", (event) => {
const { CodeMirror, CodeMirrorVim, extensions } = event.detail;
// Mappings in insert mode
CodeMirrorVim.Vim.map("<c-]>", "<Esc>", "insert");
CodeMirrorVim.Vim.map("jj", "<Esc>", "insert");
CodeMirrorVim.Vim.map("jk", "<Esc>", "insert");
// Remap in normal mode
/// To navigate through wrapped long lines
CodeMirrorVim.Vim.map("$", "g$", "normal");
CodeMirrorVim.Vim.map("0", "g0", "normal");
CodeMirrorVim.Vim.map("j", "gj", "normal");
CodeMirrorVim.Vim.map("k", "gk", "normal");
// User-defined commands
CodeMirrorVim.Vim.defineEx("forward", undefined, buctton_click_forward_search); // ref: https://discuss.codemirror.net/t/vim-how-to-use-defineex/738/2
CodeMirrorVim.Vim.defineEx("pdf", undefined, buctton_click_toggle_pdf_panel);
CodeMirrorVim.Vim.defineEx("toc", undefined, buctton_click_toggle_TOC_left_panel);
CodeMirrorVim.Vim.defineEx("only", undefined, buctton_click_toggle_editor_only);
CodeMirrorVim.Vim.defineEx("o", undefined, buctton_click_toggle_editor_only); // :o as :o[nly]
CodeMirrorVim.Vim.defineEx("re", undefined, writefull_rephrase); // :re[phrase]
// Access the functions by defining them as actions first
CodeMirrorVim.Vim.defineAction('jumpToPdf', buctton_click_forward_search);
CodeMirrorVim.Vim.defineAction('only', buctton_click_toggle_editor_only);
CodeMirrorVim.Vim.defineAction('toc', buctton_click_toggle_TOC_left_panel);
CodeMirrorVim.Vim.defineAction('WritefullPrevious', writefull_previous_error);
CodeMirrorVim.Vim.defineAction('WritefullNext', writefull_next_error);
CodeMirrorVim.Vim.defineAction('WritefullRephrase', writefull_rephrase);
// forward search: from VimTeX, <localleader>lv
CodeMirrorVim.Vim.unmap(';');
CodeMirrorVim.Vim.mapCommand("\\lv", "action", "jumpToPdf");
CodeMirrorVim.Vim.mapCommand(";lv", 'action', 'jumpToPdf');
// Alternatively: use , as leader key
CodeMirrorVim.Vim.unmap(',');
CodeMirrorVim.Vim.mapCommand(",lv", 'action', 'jumpToPdf');
// quick toggles
CodeMirrorVim.Vim.mapCommand(",v", 'action', 'toc'); // <leader>v is from VimTeX
CodeMirrorVim.Vim.mapCommand(",o", 'action', 'only'); // <leader>o for :o[nly]
// Writefull shortcuts:
// Jump to suggested typo using Writefull, with [s and ]s
CodeMirrorVim.Vim.mapCommand("[s", 'action', 'WritefullPrevious');
CodeMirrorVim.Vim.mapCommand("]s", 'action', 'WritefullNext');
// Remap in visual mode - select a paragraph, press r to trigger the rephrase tool
CodeMirrorVim.Vim.mapCommand("r", "action", "WritefullRephrase", "visual");
// Next step:
// It would be nice to accept/reject the suggestion card using 1/a or 2/r, for example. Leave it for Writefull to build this for now (2023-03-02, 15:52)
// 2. Restore Ctrl+C to copy into system clipboard - Currently, on Windows laptops, Ctrl+V in visual mode won't do anything. Overleaf should fix this ASAP, though.
//CodeMirrorVim.Vim.defineAction('CopySelection', copy_in_visual_mode);
//CodeMirrorVim.Vim.mapCommand("<c-c>", 'action', 'CopySelection');
});
})();
// Collection of functions
// Clipboard access on Windows machines
function copy_in_visual_mode(cm) {
document.execCommand('copy');
alert("Triggered the copy function");
}
// Navigation releated functions
function buctton_click_forward_search(cm) {
document.querySelector('.fa-arrow-right').click()
};
function buctton_click_toggle_pdf_panel(cm) {
document.querySelector('a[class*="custom-toggler-east"]').click()
};
function buctton_click_toggle_TOC_left_panel(cm) {
document.querySelector('a[tooltip*="the file-tree"]').click()
};
function buctton_click_toggle_editor_only(cm) {
// This is just the sum of the two: toggle toc and toggle pdf
document.querySelector('a[class*="custom-toggler-east"]').click()
document.querySelector('a[tooltip*="the file-tree"]').click()
}
// Spell-checker specific functions
function writefull_next_error(cm) {
document.querySelector('mwc-icon-button[id="writefull-next"]').click()
}
function writefull_previous_error(cm) {
document.querySelector('mwc-icon-button[id="writefull-previous"]').click()
}
function writefull_rephrase(cm) {
document.querySelector('mwc-icon-button[id="writefull-rephrase"]').click()
}