Skip to content

Commit

Permalink
release: 0.38.5
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Mar 18, 2024
1 parent 9ef0cc7 commit 7cd028b
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 48 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.4",
"version": "0.38.5",
"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.4",
"version": "0.38.5",
"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.4",
"version": "0.38.5",
"description": "The most Obsidian-native PDF annotation tool ever.",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
4 changes: 1 addition & 3 deletions src/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { App, Menu, Notice, Platform, TFile } from 'obsidian';
import PDFPlus from 'main';
import { PDFPlusLib } from 'lib';
import { PDFOutlineItem, PDFOutlines } from 'lib/outlines';
import { PDFOutlineMoveModal, PDFOutlineTitleModal } from 'modals/outline-modals';
import { PDFComposerModal } from 'modals/pdf-composer-modals';
import { PDFAnnotationDeleteModal, PDFAnnotationEditModal } from 'modals/annotation-modals';
import { PDFOutlineMoveModal, PDFOutlineTitleModal, PDFComposerModal, PDFAnnotationDeleteModal, PDFAnnotationEditModal } from 'modals';
import { toSingleLine } from 'utils';
import { PDFOutlineTreeNode, PDFViewerChild } from 'typings';
import { PDFViewerBacklinkVisualizer } from 'backlink-visualizer';
Expand Down
5 changes: 1 addition & 4 deletions src/lib/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Command, MarkdownView, Notice, TFile, normalizePath, setIcon } from 'obsidian';

import { PDFPlusLibSubmodule } from './submodule';
import { PDFComposerModal, PDFCreateModal, PDFPageDeleteModal } from 'modals/pdf-composer-modals';
import { PDFPageLabelEditModal } from 'modals/page-label-modals';
import { PDFComposerModal, PDFCreateModal, PDFPageDeleteModal, PDFPageLabelEditModal, PDFOutlineTitleModal, ExternalPDFModal } from 'modals';
import { PDFOutlines } from './outlines';
import { PDFOutlineTitleModal } from 'modals/outline-modals';
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
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Constructor, EditableFileView, EventRef, Events, Keymap, Notice, PaneType, Platform, Plugin, TFile, loadPdfJs, requireApiVersion } from 'obsidian';
import { Constructor, EditableFileView, EventRef, Events, Keymap, Notice, ObsidianProtocolData, PaneType, Platform, Plugin, TFile, loadPdfJs, requireApiVersion } from 'obsidian';
import * as pdflib from '@cantoo/pdf-lib';

import { patchPDFView, patchPDFInternals, patchBacklink, patchWorkspace, patchPagePreview, patchClipboardManager, patchPDFInternalFromPDFEmbed } from 'patchers';
Expand All @@ -10,6 +10,7 @@ import { PDFCroppedEmbed } from 'pdf-cropped-embed';
import { DEFAULT_SETTINGS, PDFPlusSettings, PDFPlusSettingTab } from 'settings';
import { subpathToParams, OverloadParameters, focusObsidian } from 'utils';
import { DestArray, ObsidianViewer, PDFEmbed, PDFView, PDFViewerChild, PDFViewerComponent, Rect } from 'typings';
import { ExternalPDFModal } from 'modals';


export default class PDFPlus extends Plugin {
Expand Down Expand Up @@ -104,6 +105,8 @@ export default class PDFPlus extends Plugin {
this.registerEvents();

this.startTrackingActiveMarkdownFile();

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

private checkVersion() {
Expand Down Expand Up @@ -480,6 +483,16 @@ export default class PDFPlus extends Plugin {
});
}

obsidianProtocolHandler(params: ObsidianProtocolData) {
if ('create-dummy' in params) {
return ExternalPDFModal.createDummyFilesFromObsidianUrl(this, params);
}

if ('setting' in params) {
return this.settingTab.openFromObsidianUrl(params);
}
}

on(evt: 'highlight', callback: (data: { type: 'selection' | 'annotation', source: 'obsidian' | 'pdf-plus', pageNumber: number, child: PDFViewerChild }) => any, context?: any): EventRef;
on(evt: 'color-palette-state-change', callback: (data: { source: ColorPalette }) => any, context?: any): EventRef;
on(evt: 'update-dom', callback: () => any, context?: any): EventRef;
Expand Down
28 changes: 28 additions & 0 deletions src/modals/base-modal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Component, Modal } from 'obsidian';

