Skip to content

Commit

Permalink
release: 0.38.10
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Mar 20, 2024
1 parent fb97eeb commit c563ed6
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 40 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.38.9",
"version": "0.38.10",
"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.38.9",
"version": "0.38.10",
"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.38.9",
"version": "0.38.10",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
26 changes: 25 additions & 1 deletion src/auto-copy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Menu } from 'obsidian';

import PDFPlus from 'main';
import { PDFPlusComponent } from 'lib/component';

Expand All @@ -8,12 +10,34 @@ export class AutoCopyMode extends PDFPlusComponent {
constructor(plugin: PDFPlus) {
super(plugin);
if (this.settings.autoCopyToggleRibbonIcon) {
let menuShown = false;

this.iconEl = plugin.settings.autoCopyToggleRibbonIcon
? plugin.addRibbonIcon(
this.settings.autoCopyIconName,
`${plugin.manifest.name}: Toggle auto-copy`,
() => this.toggle()
() => {
if (!menuShown) this.toggle();
}
) : null;

if (this.iconEl) {
this.registerDomEvent(this.iconEl, 'contextmenu', (evt) => {
if (menuShown) return;

const menu = new Menu();
menu.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.plugin.openSettingTab().scrollToHeading('auto-copy');
});
});
menu.onHide(() => { menuShown = false });
menu.showAtMouseEvent(evt);
menuShown = true;
});
}
}
}

Expand Down
148 changes: 129 additions & 19 deletions src/color-palette.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Menu, Notice, Platform, setIcon, setTooltip } from 'obsidian';

import PDFPlus from 'main';
import { KeysOfType, getEventCoords, isHexString } from 'utils';
import { KeysOfType, getEventCoords, isHexString, showMenuUnderParentEl } from 'utils';
import { PDFViewerChild, Rect } from 'typings';
import { PDFPlusComponent } from 'lib/component';

Expand Down Expand Up @@ -80,10 +80,10 @@ export class ColorPalette extends PDFPlusComponent {

this.addCropButton(this.paletteEl);

if (this.lib.isEditable(this.child)) {
this.addWriteFileToggle(this.paletteEl);
} else if (this.child.isFileExternal) {
if (this.child.isFileExternal) {
this.addImportButton(this.paletteEl);
} else {
this.addWriteFileToggle(this.paletteEl);
}

this.statusContainerEl = this.paletteEl.createDiv('pdf-plus-color-palette-status-container');
Expand Down Expand Up @@ -137,6 +137,27 @@ export class ColorPalette extends PDFPlusComponent {

evt.preventDefault();
});

let shown = false;
itemEl.addEventListener('contextmenu', () => {
if (shown) return;

const menu = new Menu()
.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.plugin.openSettingTab()
.scrollTo('colors');
});
});
menu.onHide(() => {
shown = false;
});

showMenuUnderParentEl(menu, itemEl);
shown = true;
});
}

