Skip to content

Commit

Permalink
解决不了中文乱码问题,搁置
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlb0224 committed Sep 20, 2024
1 parent a065e17 commit ac628e9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 51 deletions.
59 changes: 39 additions & 20 deletions src/component/PopupRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ export function PopupRoot({
const parentElement = tabDiv;
const boundaryElement = tabDiv;
const lastScaleKey = getPref("lastScaleKey") as string;
const lastScale = getItem(lastScaleKey);
const [lastScale, setLastScale] = useState<Zotero.Item | undefined>(getItem(lastScaleKey));
const lastScaleItemKey = getPref("lastScaleItemKey") as string;
const lastScaleItem = getItem(lastScaleItemKey);
const [lastScaleItem, setLastScaleItem] = useState<Zotero.Item | undefined>(getItem(lastScaleItemKey));

if (isDebug()) boundaryElement.style.border = "1px solid red";

Expand Down Expand Up @@ -431,6 +431,8 @@ export function PopupRoot({
const clickMeButtonRef = React.useRef<HTMLElement | null>(null);

const vars = [
lastScale,
lastScaleItem,
exAction,
sScale,
sScaleAction,
Expand Down Expand Up @@ -1112,13 +1114,21 @@ export function PopupRoot({
>
{!params.ids && lastScale && lastScale.parentKey == item.key && (
<>
<span> 量表: {lastScale.annotationText.substring(0, 30)}</span>
<div
style={{
maxWidth: (windowType == "FollowParent" && !params.ids ? selectionPopupSize.width - 16 : divMaxWidth) + "px",
display: "flex",
flexWrap: "wrap",
}}
> <button className="toolbar-button"
style={{ width: "unset", margin: " 0 2px", height: "auto" }}
onClick={() => { setLastScale(undefined); setPref("lastScaleKey", ""); setLastScaleItem(undefined); setPref("lastScaleItemKey", ""); }}></button> <span>量表: {lastScale.annotationText.substring(0, 30)}</span>
</div> <div
style={{
maxWidth: (windowType == "FollowParent" && !params.ids ? selectionPopupSize.width - 16 : divMaxWidth) + "px",
display: "flex",
flexWrap: "wrap",
}}
>
{ScaleActionTypeArray.map((a) => (
<button
Expand All @@ -1142,17 +1152,26 @@ export function PopupRoot({
</button>
))}
</div>
{lastScaleItem && lastScale.parentKey == item.key && (
{lastScaleItem && lastScaleItem.parentKey == item.key && (
<>
<span>item:{lastScaleItem.annotationText.substring(0, 30)}</span>
<div
style={{
maxWidth: (windowType == "FollowParent" && !params.ids ? selectionPopupSize.width - 16 : divMaxWidth) + "px",
display: "flex",
flexWrap: "wrap",
}}
> <button className="toolbar-button"
style={{ width: "unset", margin: " 0 2px", height: "auto" }}
onClick={() => { setLastScaleItem(undefined); setPref("lastScaleItemKey", ""); }}></button> <span>item:{lastScaleItem.annotationText.substring(0, 30)}</span>
</div>
<div
style={{
maxWidth: (windowType == "FollowParent" && !params.ids ? selectionPopupSize.width - 16 : divMaxWidth) + "px",
display: "flex",
flexWrap: "wrap",
}}
>
{" "}

{ScaleItemActionTypeArray.map((a) => (
<button
className="toolbar-button"
Expand Down Expand Up @@ -1593,20 +1612,20 @@ export function PopupRoot({
ctrlAddOrSaveTags(isAdd, cTag);
return false;
}}
// onMouseDown={(e) => {
// e.preventDefault();
// ztoolkit.log("onMouseDown 复制", e)
// return false
// }}
// onContextMenu={e => {
// e.preventDefault();
// ztoolkit.log("onContextMenu 复制", tag.key)
// new window.Clipboard().readText().then((text) => {
// ztoolkit.log("onContextMenu 复制", tag.key, text);
// (e.currentTarget as HTMLInputElement).value = text;
// })
// return false
// }}
// onMouseDown={(e) => {
// e.preventDefault();
// ztoolkit.log("onMouseDown 复制", e)
// return false
// }}
// onContextMenu={e => {
// e.preventDefault();
// ztoolkit.log("onContextMenu 复制", tag.key)
// new window.Clipboard().readText().then((text) => {
// ztoolkit.log("onContextMenu 复制", tag.key, text);
// (e.currentTarget as HTMLInputElement).value = text;
// })
// return false
// }}
>
<span>[{tag.values.length}]</span>
<span>{tag.key}</span>
Expand Down
20 changes: 9 additions & 11 deletions src/modules/AnnotationPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1261,18 +1261,16 @@ export async function saveAnnotationTags(
if (tags.some((s) => s.name == "量表")) {
setPref("lastScaleKey", newAnnItem.key);
setPref("lastScaleItemKey", "");
} else if (lastScale) {
if (newAnnItem.parentKey == lastScale.parentKey) {
if (tags.some((s) => ScaleActionTypeArray.map((a) => "量表" + a).includes(s.name))) {
} else if (lastScale && newAnnItem.parentKey == lastScale.parentKey) {
if (tags.some((s) => ScaleActionTypeArray.map((a) => "量表" + a).includes(s.name))) {
new Relations(lastScale).addRelationsToItem(newAnnItem);
if (tags.some((s) => s.name == "量表item")) {
setPref("lastScaleItemKey", newAnnItem.key);
}
} else if (lastScaleItem && lastScaleItem.parentKey == newAnnItem.parentKey) {
if (tags.some((s) => ScaleItemActionTypeArray.map((a) => "量表" + a).includes(s.name))) {
new Relations(lastScale).addRelationsToItem(newAnnItem);
if (tags.some((s) => s.name == "量表item")) {
setPref("lastScaleItemKey", newAnnItem.key);
}
} else if (lastScaleItem.parentKey == newAnnItem.parentKey) {
if (tags.some((s) => ScaleItemActionTypeArray.map((a) => "量表" + a).includes(s.name))) {
new Relations(lastScale).addRelationsToItem(newAnnItem);
new Relations(lastScaleItem).addRelationsToItem(newAnnItem);
}
new Relations(lastScaleItem).addRelationsToItem(newAnnItem);
}
}
}
Expand Down
26 changes: 24 additions & 2 deletions src/modules/AnnotationsToNote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -971,30 +971,52 @@ export async function exportScaleCsv(collectionOrItem: "collection" | "item") {
cols = data[i];
for (let j = 0; j < cols.length; j++) {
let data = cols[j]?.replace(/(\r\n|\n|\r)/gm, "").replace(/(\s\s)/gm, " ") || "";
data = data.replace(/"/g, '""');
data = data.replace(/"/g, '""').replaceAll("&#8296;", "").replaceAll("&#8297;", "");
row.push('"' + data + '"');
}
csv.push(row.join(separator));
}
const csv_string = csv_header + csv.join("\n");
ztoolkit.log(csv_string)
const classes = Components.classes as any;
const nsIFilePicker = Components.interfaces.nsIFilePicker;
const fp = classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);


fp.defaultString = "Scale - " + new Date().getTime() + ".csv";
fp.init(window, "Save to file", nsIFilePicker.modeSave);
fp.appendFilter("CSV (*.csv; *.txt)", "*.csv; *.txt");
fp.defaultExtension = "csv";

// File file = new File("文件路径+文件名")
// OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
// //追加内容到文件中
// oStreamWriter.append(str);
// //写入文件到文件中
// //oStreamWriter.write(sb.toString().replace("\n","\r\n"))
// oStreamWriter.close();


fp.open(function () {
const outputStream = classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(fp.file, 0x04 | 0x08 | 0x20, 420, 0);
const converter = classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(outputStream, "gb2312", 0, 0);
converter.init(outputStream, "gb2313", 0, 0);
converter.writeString(csv_string);
converter.close();
outputStream.close();
ztoolkit.log(fp.file.path)
// Zotero.Utilities.Internal.exec("excel.exe", [
// fp.file.path,
// ]);
});
// setTimeout(() => {
// ztoolkit.log(fp.file)
// Zotero.Utilities.Internal.exec("excel", [
// fp.file,
// ]);
// }, 1000)

}
async function getScaleData(collectionOrItem: "collection" | "item") {
const items = await getSelectedItems(collectionOrItem);
Expand Down
21 changes: 16 additions & 5 deletions src/utils/Relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { waitFor, waitUtilAsync } from "./wait";
import { getItem } from "./zzlb";

export class Relations {
item: Zotero.Item;
item!: Zotero.Item;
constructor(itemOrKeyOrId: Zotero.Item | string | number) {
this.item = getItem(itemOrKeyOrId);
if (!this.item) {
const item = getItem(itemOrKeyOrId);
if (!item) {
//修复新建item的时候getItem返回的false,需要等待才能获取的问题
setTimeout(() => {
waitUtilAsync(() => !!(this.item = getItem(itemOrKeyOrId)));
setTimeout(async () => {
const item = await waitFor(() => getItem(itemOrKeyOrId));
if (item) {
this.item = item
}
});
} else {
this.item = item
}
}
// static RelationsPredicate="link:annotation" as _ZoteroTypes.RelationsPredicate;
Expand Down Expand Up @@ -50,13 +55,15 @@ export class Relations {
return this.getLinkRelations().map((toItemURI) => getItem(Zotero.URI.getURIItemID(toItemURI) || ""));
}
getLinkRelations() {
if (!this.item) return []
try {
const rs = this.item.getRelations() as any;
return (rs[Relations.RelationsPredicate] as string[]) || [];
} catch (error) {
ztoolkit.log("新创建的item会触发getRelations错误,重新获取,再尝试一遍", error);
try {
this.item = getItem(this.item.key);
if (!this.item) return []
const rs = this.item.getRelations() as any;
return (rs[Relations.RelationsPredicate] as string[]) || [];
} catch (error) {
Expand All @@ -76,6 +83,7 @@ export class Relations {
// this.setTag();}
// }
setRelationsTag() {
if (!this.item) return;
if (this.getLinkRelations().length > 0) {
if (!this.item.hasTag(Relations.RelationsTag)) this.item.addTag(Relations.RelationsTag, 1);
} else {
Expand All @@ -84,11 +92,13 @@ export class Relations {
// this.item.saveTx();
}
getOpenPdfURI() {
if (!this.item) return ``;
return `zotero://open-pdf/library/items/${this.item.parentItemKey}?page=${this.item.annotationPageLabel}&annotation=${this.item.key}`;
}
// removeRelations(openPdfs: string[]) {
// const itemURIs= Relations.mapOpenPdf(openPdfs).map(a=>getItem(a.annotationKey)).map(a=>Zotero.URI.getItemURI(a))
removeRelations(itemURIs: string[]) {
if (!this.item) return ``;
const thisItemURI = Zotero.URI.getItemURI(this.item);
const clearAll = itemURIs.includes(thisItemURI);
// const itemURIs= Relations.mapOpenPdf(openPdfs).map(a=>getItem(a.annotationKey)).map(a=>Zotero.URI.getItemURI(a))
Expand All @@ -106,6 +116,7 @@ export class Relations {
const id = Zotero.URI.getURIItemID(f);
if (id) {
const item = getItem(id);
if (!item) return ``;
const r = new Relations(item);
r.removeRelations([thisItemURI]);
}
Expand Down
27 changes: 14 additions & 13 deletions src/utils/zzlb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@ const memAllTagsInLibraryAsync = memoize(async () => {
);
const itemTags = getPref("item-tags")
? items.flatMap((f) =>
f.getTags().map((a) => ({
tag: a.tag,
type: a.type,
dateModified: f.dateModified,
})),
)
f.getTags().map((a) => ({
tag: a.tag,
type: a.type,
dateModified: f.dateModified,
})),
)
: [];
return groupBy([...tags, ...itemTags], (t14) => t14.tag);
});
Expand Down Expand Up @@ -337,7 +337,7 @@ export function isDebug() {
}
export function getItem(itemOrKeyOrId: Zotero.Item | string | number) {
return typeof itemOrKeyOrId == "number"
? Zotero.Items.get(itemOrKeyOrId)
? (Zotero.Items.get(itemOrKeyOrId) as Zotero.Item)
: typeof itemOrKeyOrId == "string"
? (Zotero.Items.getByLibraryAndKey(Zotero.Libraries.userLibraryID, itemOrKeyOrId) as Zotero.Item)
: itemOrKeyOrId;
Expand All @@ -347,6 +347,7 @@ export async function openAnnotation(itemOrKeyOrId: Zotero.Item | string | numbe
let pdfDoc: Document | undefined = undefined;
const item = getItem(itemOrKeyOrId);

if (!item) return;
await Zotero.FileHandlers.open(item, {
location: {
annotationID: annotationKey,
Expand Down Expand Up @@ -378,7 +379,7 @@ export async function openAnnotation(itemOrKeyOrId: Zotero.Item | string | numbe
}
}

export async function injectCSSToReader() {}
export async function injectCSSToReader() { }

export const memSVG = memoize(
async (href: string) => await getFileContent(href),
Expand Down Expand Up @@ -414,11 +415,11 @@ export async function injectCSS(doc: Document | HTMLDivElement, filename: string
ignoreIfExists: true,
},
doc.querySelector("linkset") ||
doc.querySelector("head") ||
doc.querySelector("body") ||
doc.querySelector("div") ||
doc.children[0] ||
doc,
doc.querySelector("head") ||
doc.querySelector("body") ||
doc.querySelector("div") ||
doc.children[0] ||
doc,
);
// ztoolkit.log("加载css", d);
}
Expand Down

0 comments on commit ac628e9

Please sign in to comment.