Add notes overlay with hover-to-show-text functionality

- Bump version
- Feature backported from the other project
next v0.8.0
neobooru 2022-04-10 00:59:11 +02:00
parent 76d1b1e768
commit b0ebc58711
5 changed files with 275 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{
"name": "szuru-chrome",
"version": "0.7.8",
"version": "0.8.0",
"description": "Import media from various sites into szurubooru.",
"author": "neobooru",
"license": "MIT",
@ -16,11 +16,13 @@
},
"dependencies": {
"axios": "^0.23.0",
"markdown-it": "^12.3.2",
"neo-scraper": "^0.5.3",
"vue": "^2.6.14",
"webextension-polyfill": "^0.8.0"
},
"devDependencies": {
"@types/markdown-it": "^12.2.3",
"archiver": "^5.3.0",
"copy-webpack-plugin": "^9.0.1",
"css-loader": "^6.4.0",

View File

@ -70,7 +70,7 @@
@click="onClickAutocompleteTagItem(tag)"
:key="tag.name"
:class="{
active: idx == autocompleteIndex,
active: idx == autocompleteIndex
}"
>
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
@ -116,10 +116,15 @@
</div>
<div class="popup-section">
<div class="post-container">
<img :src="selectedPost.contentUrl" v-if="selectedPost.contentType == 'image'" />
<video v-if="selectedPost.contentType == 'video'" controls>
<source :src="selectedPost.contentUrl" />
</video>
<div class="post-overlay">
<PostNotes :notes="selectedPost.notes" />
</div>
</div>
</div>
</div>
</div>
@ -135,8 +140,10 @@ import { ImageSearchResult, Post } from "../SzuruTypes";
import { Config, SzuruSiteConfig } from "../Config";
import { BrowserCommand, Message, getUrl, isChrome, encodeTagName } from "../Common";
import { PostViewModel, TagViewModel } from "../ViewModels";
import PostNotes from "./components/PostNotes.vue";
export default Vue.extend({
components: { PostNotes },
data() {
return {
config: null as Config | null,
@ -151,7 +158,7 @@ export default Vue.extend({
cancelSource: null as CancelTokenSource | null,
autocompleteIndex: -1,
showTagUsages: false,
showFindSimilarButton: true,
showFindSimilarButton: true
};
},
watch: {
@ -160,13 +167,13 @@ export default Vue.extend({
// No need to check if any vars are unset, findSimilar does that internally
this.findSimilar();
}
},
}
},
methods: {
async getActiveTabId() {
const activeTabs = await browser.tabs.query({
active: true,
currentWindow: true,
currentWindow: true
});
if (activeTabs.length > 0) return activeTabs[0].id!;
@ -301,7 +308,7 @@ export default Vue.extend({
addTag(tag: TagViewModel) {
if (this.selectedPost) {
// Only add tag if it doesn't already exist
if (tag.name.length > 0 && this.selectedPost.tags.find((x) => x.name == tag.name) == undefined) {
if (tag.name.length > 0 && this.selectedPost.tags.find(x => x.name == tag.name) == undefined) {
this.selectedPost.tags.push(tag);
// Add implications for the tag
@ -368,12 +375,12 @@ export default Vue.extend({
return undefined;
},
async loadTagCounts() {
const allTags = this.posts.flatMap((x) => x.tags);
const allTags = this.posts.flatMap(x => x.tags);
for (let i = 0; i < allTags.length; i += 100) {
const query = allTags
.slice(i, i + 101)
.map((x) => encodeTagName(x.name))
.map(x => encodeTagName(x.name))
.join();
const resp = await this.szuru?.getTags(query);
@ -460,9 +467,9 @@ export default Vue.extend({
this.cancelSource.token
);
this.autocompleteTags = resp!.results.map((x) => TagViewModel.fromTag(x));
this.autocompleteTags = resp!.results.map(x => TagViewModel.fromTag(x));
if (this.autocompleteTags.length > 0) this.autocompleteShown = true;
},
}
},
async mounted() {
this.config = await Config.load();
@ -509,7 +516,7 @@ export default Vue.extend({
}
return <WebRequest.BlockingResponse>{
requestHeaders,
requestHeaders
};
},
{ urls: ["<all_urls>"] },
@ -518,7 +525,7 @@ export default Vue.extend({
// Always call grabPost, even when there is no activeSite
await this.grabPost();
},
}
});
</script>
@ -634,4 +641,16 @@ video {
min-width: 5ch; /* 5 character width */
display: inline-block;
}
.post-container {
position: relative;
display: flex;
}
.post-overlay {
position: absolute;
width: 100%;
height: 100%;
/* z-index: 2; */
}
</style>

View File

@ -0,0 +1,38 @@
<!-- Based on https://github.com/cloudacy/vue-markdown-render/blob/master/src/VueMarkdown.ts -->
<script lang="ts">
import Vue from "vue";
import MarkdownIt, { Options } from "markdown-it";
export default Vue.extend({
name: "VueMarkdown",
props: {
source: {
type: String,
required: true
}
},
data() {
return {
md: null as MarkdownIt | null
};
},
computed: {
content(): string | undefined {
console.log(this.source);
return this.md?.render(this.source);
}
},
created() {
const options: Options = {
breaks: true
};
this.md = new MarkdownIt(options);
}
});
</script>
<template>
<div v-html="content" />
</template>

View File

@ -0,0 +1,143 @@
<script lang="ts">
import Vue from "vue";
import { ScrapedNote } from "neo-scraper";
import Markdown from "./Markdown.vue";
class Point {
constructor(public x: any, public y: any) {}
}
// HACK: Copied from old client
function* range(start = 0, end: any = null, step = 1) {
if (end === null) {
end = start;
start = 0;
}
for (let i = start; i < end; i += step) {
yield i;
}
}
// HACK: Copied from old client
function _getNoteCentroid(note: any) {
const vertexCount = note.polygon.length;
const centroid = new Point(0, 0);
let signedArea = 0.0;
for (let i of range(vertexCount)) {
const x0 = note.polygon.at(i)[0];
const y0 = note.polygon.at(i)[1];
const x1 = note.polygon.at((i + 1) % vertexCount)[0];
const y1 = note.polygon.at((i + 1) % vertexCount)[1];
const a = x0 * y1 - x1 * y0;
signedArea += a;
centroid.x += (x0 + x1) * a;
centroid.y += (y0 + y1) * a;
}
signedArea *= 0.5;
centroid.x /= 6 * signedArea;
centroid.y /= 6 * signedArea;
return centroid;
}
export default Vue.extend({
props: ["notes"],
components: { Markdown },
data() {
return {
mouseOverNote: false,
mouseOverNoteText: false,
hoverNote: undefined as ScrapedNote | undefined
};
},
computed: {
showHoverNote: function(): boolean {
if (!this.hoverNote) {
return false;
}
return this.mouseOverNote || this.mouseOverNoteText;
},
// This could probably also be implemented using just CSS.
hoverNoteStyle: function(): string {
if (!this.hoverNote) return "";
const { x, y } = _getNoteCentroid(this.hoverNote);
const top = y * 100;
const left = x * 100;
return `display: block; top: ${top}%; left: ${left}%`;
}
}
});
</script>
<template>
<div>
<div
v-if="hoverNote && showHoverNote"
class="note-text"
:style="hoverNoteStyle"
@mouseenter="mouseOverNoteText = true"
@mouseleave="mouseOverNoteText = false"
>
<div class="wrapper">
<Markdown :source="hoverNote.text" />
</div>
</div>
<svg class="notes-overlay" preserveAspectRatio="none" viewBox="0 0 1 1">
<g v-for="note in notes">
<polygon
vector-effect="non-scaling-stroke"
stroke-alignment="inside"
:points="note.polygon.join()"
@mouseenter="
hoverNote = note;
mouseOverNote = true;
"
@mouseleave="mouseOverNote = false"
/>
</g>
</svg>
</div>
</template>
<style scoped>
.notes-overlay {
width: 100%;
height: 100%;
position: absolute;
}
.notes-overlay g {
stroke-width: 1px;
}
.notes-overlay g polygon {
fill: rgba(255, 255, 205, 0.3);
stroke: rgba(0, 0, 0, 0.2);
}
.note-text {
position: absolute;
max-width: 22.5em;
display: none;
text-align: left;
z-index: 20;
transform: translate(-50%, -50%);
}
.note-text > .wrapper {
background: lemonchiffon;
padding: 0.3em 0.6em;
border: 1px solid black;
color: black;
box-sizing: border-box;
}
.note-text > .wrapper :deep(p:last-of-type) {
margin-bottom: 0;
}
.note-text > .wrapper :deep(p:first-of-type) {
margin-top: 0;
}
</style>

View File

@ -62,6 +62,24 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d"
integrity sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==
"@types/linkify-it@*":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@types/linkify-it/-/linkify-it-3.0.2.tgz#fd2cd2edbaa7eaac7e7f3c1748b52a19143846c9"
integrity sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==
"@types/markdown-it@^12.2.3":
version "12.2.3"
resolved "https://registry.yarnpkg.com/@types/markdown-it/-/markdown-it-12.2.3.tgz#0d6f6e5e413f8daaa26522904597be3d6cd93b51"
integrity sha512-GKMHFfv3458yYy+v/N8gjufHO6MSZKCOXpZc5GXIWWy8uldwfmPn98vp81gZ5f9SVw8YYBctgfJ22a2d7AOMeQ==
dependencies:
"@types/linkify-it" "*"
"@types/mdurl" "*"
"@types/mdurl@*":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/mdurl/-/mdurl-1.0.2.tgz#e2ce9d83a613bacf284c7be7d491945e39e1f8e9"
integrity sha512-eC4U9MlIcu2q0KQmXszyn5Akca/0jrQmwDRgpAMJai7qBWq4amIQhZyNau4VYGtCeALvW1/NtjzJJ567aZxfKA==
"@types/minimatch@*":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
@ -337,6 +355,11 @@ archiver@^5.3.0:
tar-stream "^2.2.0"
zip-stream "^4.1.0"
argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
array-union@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-1.0.2.tgz#9a34410e4f4e3da23dea375be5be70f24778ec39"
@ -767,6 +790,11 @@ enhanced-resolve@^5.0.0, enhanced-resolve@^5.8.3:
graceful-fs "^4.2.4"
tapable "^2.2.0"
entities@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.1.0.tgz#992d3129cf7df6870b96c57858c249a120f8b8b5"
integrity sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==
envinfo@^7.7.3:
version "7.8.1"
resolved "https://registry.yarnpkg.com/envinfo/-/envinfo-7.8.1.tgz#06377e3e5f4d379fea7ac592d5ad8927e0c4d475"
@ -1310,6 +1338,13 @@ lazystream@^1.0.0:
dependencies:
readable-stream "^2.0.5"
linkify-it@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-3.0.3.tgz#a98baf44ce45a550efb4d49c769d07524cc2fa2e"
integrity sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==
dependencies:
uc.micro "^1.0.1"
loader-runner@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.2.0.tgz#d7022380d66d14c5fb1d496b89864ebcfd478384"
@ -1406,6 +1441,22 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"
markdown-it@^12.3.2:
version "12.3.2"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-12.3.2.tgz#bf92ac92283fe983fe4de8ff8abfb5ad72cd0c90"
integrity sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==
dependencies:
argparse "^2.0.1"
entities "~2.1.0"
linkify-it "^3.0.1"
mdurl "^1.0.1"
uc.micro "^1.0.5"
mdurl@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
integrity sha1-/oWy7HWlkDfyrf7BAP1sYBdhFS4=
merge-source-map@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/merge-source-map/-/merge-source-map-1.1.0.tgz#2fdde7e6020939f70906a68f2d7ae685e4c8c646"
@ -2162,6 +2213,11 @@ typescript@^4.4.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.4.tgz#2cd01a1a1f160704d3101fd5a58ff0f9fcb8030c"
integrity sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==
uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.6"
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"
integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==
uri-js@^4.2.2:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"