Skip to content

Commit 3cb84fc

Browse files
committed
Merge commit '56c5cad44cdc5e9e081aa5fb7fde75d7313e189c'
2 parents 83466a2 + 56c5cad commit 3cb84fc

14 files changed

+268
-172
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = tab
9+
indent_size = 4
10+
tab_width = 4

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
npm node_modules
2+
build

.eslintrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"env": { "node": true },
5+
"plugins": [
6+
"@typescript-eslint"
7+
],
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/eslint-recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"parserOptions": {
14+
"sourceType": "module"
15+
},
16+
"rules": {
17+
"no-unused-vars": "off",
18+
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
19+
"@typescript-eslint/ban-ts-comment": "off",
20+
"no-prototype-builtins": "off",
21+
"@typescript-eslint/no-empty-function": "off"
22+
}
23+
}

.gitignore

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# vscode
2+
.vscode
3+
14
# Intellij
25
*.iml
36
.idea
@@ -6,9 +9,15 @@
69
node_modules
710
package-lock.json
811

9-
# build
12+
# Don't include the compiled main.js file in the repo.
13+
# They should be uploaded to GitHub releases instead.
1014
main.js
11-
*.js.map
15+
16+
# Exclude sourcemaps
17+
*.map
1218

1319
# obsidian
1420
data.json
21+
22+
# Exclude macOS Finder (System Explorer) View States
23+
.DS_Store

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tag-version-prefix=""

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This makes copying URLs in prewiew like it is in edit mode by deafult. For copyi
66

77
Copying images:
88