setActiveItem(name: string | null) {
Expand All @@ -152,7 +173,10 @@ export class ColorPalette extends PDFPlusComponent {
setTooltip(buttonEl, tooltip);
buttonEl.dataset.checkedIndex = '' + this[checkedIndexKey];

let shown = false;
buttonEl.addEventListener('click', () => {
if (shown) return;

const menu = new Menu();

for (let i = 0; i < itemNames.length; i++) {
Expand All @@ -176,14 +200,12 @@ export class ColorPalette extends PDFPlusComponent {

beforeShowMenu?.(menu);

const { x, bottom, width } = buttonEl.getBoundingClientRect();
menu.setParentElement(buttonEl).showAtPosition({
x,
y: bottom,
width,
overlap: true,
left: false
menu.onHide(() => {
shown = false;
});

showMenuUnderParentEl(menu, buttonEl);
shown = true;
});
});
}
Expand Down Expand Up @@ -266,9 +288,33 @@ export class ColorPalette extends PDFPlusComponent {
this.removeWriteFileToggle();

this.writeFileButtonEl = paletteEl.createDiv('clickable-icon', (el) => {
setIcon(el, 'lucide-save');
setIcon(el, 'lucide-edit');
setTooltip(el, `${this.plugin.manifest.name}: Add ${this.plugin.settings.selectionBacklinkVisualizeStyle}s to file directly`);
el.toggleClass('is-disabled', !this.lib.isEditable(this.child));

let shown = false;
el.addEventListener('click', () => {
if (!this.lib.isEditable(this.child)) {
if (shown) return;

const menu = new Menu()
.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Enable PDF editing...')
.onClick(() => {
this.plugin.openSettingTab()
.scrollToHeading('edit');
});
});
menu.onHide(() => {
shown = false;
});

showMenuUnderParentEl(menu, el);
shown = true;
return;
}

this.setWriteFile(!this.writeFile);

if (this.plugin.settings.syncWriteFileToggle && this.plugin.settings.syncDefaultWriteFileToggle) {
Expand All @@ -277,6 +323,36 @@ export class ColorPalette extends PDFPlusComponent {

this.plugin.trigger('color-palette-state-change', { source: this });
});

el.addEventListener('contextmenu', () => {
if (shown) return;

const menu = new Menu()
.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle(this.lib.isEditable(this.child) ? 'Disable PDF editing...' : 'Enable PDF editing...')
.onClick(() => {
this.plugin.openSettingTab()
.scrollToHeading('edit');
});
});
if (this.lib.isEditable(this.child)) {
menu.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.plugin.openSettingTab()
.scrollToHeading('annot');
});
});
}
menu.onHide(() => {
shown = false;
});

showMenuUnderParentEl(menu, el);
shown = true;
});
});

if (this.cropButtonEl) {
Expand Down Expand Up @@ -344,7 +420,7 @@ export class ColorPalette extends PDFPlusComponent {
await this.app.vault.modifyBinary(file, buffer);

this.removeImportButton();
if (this.lib.isEditable(this.child) && this.paletteEl) {
if (this.paletteEl) {
this.addWriteFileToggle(this.paletteEl);
}
new Notice(`${this.plugin.manifest.name}: Successfully imported the PDF file into the vault.`);
Expand All @@ -367,6 +443,27 @@ export class ColorPalette extends PDFPlusComponent {
el.addEventListener('click', () => {
this.startRectangularSelection(false);
});

let shown = false;
el.addEventListener('contextmenu', () => {
if (shown) return;

const menu = new Menu()
.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.plugin.openSettingTab()
.scrollToHeading('rect');
});
});
menu.onHide(() => {
shown = false;
});

showMenuUnderParentEl(menu, el);
shown = true;
});
});
}