import PDFPlus from 'main';
import { PDFPlusLib } from 'lib';


export class PDFPlusModal extends Modal {
plugin: PDFPlus;
lib: PDFPlusLib;
component: Component;

constructor(plugin: PDFPlus) {
super(plugin.app);
this.plugin = plugin;
this.lib = plugin.lib;
this.component = new Component();
this.contentEl.addClass('pdf-plus-modal');
}

onOpen() {
this.component.load();
}

onClose() {
this.contentEl.empty();
this.component.unload();
}
}
53 changes: 50 additions & 3 deletions src/modals/external-pdf-modals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PDFPlus from 'main';
import { PDFPlusModal } from 'modals';
import { Notice, Platform, Setting, normalizePath } from 'obsidian';
import { Notice, ObsidianProtocolData, Platform, Setting, TFile, normalizePath } from 'obsidian';
import { FuzzyFolderSuggest } from 'utils';


Expand Down Expand Up @@ -190,6 +191,7 @@ export class ExternalPDFModal extends PDFPlusModal {
.setDesc('Must start with "https://" or "http://".')
.addText((text) => {
text.inputEl.size = 30;
if (this.urls.length) text.setValue(this.urls[0]);
text.onChange((value) => {
if (!value || value.startsWith('https://') || value.startsWith('http://')) {
this.urls = [value];
Expand Down Expand Up @@ -240,10 +242,22 @@ export class ExternalPDFModal extends PDFPlusModal {

async createDummyFiles() {
let failed: string[] = [];
const promises: Promise<any>[] = [];
const promises: Promise<TFile | null>[] = [];

const createDummyFile = async (url: string, filePath: string) => {
// Create the parent folder if it doesn't exist
const folderPath = normalizePath(filePath.split('/').slice(0, -1).join('/'));
if (folderPath) {
const folderExists = !!(this.app.vault.getAbstractFileByPath(folderPath));
if (!folderExists) {
await this.app.vault.createFolder(folderPath);
}
}

// Find an available file path in that folder
const availableFilePath = this.app.vault.getAvailablePath(filePath.slice(0, -4), 'pdf')

// Create the dummy file
const file = await this.app.vault.create(availableFilePath, url);
return file;
};
Expand All @@ -260,6 +274,7 @@ export class ExternalPDFModal extends PDFPlusModal {
createDummyFile(url, filePath).catch((err) => {
failed.push(url);
console.error(err);
return null;
})
);
}
Expand All @@ -272,19 +287,51 @@ export class ExternalPDFModal extends PDFPlusModal {
createDummyFile(this.urls[0], filePath).catch((err) => {
failed = this.urls;
console.error(err);
return null;
})
);
}
} else {
failed = this.urls;
}

await Promise.all(promises);
const files = await Promise.all(promises);

if (failed.length) {
new Notice(`${this.plugin.manifest.name}: Failed to create dummy files for the following URLs: ${failed.join(', ')}`);
} else {
new Notice(`${this.plugin.manifest.name}: Dummy files created successfully.`);
}

// Ideally, I want to open all the created files, but it does not work as expected for some reasons that I don't understand.
// TODO: Figure it out!
// So, for now, I open only the first file.
const firstFile = files.find((file): file is TFile => !!file);
if (firstFile) {
const leaf = this.app.workspace.getLeaf(true);
await leaf.openFile(firstFile);
}
}

static async createDummyFilesFromObsidianUrl(plugin: PDFPlus, params: ObsidianProtocolData) {
// Ignore everything before https://, http:// or file:///, e.g. "chrome-extension://..."
const url = params['create-dummy'].replace(/^.*((https?)|(file):\/\/)/, '$1');
const modal = new ExternalPDFModal(plugin);
modal.source = url.startsWith('http') ? 'web' : 'file';
modal.urls = [url];

if ('folder' in params) {
const folderPath = params.folder;
if (modal.source === 'web') {
modal.filePath = normalizePath(folderPath + '/Untitled.pdf');
} else {
modal.folderPath = normalizePath(folderPath);
}
await modal.createDummyFiles();
return;
}

// If the folder path is not provided, ask the user for it
modal.open();
}
}
34 changes: 6 additions & 28 deletions src/modals/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
import { Component, Modal } from 'obsidian';

import PDFPlus from 'main';
import { PDFPlusLib } from 'lib';


export class PDFPlusModal extends Modal {
plugin: PDFPlus;
lib: PDFPlusLib;
component: Component;

constructor(plugin: PDFPlus) {
super(plugin.app);
this.plugin = plugin;
this.lib = plugin.lib;
this.component = new Component();
this.contentEl.addClass('pdf-plus-modal');
}

onOpen() {
this.component.load();
}

onClose() {
this.contentEl.empty();
this.component.unload();
}
}
export * from './base-modal';
export * from './annotation-modals';
export * from './pdf-composer-modals';
export * from './outline-modals';
export * from './page-label-modals';
export * from './external-pdf-modals';
2 changes: 1 addition & 1 deletion src/patchers/pdf-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { around } from 'monkey-around';

import PDFPlus from 'main';
import { ColorPalette } from 'color-palette';
import { PDFAnnotationDeleteModal, PDFAnnotationEditModal } from 'modals/annotation-modals';
import { PDFAnnotationDeleteModal, PDFAnnotationEditModal } from 'modals';
import { onContextMenu, onOutlineContextMenu, onThumbnailContextMenu } from 'context-menu';
import { registerAnnotationPopupDrag, registerOutlineDrag, registerThumbnailDrag } from 'drag';
import { patchPDFOutlineViewer } from './pdf-outline-viewer';
Expand Down
27 changes: 24 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Component, DropdownComponent, HexString, IconName, MarkdownRenderer, Notice, PluginSettingTab, Setting, TextAreaComponent, TextComponent, debounce, setIcon, setTooltip } from 'obsidian';
import { Component, DropdownComponent, HexString, IconName, MarkdownRenderer, Notice, ObsidianProtocolData, PluginSettingTab, Setting, TextAreaComponent, TextComponent, debounce, setIcon, setTooltip } from 'obsidian';

import PDFPlus from 'main';
import { ExtendedPaneType, isSidebarType } from 'lib/workspace-lib';
import { AutoFocusTarget } from 'lib/copy-link';
import { CommandSuggest, FuzzyFolderSuggest, FuzzyMarkdownFileSuggest, KeysOfType, getModifierNameInPlatform, isHexString } from 'utils';
import { PAGE_LABEL_UPDATE_METHODS, PageLabelUpdateMethod } from 'modals/pdf-composer-modals';
import { PAGE_LABEL_UPDATE_METHODS, PageLabelUpdateMethod } from 'modals';


const SELECTION_BACKLINK_VISUALIZE_STYLE = {
Expand Down Expand Up @@ -448,6 +448,11 @@ export const DEFAULT_SETTINGS: PDFPlusSettings = {
};


export function isPDFPlusSettingsKey(key: string): key is keyof PDFPlusSettings {
return DEFAULT_SETTINGS.hasOwnProperty(key);
}


export class PDFPlusSettingTab extends PluginSettingTab {
component: Component;
items: Partial<Record<keyof PDFPlusSettings, Setting>>;
Expand Down Expand Up @@ -560,6 +565,18 @@ export class PDFPlusSettingTab extends PluginSettingTab {
if (el) this.containerEl.scrollTo({ top: el.offsetTop - this.headerContainerEl.offsetHeight, ...options });
}

openFromObsidianUrl(params: ObsidianProtocolData) {
const id = params.setting;
if (id.startsWith('heading:')) {
this.plugin.openSettingTab()
.scrollToHeading(id.slice('heading:'.length));
} else if (isPDFPlusSettingsKey(id)) {
this.plugin.openSettingTab()
.scrollTo(id);
}
return;
}

addTextSetting(settingName: KeysOfType<PDFPlusSettings, string>, placeholder?: string, onBlurOrEnter?: (setting: Setting) => any) {
const setting = this.addSetting(settingName)
.addText((text) => {
Expand Down Expand Up @@ -2139,7 +2156,11 @@ export class PDFPlusSettingTab extends PluginSettingTab {
.setDesc(`Press ${getModifierNameInPlatform('Mod').toLowerCase()} while hovering a PDF link to actually open it if the target PDF is already opened in another tab.`)
this.addSetting()
.setName('Open PDF links with an external app')
.setDesc('See the "Integration with external apps" section for the details.');
.setDesc(createFragment((el) => {
el.appendText('See the ');
el.appendChild(this.createLinkToHeading('external-app'));
el.appendText(' section for the details.');
}));


this.addSetting()
Expand Down

0 comments on commit 7cd028b

Please sign in to comment.