9-
https://user-images.githubusercontent.com/1992842/132140547-fead74c1-4bec-489a-945c-f28cbba43493.mp4
9+
[copy-images-vid](https://user-images.githubusercontent.com/1992842/132140547-fead74c1-4bec-489a-945c-f28cbba43493.mp4)
1010

1111
Copying URLs:
1212

@@ -30,7 +30,8 @@ You can also manually copy from releases to your `.obsidian/plugins/copy-url-in-
3030

3131
Thank you to the makers of the [Tag Wrangler plugin](https://github.com/pjeby/tag-wrangler), as it was a great starting point for working with context menus in Obsidian.
3232

33-
Copying images developed by [luckman212](https://github.com/luckman212).
33+
[Copying](https://github.com/NomarCub/obsidian-copy-url-in-preview/pull/2) [images](https://github.com/NomarCub/obsidian-copy-url-in-preview/pull/3) developed by [luckman212](https://github.com/luckman212).
34+
[Android image sharing](https://github.com/NomarCub/obsidian-copy-url-in-preview/issues/5) developed by [mnaoumov](https://github.com/mnaoumov).
3435

3536
Original plugin by [NomarCub](https://github.com/luckman212)
3637

esbuild.config.mjs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import esbuild from "esbuild";
2+
import process from "process";
3+
import builtins from 'builtin-modules'
4+
5+
const banner =
6+
`/*
7+
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
8+
if you want to view the source, please visit the github repository of this plugin
9+
*/
10+
`;
11+
12+
const prod = (process.argv[2] === 'production');
13+
14+
esbuild.build({
15+
banner: {
16+
js: banner,
17+
},
18+
entryPoints: ['src/main.ts'],
19+
bundle: true,
20+
external: [
21+
'obsidian',
22+
'electron',
23+
'@codemirror/autocomplete',
24+
'@codemirror/closebrackets',
25+
'@codemirror/collab',
26+
'@codemirror/commands',
27+
'@codemirror/comment',
28+
'@codemirror/fold',
29+
'@codemirror/gutter',
30+
'@codemirror/highlight',
31+
'@codemirror/history',
32+
'@codemirror/language',
33+
'@codemirror/lint',
34+
'@codemirror/matchbrackets',
35+
'@codemirror/panel',
36+
'@codemirror/rangeset',
37+
'@codemirror/rectangular-selection',
38+
'@codemirror/search',
39+
'@codemirror/state',
40+
'@codemirror/stream-parser',
41+
'@codemirror/text',
42+
'@codemirror/tooltip',
43+
'@codemirror/view',
44+
...builtins],
45+
format: 'cjs',
46+
watch: !prod,
47+
target: 'es2016',
48+
logLevel: "info",
49+
sourcemap: prod ? false : 'inline',
50+
treeShaking: true,
51+
outfile: 'main.js',
52+
}).catch(() => process.exit(1));

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "copy-url-in-preview",
33
"name": "Copy Image and URL in Preview",
4-
"version": "1.1.0",
4+
"version": "1.2.0",
55
"minAppVersion": "0.11.13",
66
"description": "Copy Image and Copy URL context menu in preview mode",
77
"author": "NomarCub",

package.json

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
{
2-
"name": "copy-url-in-preview",
3-
"version": "1.1.0",
4-
"description": "Copy Image and Copy URL context menu in preview mode for Obsidian (https://obsidian.md)",
5-
"main": "main.js",
6-
"scripts": {
7-
"dev": "rollup --config rollup.config.js -w",
8-
"build": "rollup --config rollup.config.js --environment BUILD:production"
9-
},
10-
"keywords": [],
11-
"author": "",
12-
"license": "MIT",
13-
"devDependencies": {
14-
"@rollup/plugin-commonjs": "^18.0.0",
15-
"@rollup/plugin-node-resolve": "^11.2.1",
16-
"@rollup/plugin-typescript": "^8.2.1",
17-
"@types/node": "^14.14.37",
18-
"obsidian": "^0.12.0",
19-
"rollup": "^2.32.1",
20-
"tslib": "^2.2.0",
21-
"typescript": "^4.2.4"
22-
}
2+
"name": "copy-url-in-preview",
3+
"version": "1.2.0",
4+
"description": "Copy Image and Copy URL context menu in preview mode for Obsidian (https://obsidian.md)",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "node esbuild.config.mjs",
8+
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
9+
"version": "node version-bump.mjs && git add manifest.json versions.json"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "MIT",
14+
"devDependencies": {
15+
"@types/node": "^16.11.6",
16+
"@typescript-eslint/eslint-plugin": "^5.2.0",
17+
"@typescript-eslint/parser": "^5.2.0",
18+
"builtin-modules": "^3.2.0",
19+
"esbuild": "0.13.12",
20+
"obsidian": "^0.12.0",
21+
"tslib": "2.3.1",
22+
"typescript": "4.6.4"
23+
}
2324
}

rollup.config.js

-30
This file was deleted.

src/helpers.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { FileSystemAdapter } from "obsidian";
2+
3+
const loadImageBlobTimeout = 5000;
4+
5+
export interface ElectronWindow extends Window {
6+
WEBVIEW_SERVER_URL: string
7+
}
8+
9+
export interface FileSystemAdapterWithInternalApi extends FileSystemAdapter {
10+
open(path: string): Promise<void>
11+
}
12+
13+
export interface Listener {
14+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15+
(this: Document, ev: Event): any;
16+
}
17+
18+
export function withTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
19+
const timeout = new Promise((resolve, reject) => {
20+
const id = setTimeout(() => {
21+
clearTimeout(id);
22+
reject(`timed out after ${ms} ms`)
23+
}, ms)
24+
})
25+
return Promise.race([
26+
promise,
27+
timeout
28+
]) as Promise<T>
29+
}
30+
31+
// https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image
32+
// option?: https://www.npmjs.com/package/html-to-image
33+
export async function loadImageBlob(imgSrc: string): Promise<Blob> {
34+
const loadImageBlobCore = () => {
35+
return new Promise<Blob>((resolve, reject) => {
36+
const image = new Image();
37+
image.crossOrigin = "anonymous";
38+
image.onload = () => {
39+
const canvas = document.createElement("canvas");
40+
canvas.width = image.width;
41+
canvas.height = image.height;
42+
const ctx = canvas.getContext("2d");
43+
ctx.drawImage(image, 0, 0);
44+
canvas.toBlob((blob: Blob) => {
45+
resolve(blob);
46+
});
47+
};
48+
image.onerror = async () => {
49+
try {
50+
await fetch(image.src, { "mode": "no-cors" });
51+
52+
// console.log("possible CORS violation, falling back to allOrigins proxy");
53+
// https://github.com/gnuns/allOrigins
54+
const blob = await loadImageBlob(`https://api.allorigins.win/raw?url=${encodeURIComponent(imgSrc)}`);
55+
resolve(blob);
56+
} catch {
57+
reject();
58+
}
59+
}
60+
image.src = imgSrc;
61+
});
62+
};
63+
return withTimeout(loadImageBlobTimeout, loadImageBlobCore())
64+
}
65+
66+
export function onElement(
67+
el: Document,
68+
event: keyof HTMLElementEventMap,
69+
selector: string,
70+
listener: Listener,
71+
options?: { capture?: boolean; }
72+
) {
73+
el.on(event, selector, listener, options);
74+
return () => el.off(event, selector, listener, options);
75+
}

0 commit comments

Comments
 (0)