Skip to content

Commit

Permalink
release: 0.40.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed May 29, 2024
1 parent b1097e1 commit 22fd79a
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 72 deletions.
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "pdf-plus",
"name": "PDF++",
"version": "0.39.26",
"version": "0.40.0",
"minAppVersion": "1.4.16",
"description": "The most Obsidian-native PDF annotation tool ever.",
"author": "Ryota Ushio",
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": "pdf-plus",
"name": "PDF++",
"version": "0.39.26",
"version": "0.40.0",
"minAppVersion": "1.4.16",
"description": "The most Obsidian-native PDF annotation tool ever.",
"author": "Ryota Ushio",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-pdf-plus",
"version": "0.39.26",
"version": "0.40.0",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
7 changes: 3 additions & 4 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2900,7 +2900,8 @@ export class PDFPlusSettingTab extends PluginSettingTab {
`- \`c\`: Run the "${this.plugin.lib.commands.stripCommandNamePrefix(this.plugin.lib.commands.getCommand('copy-link-to-selection').name)}" command`,
'- `C`: Show context menu at text selection',
'- `:`: Enter command-line mode (experimental)',
'- `<Tab>`: Open outline (table of contents)',
'- `<Tab>`: Toggle outline (table of contents)',
'- `<S-Tab>`: Toggle thumbnails (`S`=`Shift`)',
'- `<Esc>`: Go back to normal mode, abort search, etc',
'',
'Many of them can be combined with counts. For example:',
Expand Down Expand Up @@ -2929,11 +2930,9 @@ export class PDFPlusSettingTab extends PluginSettingTab {
'- `l`: Expand entry & move to child entry',
'- `H`: Collapse all entries',
'- `L`: Expand all entries',
'- `<CR>/<Space>`: Open the selected entry',
'- `<CR>/<Space>`: Open the selected entry (`<CR>`=`Enter`)',
'- `<Esc>`: Close sidebar and go back to normal mode',
'',
'(`<CR>` means the Enter key.)',
'',
'If disabled, you can use j/k/h/l/H/L keys to scroll the page whether the outline view is opened or not. ',
'This option requires reload to take effect.'
], setting.descEl);
Expand Down
5 changes: 3 additions & 2 deletions src/vim/outline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ type OutlineCommand = (outline: PDFOutlineViewer, n?: number) => any;
export class VimOutlineMode extends VimBindingsMode {
constructor(vim: VimBindings) {
super(vim);
this.defineKeymaps();
}

onload() {
this.viewer.then((child) => {
this.lib.registerPDFEvent('sidebarviewchanged', child.pdfViewer.eventBus, this, ({ view }) => {
if (view === SidebarView.OUTLINE) {
Expand All @@ -28,8 +31,6 @@ export class VimOutlineMode extends VimBindingsMode {
}
});
});

this.defineKeymaps();
}

defineKeymaps() {
Expand Down
90 changes: 50 additions & 40 deletions src/vim/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class VimScope extends Scope {
currentKeys: string = '';
searchFrom = 0;
searchTo = -1
onEscapeCallbacks: (() => any)[] = [];
onEscapeCallbacks: ((isRealEscape: boolean) => any)[] = [];
escapeAliases: string[] = [];

constructor(parent?: Scope) {
super(parent);
Expand Down Expand Up @@ -74,10 +75,14 @@ export class VimScope extends Scope {
this.searchTo = -1;
}

onEscape(callback: () => any) {
onEscape(callback: (isRealEscape: boolean) => any) {
this.onEscapeCallbacks.push(callback);
}

addEscapeAliases(...aliases: string[]) {
this.escapeAliases.push(...aliases);
}

handleKey(evt: KeyboardEvent, info: KeymapInfo) {
let shouldCallParent = true;

Expand All @@ -88,12 +93,12 @@ export class VimScope extends Scope {

if ((this.currentMode === 'insert') !== isTargetTypable(evt)) return;

const key = this.canonicalizeKey(info);
const key = VimScope.canonicalizeKey(info);
if (key === null) {
return this.reset();
}
if (key === '<Esc>') {
this.onEscapeCallbacks.forEach((callback) => callback());
if (key === '<Esc>' || this.escapeAliases.includes(key)) {
this.onEscapeCallbacks.forEach((callback) => callback(key === '<Esc>'));
return this.reset();
}
this.currentKeys += key;
Expand Down Expand Up @@ -133,50 +138,55 @@ export class VimScope extends Scope {
if (shouldCallParent && this.parent) this.parent.handleKey(evt, info);
}

canonicalizeKey(info: KeymapInfo): string | null {
static canonicalizeKey(info: KeymapInfo): string | null {
if (info.modifiers === null || info.key === null) return null;

const result = VimScope.canonicalizeSpecialKey(info.key);

switch (info.modifiers) {
case '':
switch (info.key) {
case '<':
return '<lt>';
case 'Backspace':
return '<BS>';
case 'Tab':
return '<Tab>';
case 'Enter':
return '<CR>';
case 'Escape':
return '<Esc>';
case ' ':
return '<Space>';
case '\\':
return '<Bslash>';
case '|':
return '<Bar>';
case 'ArrowUp':
return '<Up>';
case 'ArrowDown':
return '<Down>';
case 'ArrowLeft':
return '<Left>';
case 'ArrowRight':
return '<Right>';
default:
return info.key;
}
return result ? `<${result}>` : info.key;
case 'Shift':
if (info.key.length === 1) return info.key;
if (info.key.startsWith('Arrow')) return `<S-${info.key.slice(5)}>`;
return `<S-${info.key}>`;
if (info.key.length === 1 && info.key !== ' ') return info.key;
return `<S-${result ?? info.key}>`;
case 'Ctrl':
return `<C-${info.key}>`;
return `<C-${result ?? info.key}>`;
case 'Alt':
// @ts-ignore
return `<M-${info.vkey.toLowerCase()}>`;
return `<M-${VimScope.canonicalizeSpecialKey(info.vkey) ?? info.vkey.toLowerCase()}>`;
case 'Meta':
return `<M-${info.key}>`;
return `<M-${result ?? info.key}>`;
default:
return null;
}
}

static canonicalizeSpecialKey(key: string) {
switch (key) {
case '<':
return 'lt';
case 'Backspace':
return 'BS';
case 'Tab':
return 'Tab';
case 'Enter':
return 'CR';
case 'Escape':
return 'Esc';
case ' ':
return 'Space';
case '\\':
return 'Bslash';
case '|':
return 'Bar';
case 'ArrowUp':
return 'Up';
case 'ArrowDown':
return 'Down';
case 'ArrowLeft':
return 'Left';
case 'ArrowRight':
return 'Right';
default:
return null;
}
Expand Down
52 changes: 37 additions & 15 deletions src/vim/vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,35 @@ export class VimBindings extends PDFPlusComponent {

constructor(plugin: PDFPlus, viewer: PDFViewerComponent) {
super(plugin);

this.viewer = viewer;

this.vimScope = new VimScope(this.viewer.scope);

this.vimScope.registerKeymaps(['normal', 'visual', 'outline'], {
':': () => this.enterCommandMode(),
'<Tab>': () => {
if (this.obsidianViewer) {
const sidebar = this.obsidianViewer.pdfSidebar;
if (sidebar.isOpen && sidebar.active === SidebarView.OUTLINE) {
sidebar.close();
} else {
sidebar.switchView(SidebarView.OUTLINE, true);
}
}
},
'<S-Tab>': () => {
if (this.obsidianViewer) {
const sidebar = this.obsidianViewer.pdfSidebar;
if (sidebar.isOpen && sidebar.active === SidebarView.THUMBS) {
sidebar.close();
} else {
sidebar.switchView(SidebarView.THUMBS, true);
}
}
},
});

this.vimScope.registerKeymaps(['normal', 'visual'], {
this.vimScope.registerKeymaps(['normal', 'visual', 'outline'], {
'j': (n) => this.scroll.scrollTo('down', n),
'k': (n) => this.scroll.scrollTo('up', n),
'h': (n) => this.scroll.scrollTo('left', n),
Expand All @@ -77,18 +97,8 @@ export class VimBindings extends PDFPlusComponent {
this.obsidianViewer?.zoomReset();
},
'r': (n) => this.obsidianViewer?.rotatePages(90 * (n ?? 1)),
'<Tab>': () => {
if (this.obsidianViewer) {
const sidebar = this.obsidianViewer.pdfSidebar;
if (sidebar.isOpen && sidebar.active === SidebarView.OUTLINE) {
sidebar.close();
} else {
sidebar.switchView(SidebarView.OUTLINE, true);
}
}
},
});
this.vimScope.map(['normal', 'visual'], {
this.vimScope.map(['normal', 'visual', 'outline'], {
'H': '^',
'L': '$',
'zi': '+',
Expand All @@ -97,10 +107,22 @@ export class VimBindings extends PDFPlusComponent {
});

this.vimScope.setMode('normal');
this.vimScope.onEscape(() => {

this.vimScope.onEscape((isRealEscape) => {
this.enterNormalMode();
this.obsidianViewer?.pdfSidebar.close();
this.obsidianViewer?.pdfSidebar.close();

if (!isRealEscape) {
// The following is registered as a keymap event handler of
// the original scope of the PDF view in `PDFViewerChild.load`.
// Since this is a fake escape, we need to manually do the same thing here.
this.viewer.then((child) => {
child.clearEphemeralUI();
child.findBar.close();
});
}
});
this.vimScope.addEscapeAliases('<C-[>', '<C-c>');

this.scroll = new ScrollController(this);
this.search = new VimSearch(this);
Expand Down
12 changes: 6 additions & 6 deletions src/vim/visual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ export class VimVisualMode extends VimBindingsMode {
selectionChangedByVisualMotion = false;

get structureParser() {
return this.vim.structureParser;
return this.vim.structureParser;
}

constructor(vim: VimBindings) {
super(vim);
this.defineKeymaps();
}

onload() {
// Watch selection change to switch between normal and visual mode
const doc = this.vim.doc;
this.registerDomEvent(doc, 'selectionchange', () => {
const selection = doc.getSelection();
this.registerDomEvent(this.doc, 'selectionchange', () => {
const selection = this.doc.getSelection();
switch (this.vim.vimScope.currentMode) {
case 'visual':
if (!selection || selection.isCollapsed) {
Expand All @@ -37,8 +39,6 @@ export class VimVisualMode extends VimBindingsMode {
}
this.selectionChangedByVisualMotion = false;
});

this.defineKeymaps();
}

defineKeymaps() {
Expand Down

0 comments on commit 22fd79a

Please sign in to comment.