Expand Down Expand Up @@ -436,16 +533,22 @@ export class ColorPalette extends PDFPlusComponent {
pageEl.removeEventListener('touchend', onMouseUp);
pageEl.removeChild(boxEl);

// Discard empty selections
if (selectBox.height <= 0 || selectBox.width <= 0) return;

// Get the screen coordinates of the selection box relative to the page
const left = selectBox.left - (pageRect.left + borderLeft + paddingLeft);
const top = selectBox.top - (pageRect.top + borderTop + paddingTop);
const right = left + selectBox.width;
const bottom = top + selectBox.height;

// Convert screen coordinates to PDF coordinates
const rect = window.pdfjsLib.Util.normalizeRect([
...pageView.getPagePoint(left, bottom),
...pageView.getPagePoint(right, top)
]) as Rect;

// Copy an embed link to the selection
this.lib.copyLink.copyEmbedLinkToRect(
false, child, pageView.id, rect,
this.plugin.settings.includeColorWhenCopyingRectLink
Expand All @@ -456,21 +559,28 @@ export class ColorPalette extends PDFPlusComponent {
toggle();
};

pageEl.addEventListener('mousemove', onMouseMove);
pageEl.addEventListener('touchmove', onMouseMove);
pageEl.addEventListener('mouseup', onMouseUp);
pageEl.addEventListener('touchend', onMouseUp);
// `pageEl` is not a part of this component, so just `pageEl.addEventListener` & `pageEl.removeEventListener`is not enough.
// We have to explicitly remove the event listeners not just when the selection is done, but also
// when this component gets unloaded.
this.registerDomEvent(pageEl, 'mousemove', onMouseMove);
this.registerDomEvent(pageEl, 'touchmove', onMouseMove);
this.registerDomEvent(pageEl, 'mouseup', onMouseUp);
this.registerDomEvent(pageEl, 'touchend', onMouseUp);
};

const toggle = () => {
cropButtonEl.toggleClass('is-active', !cropButtonEl.hasClass('is-active'));
viewerEl.toggleClass('pdf-plus-selecting', cropButtonEl.hasClass('is-active'));
this.register(() => viewerEl.removeClass('pdf-plus-selecting'));

activeWindow.getSelection()?.empty();

if (cropButtonEl.hasClass('is-active')) {
viewerEl.addEventListener('mousedown', onMouseDown);
viewerEl.addEventListener('touchstart', onMouseDown);
// `viewerEl` is not a part of this component, so just `viewerEl.addEventListener` & `viewerEl.removeEventListener`is not enough.
// We have to explicitly remove the event listeners not just when the selection is done, but also
// when this component gets unloaded.
this.registerDomEvent(viewerEl, 'mousedown', onMouseDown);
this.registerDomEvent(viewerEl, 'touchstart', onMouseDown);
} else {
viewerEl.removeEventListener('mousedown', onMouseDown);
viewerEl.removeEventListener('touchstart', onMouseDown);
Expand Down
41 changes: 38 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class PDFPlus extends Plugin {

this.startTrackingActiveMarkdownFile();

this.registerObsidianProtocolHandler('pdf-plus', this.obsidianProtocolHandler.bind(this));
this.registerObsidianProtocolHandler('pdf-plus', this.obsidianProtocolHandler.bind(this));
}

private checkVersion() {
Expand Down Expand Up @@ -201,17 +201,52 @@ export default class PDFPlus extends Plugin {
this.register(() => this.autoCopyMode.unload());

if (this.settings.autoFocusToggleRibbonIcon) {
let menuShown = false;

this.autoFocusToggleIconEl = this.addRibbonIcon(this.settings.autoFocusIconName, `${this.manifest.name}: Toggle auto-focus`, () => {
this.toggleAutoFocus();
if (!menuShown) this.toggleAutoFocus();
});
this.autoFocusToggleIconEl.toggleClass('is-active', this.settings.autoFocus);

this.registerDomEvent(this.autoFocusToggleIconEl, 'contextmenu', (evt) => {
if (menuShown) return;

const menu = new Menu();
menu.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.openSettingTab().scrollToHeading('auto-focus');
});
});
menu.onHide(() => { menuShown = false });
menu.showAtMouseEvent(evt);
menuShown = true;
});
}

if (this.settings.autoPasteToggleRibbonIcon) {
let menuShown = false;

this.autoPasteToggleIconEl = this.addRibbonIcon(this.settings.autoPasteIconName, `${this.manifest.name}: Toggle auto-paste`, () => {
this.toggleAutoPaste();
if (!menuShown) this.toggleAutoPaste();
});
this.autoPasteToggleIconEl.toggleClass('is-active', this.settings.autoPaste);
this.registerDomEvent(this.autoPasteToggleIconEl, 'contextmenu', (evt) => {
if (menuShown) return;

const menu = new Menu();
menu.addItem((item) => {
item.setIcon('lucide-settings')
.setTitle('Customize...')
.onClick(() => {
this.openSettingTab().scrollToHeading('auto-paste');
});
});
menu.onHide(() => { menuShown = false });
menu.showAtMouseEvent(evt);
menuShown = true;
});
}
}

Expand Down
Loading

0 comments on commit c563ed6

Please sign in to comment.