Skip to content

Commit

Permalink
release: 0.38.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Mar 14, 2024
1 parent 4bfe762 commit e667aed
Show file tree
Hide file tree
Showing 17 changed files with 719 additions and 212 deletions.
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.37.16",
"version": "0.38.0-beta",
"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.37.16",
"version": "0.38.0-beta",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
103 changes: 91 additions & 12 deletions src/color-palette.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { App, Component, Menu, setIcon, setTooltip } from 'obsidian';
import { Menu, Notice, Platform, setIcon, setTooltip } from 'obsidian';

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


export type ColorPaletteState = Pick<ColorPalette, 'selectedColorName' | 'actionIndex' | 'displayTextFormatIndex' | 'writeFile'>;

export class ColorPalette extends Component {
export class ColorPalette extends PDFPlusComponent {
static readonly CLS = 'pdf-plus-color-palette';
/** Maps a paletteEl to the corresponding ColorPalette instance */
static elInstanceMap = new Map<HTMLElement, ColorPalette>();

app: App;
lib: PDFPlusLib;
child: PDFViewerChild;

toolbarLeftEl: HTMLElement;

spacerEl: HTMLElement | null;
paletteEl: HTMLElement | null;
Expand All @@ -25,17 +26,18 @@ export class ColorPalette extends Component {
cropButtonEl: HTMLElement | null;
statusContainerEl: HTMLElement | null;
statusEl: HTMLElement | null;
importButtonEl: HTMLElement | null;

/** The state of a color palette is specified by a 4-tuple consisting of the following. */
selectedColorName: string | null;
actionIndex: number;
displayTextFormatIndex: number;
writeFile: boolean;

constructor(public plugin: PDFPlus, public toolbarLeftEl: HTMLElement) {
super();
this.app = plugin.app;
this.lib = plugin.lib;
constructor(plugin: PDFPlus, child: PDFViewerChild, toolbarLeftEl: HTMLElement) {
super(plugin);
this.child = child;
this.toolbarLeftEl = toolbarLeftEl;

this.spacerEl = null;
this.paletteEl = null;
Expand All @@ -46,11 +48,12 @@ export class ColorPalette extends Component {
this.cropButtonEl = null;
this.statusContainerEl = null;
this.statusEl = null;
this.importButtonEl = null;

this.selectedColorName = null;
this.actionIndex = plugin.settings.defaultColorPaletteActionIndex;
this.displayTextFormatIndex = plugin.settings.defaultDisplayTextFormatIndex;
this.writeFile = plugin.settings.enablePDFEdit && plugin.settings.defaultWriteFileToggle;
this.writeFile = this.lib.isEditable(this.child) && plugin.settings.defaultWriteFileToggle;
}

onload() {
Expand All @@ -77,8 +80,10 @@ export class ColorPalette extends Component {

this.addCropButton(this.paletteEl);

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

this.statusContainerEl = this.paletteEl.createDiv('pdf-plus-color-palette-status-container');
Expand Down Expand Up @@ -258,6 +263,8 @@ export class ColorPalette extends Component {
}

addWriteFileToggle(paletteEl: HTMLElement) {
this.removeWriteFileToggle();

this.writeFileButtonEl = paletteEl.createDiv('clickable-icon', (el) => {
setIcon(el, 'lucide-save');
setTooltip(el, `${this.plugin.manifest.name}: Add ${this.plugin.settings.selectionBacklinkVisualizeStyle}s to file directly`);
Expand All @@ -271,6 +278,78 @@ export class ColorPalette extends Component {
this.plugin.trigger('color-palette-state-change', { source: this });
});
});

if (this.cropButtonEl) {
paletteEl.insertAfter(this.writeFileButtonEl, this.cropButtonEl);
}
}

removeWriteFileToggle() {
this.writeFileButtonEl?.remove();
this.writeFileButtonEl = null;
}

addImportButton(paletteEl: HTMLElement) {
this.removeImportButton();

this.importButtonEl = paletteEl.createDiv('clickable-icon', (el) => {
setIcon(el, 'lucide-import');
setTooltip(el, `${this.plugin.manifest.name}: Import PDF into vault`);
el.addEventListener('click', () => {
this.importFile();
});
});

if (this.cropButtonEl) {
paletteEl.insertAfter(this.importButtonEl, this.cropButtonEl);
}
}

removeImportButton() {
this.importButtonEl?.remove();
this.importButtonEl = null;
}

async importFile() {
const url = this.child.externalFileUrl;
const file = this.child.file;
if (!url || !file) return;

if (!Platform.isDesktopApp && url.startsWith(Platform.resourcePathPrefix)) {
new Notice(`${this.plugin.manifest.name}: Importing local PDFs outside the vault is supported only on the desktop app.`);
return;
}
// if (url.startsWith('https://') || url.startsWith('http://')) {
// const res = await requestUrl(url);
// if (res.status === 200 && res.headers['content-type'] === 'application/pdf') {
// await this.app.vault.modifyBinary(file, res.arrayBuffer);
// success = true;
// } else {
// new Notice(`${this.plugin.manifest.name}: Import failed because the URL is invalid or the file is not a PDF.`);
// }
// } else if (url.startsWith(Platform.resourcePathPrefix)) {
// if (Platform.isDesktopApp) { // `fs` is only availabel in the desktop app
// const buffer = readFileSync(url.slice(Platform.resourcePathPrefix.length));
// await this.app.vault.modifyBinary(file, buffer);
// success = true;
// } else {
// new Notice(`${this.plugin.manifest.name}: Importing local PDFs outside the vault is supported only on the desktop app.`);
// }

const res = await fetch(url);
if (res.ok) {
const buffer = await res.arrayBuffer();
await this.app.vault.modifyBinary(file, buffer);

this.removeImportButton();
if (this.lib.isEditable(this.child) && this.paletteEl) {
this.addWriteFileToggle(this.paletteEl);
}
new Notice(`${this.plugin.manifest.name}: Successfully imported the PDF file into the vault.`);
return;
}

new Notice(`${this.plugin.manifest.name}: Import failed. Response status: ${res.status}`);
}

setWriteFile(value: boolean) {
Expand Down
14 changes: 7 additions & 7 deletions src/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const onContextMenu = async (plugin: PDFPlus, child: PDFViewerChild, evt:
}

export const onThumbnailContextMenu = (plugin: PDFPlus, child: PDFViewerChild, evt: MouseEvent): void => {
const { lib, settings } = plugin;
const { lib } = plugin;

const node = evt.targetNode;
if (node && node.instanceOf(HTMLElement) && node.hasClass('thumbnail') && node.dataset.pageNumber !== undefined) {
Expand All @@ -64,7 +64,7 @@ export const onThumbnailContextMenu = (plugin: PDFPlus, child: PDFViewerChild, e
})
});

if (settings.enablePDFEdit) {
if (lib.isEditable(child)) {
menu
.addItem((item) => {
item.setTitle('Insert page before this page')
Expand Down Expand Up @@ -164,7 +164,7 @@ export const onOutlineItemContextMenu = (plugin: PDFPlus, child: PDFViewerChild,
})
});

if (plugin.settings.enablePDFEdit) {
if (lib.isEditable(child)) {
menu.addItem((menuItem) => {
menuItem
.setTitle('Add subitem')
Expand Down Expand Up @@ -333,7 +333,7 @@ export const onOutlineItemContextMenu = (plugin: PDFPlus, child: PDFViewerChild,
export const onOutlineContextMenu = (plugin: PDFPlus, child: PDFViewerChild, file: TFile, evt: MouseEvent) => {
const { lib } = plugin;

if (plugin.settings.enablePDFEdit) {
if (lib.isEditable(child)) {
new Menu()
.addItem((menuItem) => {
menuItem
Expand Down Expand Up @@ -466,7 +466,7 @@ export class PDFPlusContextMenu extends Menu {
// }
// }

if (plugin.settings.enablePDFEdit) {
if (lib.isEditable(child)) {
for (const { name, template } of formats) {
this.addItem((item) => {
return item
Expand Down Expand Up @@ -562,7 +562,7 @@ export class PDFPlusContextMenu extends Menu {
// }

// edit & delete annotation //
if (plugin.settings.enablePDFEdit) {
if (lib.isEditable(child)) {
if (plugin.settings.enableAnnotationContentEdit && PDFAnnotationEditModal.isSubtypeSupported(annot.data.subtype)) {
const subtype = annot.data.subtype;
this.addItem((item) => {
Expand Down Expand Up @@ -600,7 +600,7 @@ export class PDFPlusContextMenu extends Menu {

// Add a PDF internal link to selection
if (selectedText && selection
&& plugin.settings.enablePDFEdit
&& lib.isEditable(child)
&& plugin.lastCopiedDestInfo
&& plugin.lastCopiedDestInfo.file === child.file) {
if ('destArray' in plugin.lastCopiedDestInfo) {
Expand Down
11 changes: 6 additions & 5 deletions src/dom-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ export class DomManager extends PDFPlusComponent {
}

onload() {
this.app.workspace.iterateAllLeaves((leaf) => {
for (const toolbarLeftEl of leaf.containerEl.querySelectorAll<HTMLElement>('.pdf-toolbar-left')) {
this.addChild(new ColorPalette(this.plugin, toolbarLeftEl));
}
});
this.plugin.trigger('update-dom');
// this.app.workspace.iterateAllLeaves((leaf) => {
// for (const toolbarLeftEl of leaf.containerEl.querySelectorAll<HTMLElement>('.pdf-toolbar-left')) {
// this.addChild(new ColorPalette(this.plugin, toolbarLeftEl));
// }
// });

this.updateStyleEl();

Expand Down
4 changes: 2 additions & 2 deletions src/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const registerOutlineDrag = async (plugin: PDFPlus, pdfOutlineViewer: PDF
});

app.dragManager.handleDrop(item.selfEl, (evt, draggable, dragging) => {
if (!plugin.settings.enablePDFEdit) return;
if (!lib.isEditable(child)) return;

if (!draggable || draggable.source !== 'pdf-plus' || draggable.type !== 'pdf-offset') return;

Expand Down Expand Up @@ -83,7 +83,7 @@ export const registerOutlineDrag = async (plugin: PDFPlus, pdfOutlineViewer: PDF
await Promise.all(promises);

app.dragManager.handleDrop(pdfOutlineViewer.childrenEl, (evt, draggable, dragging) => {
if (!plugin.settings.enablePDFEdit) return;
if (!lib.isEditable(child)) return;

if (!draggable || draggable.source !== 'pdf-plus' || draggable.type !== 'pdf-offset') return;

Expand Down
55 changes: 49 additions & 6 deletions src/lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { TemplateProcessor } from 'template';
import { parsePDFSubpath } from 'utils';
import { DestArray } from 'typings';
import { PDFPlusSettingTab } from 'settings';
import { ExternalPDFModal } from 'modals/external-pdf-modals';


export class PDFPlusCommands extends PDFPlusLibSubmodule {
Expand Down Expand Up @@ -169,6 +170,18 @@ export class PDFPlusCommands extends PDFPlusLibSubmodule {
id: 'create-pdf',
name: 'Create new PDF',
callback: () => this.createPDF()
}, {
id: 'import',
name: 'Import this PDF into vault',
checkCallback: (checking) => this.importExternalFileIntoVault(checking)
}, {
id: 'open-external',
name: 'Open this PDF in the original location',
checkCallback: (checking) => this.openExternalSource(checking)
}, {
id: 'create-dummy',
name: 'Create dummy file for external PDF',
callback: () => this.createDummyForExternalPDF()
}
];

Expand Down Expand Up @@ -449,9 +462,9 @@ export class PDFPlusCommands extends PDFPlusLibSubmodule {
}

setWriteFile(checking: boolean, writeFile: boolean) {
if (!this.settings.enablePDFEdit) return false;
const palette = this.lib.getColorPalette();
if (!palette) return false;
if (!this.lib.isEditable(palette.child)) return false;
if (palette.writeFile === writeFile) return false;
if (!checking) {
palette.setWriteFile(writeFile);
Expand Down Expand Up @@ -648,10 +661,11 @@ export class PDFPlusCommands extends PDFPlusLibSubmodule {
}

editPageLabels(checking: boolean) {
if (!this.settings.enablePDFEdit) return false;

const view = this.lib.workspace.getActivePDFView();
if (!view) return false;
if (!view.viewer.child) return false;
if (!this.lib.isEditable(view.viewer.child)) return false;

const file = view.file;
if (!file) return false;

Expand Down Expand Up @@ -716,11 +730,12 @@ export class PDFPlusCommands extends PDFPlusLibSubmodule {
}

addOutlineItem(checking: boolean) {
if (!this.settings.enablePDFEdit) return false;

const view = this.lib.workspace.getActivePDFView();
const file = view?.file;
if (!view || !file) return false;
const child = view?.viewer.child;
if (!view || !file || !child) return false;

if (!this.lib.isEditable(child)) return false;

const state = view.getState();
const destArray = this.lib.viewStateToDestArray(state, true);
Expand Down Expand Up @@ -899,4 +914,32 @@ export class PDFPlusCommands extends PDFPlusLibSubmodule {

return true;
}

importExternalFileIntoVault(checking: boolean) {
const child = this.lib.getPDFViewerChild(true);
if (!child || !child.isFileExternal || !child.palette) return false;

if (!checking) child.palette.importFile();

return true;
}

openExternalSource(checking: boolean) {
const child = this.lib.getPDFViewerChild(true);
const file = child?.file;
if (!child || !child.isFileExternal || !file) return false;

if (!checking) {
(async () => {
const url = (await this.app.vault.read(file)).trim();
window.open(url, '_blank');
})();
}

return true;
}

createDummyForExternalPDF() {
new ExternalPDFModal(this.plugin).open();
}
}

0 comments on commit e667aed

Please sign in to comment.