Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose API for registering callbacks when the PDF is clicked #155

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"use strict";

(function () {
const vscode = acquireVsCodeApi();

function handleclick(event) {
debugger;
vscode.postMessage({
type: 'clicked',
text: event.target.innerText
})
}
function loadConfig() {
const elem = document.getElementById('pdf-preview-config')
if (elem) {
Expand Down Expand Up @@ -73,6 +82,7 @@
PDFViewerApplication.load(doc)
})
})
document.getElementById('viewer').addEventListener('click', handleclick)

window.addEventListener('message', async function () {
// Prevents flickering of page when PDF is reloaded
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@
"vscode-test": "^1.3.0"
},
"extensionKind": [
"ui"
"ui", "workspace"
]
}
11 changes: 9 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as vscode from 'vscode';
import { PdfCustomProvider } from './pdfProvider';

export function activate(context: vscode.ExtensionContext): void {
export function activate(context: vscode.ExtensionContext) {
const extensionRoot = vscode.Uri.file(context.extensionPath);
let callbacks = []
// Register our custom editor provider
const provider = new PdfCustomProvider(extensionRoot);
const provider = new PdfCustomProvider(extensionRoot, callbacks);
context.subscriptions.push(
vscode.window.registerCustomEditorProvider(
PdfCustomProvider.viewType,
Expand All @@ -17,6 +18,12 @@ export function activate(context: vscode.ExtensionContext): void {
}
)
);

return {
registerOnClickCallback(callback: (contents: string) => any) {
callbacks.push(callback)
}
}
}

export function deactivate(): void {}
11 changes: 10 additions & 1 deletion src/pdfPreview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HookCallbacks } from 'async_hooks';
import * as path from 'path';
import * as vscode from 'vscode';
import { MessageChannel } from 'worker_threads';
import { Disposable } from './disposable';

function escapeAttribute(value: string | vscode.Uri): string {
Expand All @@ -14,7 +16,8 @@ export class PdfPreview extends Disposable {
constructor(
private readonly extensionRoot: vscode.Uri,
private readonly resource: vscode.Uri,
private readonly webviewEditor: vscode.WebviewPanel
private readonly webviewEditor: vscode.WebviewPanel,
private callbacks: {(content:string)}[]
) {
super();
const resourceRoot = resource.with({
Expand All @@ -38,6 +41,12 @@ export class PdfPreview extends Disposable {
);
break;
}
case 'clicked': {
for (const callback of this.callbacks) {
callback(message.text)
}
break;
}
}
})
);
Expand Down
8 changes: 6 additions & 2 deletions src/pdfProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export class PdfCustomProvider implements vscode.CustomReadonlyEditorProvider {
private readonly _previews = new Set<PdfPreview>();
private _activePreview: PdfPreview | undefined;

constructor(private readonly extensionRoot: vscode.Uri) {}
constructor(
private readonly extensionRoot: vscode.Uri,
private callbacks: {(content:string)}[]
) {}

public openCustomDocument(uri: vscode.Uri): vscode.CustomDocument {
return { uri, dispose: (): void => {} };
Expand All @@ -20,7 +23,8 @@ export class PdfCustomProvider implements vscode.CustomReadonlyEditorProvider {
const preview = new PdfPreview(
this.extensionRoot,
document.uri,
webviewEditor
webviewEditor,
this.callbacks
);
this._previews.add(preview);
this.setActivePreview(preview);
Expand Down