Skip to content

Commit

Permalink
fix: keep query of url of note
Browse files Browse the repository at this point in the history
  • Loading branch information
betterRunner committed Jan 2, 2022
1 parent d5797be commit 12dbe48
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
4 changes: 3 additions & 1 deletion src/content-scripts/renderer/popup/note-book/note-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ export default defineComponent({
};
// 1. create the note item
const href = window.location.href;
const note: TNote = {
id: noteId,
createTime: dayjs().unix(),
updateTime: dayjs().unix(),
link: removeUrlPostfix(window.location.href),
link: removeUrlPostfix(href),
rawLink: href,
linkTitle: window.document.title ?? '',
content: text,
rects: rects,
Expand Down
15 changes: 5 additions & 10 deletions src/content-scripts/renderer/popup/note-book/note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import { Storage } from "@/types/storage";
import { Coor, Oper } from "@/types/common";
import { Query } from "@/types/dom";
import mitt from "@/utils/mitt";
import { wrapUrlWithQuery } from "@/utils/utils";
import { appendUrlQuery } from "@/utils/utils";
import TagBook from "../tag-book/index.vue";
import More from "../shared/more.vue";
Expand Down Expand Up @@ -112,15 +112,12 @@ export default {
);
/// note link
const noteLink = ref(props.note.link || "");
const handleCopy = () => {
ElMessage.success("Copied");
};
const handleOpenLink = (note: Note) => {
const query: Query = {
noteId: note.id,
};
const url = wrapUrlWithQuery(note.link, query);
console.log(note.rawLink);
const url = appendUrlQuery(note.rawLink, query);
window.open(url);
};
Expand All @@ -136,8 +133,8 @@ export default {
{
title: "Copy to clipboard",
onClick: () => {
const { link = '', linkTitle = '', content = '', tags = [] } : Note = props.note;
const text = `[${linkTitle || 'unknowned'}](${link})
const { rawLink = '', linkTitle = '', content = '', tags = [] } : Note = props.note;
const text = `[${linkTitle || 'unknowned'}](${rawLink})
- ${content}
${tags.join(',')}`;
toClipboard(text);
Expand Down Expand Up @@ -236,8 +233,6 @@ export default {
colorBarStyle,
notSelected,
noteLink,
handleCopy,
handleOpenLink,
moreOpers,
Expand Down
1 change: 1 addition & 0 deletions src/types/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Note {
createTime: number;
updateTime: number;
link: string;
rawLink: string;
linkTitle: string;
content: string;
tags: string[];
Expand Down
56 changes: 36 additions & 20 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
import appendQuery from 'append-query';

export const isObject = (obj: any) => {
return typeof obj === "object" && obj !== null;
};
return typeof obj === 'object' && obj !== null
}

export const getObjectType = (obj: any) => Object.prototype.toString.call(obj);
export const getObjectType = (obj: any) => Object.prototype.toString.call(obj)

export const removeUrlPostfix = (url: string) => {
let res = url.split("?")?.[0] ?? "";
res = res.split("#")?.[0] ?? "";
return res;
};
let res = url.split('?')?.[0] ?? ''
res = res.split('#')?.[0] ?? ''
return res
}

export const getUrlQuery = (url: string) => {
const queryStr = url.split("?")?.[1] ?? "";
const queryStr = url.split('?')?.[1] ?? ''
const res = Object.fromEntries(
queryStr.split("&").map((part) => {
return part.split("=");
queryStr.split('&').map((part) => {
return part.split('=')
})
);
return res;
};
)
return res
}

// export const appendUrlQuery = (
// url: string,
// query: { [key: string]: any }
// ) => {
// return appendQuery(url, query)
// }

export const wrapUrlWithQuery = (
url: string,
query: { [key: string]: any }
) => {
if (!query || Object.keys(query).length === 0) return url;
return `${url}?${Object.entries(query).map(e => e.join('=')).join('&')}`;
};
export const appendUrlQuery = (
baseUrl: string,
params: Record<string, any>
): string => {
const Url = new URL(baseUrl);
const urlParams: URLSearchParams = new URLSearchParams(Url.search);
for (const key in params) {
if (params[key] !== undefined) {
urlParams.set(key, params[key]);
}
}
Url.search = urlParams.toString();
return Url.toString();
};

0 comments on commit 12dbe48

Please sign in to comment.