2022-10-05 10:50:42 +08:00
|
|
|
|
import {focusByRange} from "./selection";
|
2022-10-30 09:51:07 +08:00
|
|
|
|
import {fetchPost} from "../../util/fetch";
|
2022-11-22 00:56:37 +08:00
|
|
|
|
import {Constants} from "../../constants";
|
2022-10-05 10:50:42 +08:00
|
|
|
|
|
2022-09-10 21:22:11 +08:00
|
|
|
|
export const openByMobile = (uri: string) => {
|
2022-07-27 07:17:45 +08:00
|
|
|
|
if (!uri) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (window.siyuan.config.system.container === "ios") {
|
|
|
|
|
|
window.location.href = uri;
|
2023-09-05 16:12:29 +08:00
|
|
|
|
} else if (isInAndroid()) {
|
2022-07-27 07:17:45 +08:00
|
|
|
|
window.JSAndroid.openExternal(uri);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
window.open(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-04-17 17:51:45 +08:00
|
|
|
|
export const readText = () => {
|
2023-09-05 16:12:29 +08:00
|
|
|
|
if (isInAndroid()) {
|
2022-10-07 11:00:52 +08:00
|
|
|
|
return window.JSAndroid.readClipboard();
|
|
|
|
|
|
}
|
|
|
|
|
|
return navigator.clipboard.readText();
|
2022-10-08 15:23:56 +08:00
|
|
|
|
};
|
2022-10-07 11:00:52 +08:00
|
|
|
|
|
2023-04-17 17:51:45 +08:00
|
|
|
|
export const writeText = (text: string) => {
|
2022-10-05 21:29:56 +08:00
|
|
|
|
let range: Range;
|
2022-10-05 10:50:42 +08:00
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
|
range = getSelection().getRangeAt(0).cloneRange();
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
try {
|
2022-06-16 14:02:44 +08:00
|
|
|
|
// navigator.clipboard.writeText 抛出异常不进入 catch,这里需要先处理移动端复制
|
2023-09-05 16:12:29 +08:00
|
|
|
|
if (isInAndroid()) {
|
2022-06-16 14:02:44 +08:00
|
|
|
|
window.JSAndroid.writeClipboard(text);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-09-05 16:12:29 +08:00
|
|
|
|
if (isInIOS()) {
|
2022-06-16 14:02:44 +08:00
|
|
|
|
window.webkit.messageHandlers.setClipboard.postMessage(text);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
|
|
} catch (e) {
|
2023-09-05 16:12:29 +08:00
|
|
|
|
if (isInIOS()) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.webkit.messageHandlers.setClipboard.postMessage(text);
|
2023-09-05 16:12:29 +08:00
|
|
|
|
} else if (isInAndroid()) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
window.JSAndroid.writeClipboard(text);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const textElement = document.createElement("textarea");
|
|
|
|
|
|
textElement.value = text;
|
|
|
|
|
|
textElement.style.position = "fixed"; //avoid scrolling to bottom
|
|
|
|
|
|
document.body.appendChild(textElement);
|
|
|
|
|
|
textElement.focus();
|
|
|
|
|
|
textElement.select();
|
|
|
|
|
|
document.execCommand("copy");
|
|
|
|
|
|
document.body.removeChild(textElement);
|
2022-10-05 10:50:42 +08:00
|
|
|
|
if (range) {
|
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-11-22 00:56:37 +08:00
|
|
|
|
export const copyPlainText = async (text: string) => {
|
|
|
|
|
|
text = text.replace(new RegExp(Constants.ZWSP, "g"), ""); // `复制纯文本` 时移除所有零宽空格 https://github.com/siyuan-note/siyuan/issues/6674
|
|
|
|
|
|
await writeText(text);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// 用户 iPhone 点击延迟/需要双击的处理
|
|
|
|
|
|
export const getEventName = () => {
|
2023-09-06 17:19:11 +08:00
|
|
|
|
if (isIPhone()) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return "touchstart";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return "click";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 区别 mac 上的 ctrl 和 meta
|
2023-04-20 18:40:29 +08:00
|
|
|
|
export const isCtrl = (event: KeyboardEvent | MouseEvent) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if (isMac()) {
|
|
|
|
|
|
// mac
|
|
|
|
|
|
if (event.metaKey && !event.ctrlKey) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (!event.metaKey && event.ctrlKey) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-08-03 12:37:14 +08:00
|
|
|
|
export const isHuawei = () => {
|
|
|
|
|
|
return window.siyuan.config.system.osPlatform.toLowerCase().indexOf("huawei") > -1;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-09-06 17:19:11 +08:00
|
|
|
|
export const isIPhone = () => {
|
2023-09-06 17:20:52 +08:00
|
|
|
|
return navigator.userAgent.indexOf("iPhone") > -1;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const isIPad = () => {
|
|
|
|
|
|
return navigator.userAgent.indexOf("iPad") > -1;
|
|
|
|
|
|
};
|
2023-09-06 17:19:11 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
export const isMac = () => {
|
|
|
|
|
|
return navigator.platform.toUpperCase().indexOf("MAC") > -1;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2023-09-05 16:12:29 +08:00
|
|
|
|
export const isInAndroid = () => {
|
2023-09-06 17:20:52 +08:00
|
|
|
|
return window.siyuan.config.system.container === "android" && window.JSAndroid;
|
|
|
|
|
|
};
|
2023-09-05 16:12:29 +08:00
|
|
|
|
|
|
|
|
|
|
export const isInIOS = () => {
|
2023-09-06 17:20:52 +08:00
|
|
|
|
return window.siyuan.config.system.container === "ios" && window.webkit?.messageHandlers;
|
|
|
|
|
|
};
|
2023-09-05 16:12:29 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// Mac,Windows 快捷键展示
|
|
|
|
|
|
export const updateHotkeyTip = (hotkey: string) => {
|
|
|
|
|
|
if (/Mac/.test(navigator.platform) || navigator.platform === "iPhone") {
|
|
|
|
|
|
return hotkey;
|
|
|
|
|
|
}
|
2022-09-10 21:22:11 +08:00
|
|
|
|
|
|
|
|
|
|
const KEY_MAP = new Map(Object.entries({
|
|
|
|
|
|
"⌘": "Ctrl",
|
2023-05-03 14:43:19 +08:00
|
|
|
|
"⌃": "Ctrl",
|
2022-09-10 21:22:11 +08:00
|
|
|
|
"⇧": "Shift",
|
|
|
|
|
|
"⌥": "Alt",
|
|
|
|
|
|
"⇥": "Tab",
|
|
|
|
|
|
"⌫": "Backspace",
|
|
|
|
|
|
"⌦": "Delete",
|
|
|
|
|
|
"↩": "Enter",
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
2022-09-10 21:34:44 +08:00
|
|
|
|
const keys = [];
|
2022-09-10 21:22:11 +08:00
|
|
|
|
|
|
|
|
|
|
if (hotkey.indexOf("⌘") > -1) keys.push(KEY_MAP.get("⌘"));
|
|
|
|
|
|
if (hotkey.indexOf("⇧") > -1) keys.push(KEY_MAP.get("⇧"));
|
|
|
|
|
|
if (hotkey.indexOf("⌥") > -1) keys.push(KEY_MAP.get("⌥"));
|
|
|
|
|
|
|
2022-09-18 09:39:07 +08:00
|
|
|
|
// 不能去最后一个,需匹配 F2
|
|
|
|
|
|
const lastKey = hotkey.replace(/⌘|⇧|⌥/g, "");
|
2022-09-25 12:25:01 +08:00
|
|
|
|
if (lastKey) {
|
|
|
|
|
|
keys.push(KEY_MAP.get(lastKey) || lastKey);
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-10 21:22:11 +08:00
|
|
|
|
|
|
|
|
|
|
return keys.join("+");
|
2022-05-26 15:18:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-03-14 23:16:06 +08:00
|
|
|
|
export const getLocalStorage = (cb: () => void) => {
|
2023-01-01 15:18:32 +08:00
|
|
|
|
fetchPost("/api/storage/getLocalStorage", undefined, (response) => {
|
|
|
|
|
|
window.siyuan.storage = response.data;
|
|
|
|
|
|
// 历史数据迁移
|
|
|
|
|
|
const defaultStorage: any = {};
|
2023-08-12 23:51:14 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_SEARCHASSET] = {
|
|
|
|
|
|
keys: [],
|
|
|
|
|
|
col: "",
|
|
|
|
|
|
row: "",
|
|
|
|
|
|
layout: 0,
|
|
|
|
|
|
method: 0,
|
2023-08-18 12:09:55 +08:00
|
|
|
|
types: {},
|
2023-08-12 23:51:14 +08:00
|
|
|
|
sort: 0,
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
};
|
2023-08-18 12:09:55 +08:00
|
|
|
|
Constants.SIYUAN_ASSETS_SEARCH.forEach(type => {
|
2023-08-18 20:03:04 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_SEARCHASSET].types[type] = true;
|
|
|
|
|
|
});
|
2023-03-22 11:56:58 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_SEARCHKEYS] = {
|
2023-01-01 15:18:32 +08:00
|
|
|
|
keys: [],
|
|
|
|
|
|
replaceKeys: [],
|
|
|
|
|
|
col: "",
|
|
|
|
|
|
row: "",
|
2023-01-04 13:52:41 +08:00
|
|
|
|
layout: 0,
|
|
|
|
|
|
colTab: "",
|
|
|
|
|
|
rowTab: "",
|
|
|
|
|
|
layoutTab: 0
|
2023-01-01 15:18:32 +08:00
|
|
|
|
};
|
2023-04-07 12:13:19 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_PDFTHEME] = {
|
|
|
|
|
|
light: "light",
|
|
|
|
|
|
dark: "dark",
|
|
|
|
|
|
annoColor: "var(--b3-pdf-background1)"
|
|
|
|
|
|
};
|
2023-03-17 20:51:52 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_LAYOUTS] = []; // {name: "", layout:{}}
|
2023-03-25 10:57:13 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_AI] = []; // {name: "", memo: ""}
|
2023-06-03 21:45:24 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_PLUGINTOPUNPIN] = [];
|
2023-01-01 15:18:32 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_BAZAAR] = {
|
|
|
|
|
|
theme: "0",
|
|
|
|
|
|
template: "0",
|
|
|
|
|
|
icon: "0",
|
|
|
|
|
|
widget: "0",
|
|
|
|
|
|
};
|
|
|
|
|
|
defaultStorage[Constants.LOCAL_EXPORTWORD] = {removeAssets: false, mergeSubdocs: false};
|
|
|
|
|
|
defaultStorage[Constants.LOCAL_EXPORTPDF] = {
|
|
|
|
|
|
landscape: false,
|
|
|
|
|
|
marginType: "0",
|
|
|
|
|
|
scale: 1,
|
|
|
|
|
|
pageSize: "A4",
|
|
|
|
|
|
removeAssets: true,
|
|
|
|
|
|
keepFold: false,
|
|
|
|
|
|
mergeSubdocs: false,
|
|
|
|
|
|
};
|
2023-01-22 12:21:20 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_EXPORTIMG] = {
|
|
|
|
|
|
keepFold: false,
|
|
|
|
|
|
};
|
2023-01-01 15:18:32 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_DOCINFO] = {
|
|
|
|
|
|
id: "",
|
|
|
|
|
|
};
|
|
|
|
|
|
defaultStorage[Constants.LOCAL_FONTSTYLES] = [];
|
2023-03-22 11:56:58 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_SEARCHDATA] = {
|
2023-04-21 10:23:35 +08:00
|
|
|
|
page: 1,
|
2023-01-01 15:18:32 +08:00
|
|
|
|
sort: 0,
|
|
|
|
|
|
group: 0,
|
|
|
|
|
|
hasReplace: false,
|
|
|
|
|
|
method: 0,
|
|
|
|
|
|
hPath: "",
|
|
|
|
|
|
idPath: [],
|
|
|
|
|
|
k: "",
|
|
|
|
|
|
r: "",
|
|
|
|
|
|
types: {
|
|
|
|
|
|
document: window.siyuan.config.search.document,
|
|
|
|
|
|
heading: window.siyuan.config.search.heading,
|
|
|
|
|
|
list: window.siyuan.config.search.list,
|
|
|
|
|
|
listItem: window.siyuan.config.search.listItem,
|
|
|
|
|
|
codeBlock: window.siyuan.config.search.codeBlock,
|
|
|
|
|
|
htmlBlock: window.siyuan.config.search.htmlBlock,
|
|
|
|
|
|
mathBlock: window.siyuan.config.search.mathBlock,
|
|
|
|
|
|
table: window.siyuan.config.search.table,
|
|
|
|
|
|
blockquote: window.siyuan.config.search.blockquote,
|
|
|
|
|
|
superBlock: window.siyuan.config.search.superBlock,
|
|
|
|
|
|
paragraph: window.siyuan.config.search.paragraph,
|
2023-01-19 14:07:38 +08:00
|
|
|
|
embedBlock: window.siyuan.config.search.embedBlock,
|
2023-10-19 00:01:42 +08:00
|
|
|
|
databaseBlock: window.siyuan.config.search.databaseBlock,
|
2023-01-01 15:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2023-01-06 20:26:14 +08:00
|
|
|
|
defaultStorage[Constants.LOCAL_ZOOM] = 1;
|
2023-01-01 15:18:32 +08:00
|
|
|
|
|
2023-08-12 23:51:14 +08:00
|
|
|
|
[Constants.LOCAL_EXPORTIMG, Constants.LOCAL_SEARCHKEYS, Constants.LOCAL_PDFTHEME, Constants.LOCAL_BAZAAR,
|
|
|
|
|
|
Constants.LOCAL_EXPORTWORD, Constants.LOCAL_EXPORTPDF, Constants.LOCAL_DOCINFO, Constants.LOCAL_FONTSTYLES,
|
|
|
|
|
|
Constants.LOCAL_SEARCHDATA, Constants.LOCAL_ZOOM, Constants.LOCAL_LAYOUTS, Constants.LOCAL_AI,
|
|
|
|
|
|
Constants.LOCAL_PLUGINTOPUNPIN, Constants.LOCAL_SEARCHASSET].forEach((key) => {
|
2023-01-01 15:18:32 +08:00
|
|
|
|
if (typeof response.data[key] === "string") {
|
|
|
|
|
|
try {
|
2023-07-30 14:15:27 +08:00
|
|
|
|
const parseData = JSON.parse(response.data[key]);
|
|
|
|
|
|
if (typeof parseData === "number") {
|
|
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/8852 Object.assign 会导致 number to Number
|
|
|
|
|
|
window.siyuan.storage[key] = parseData;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
window.siyuan.storage[key] = Object.assign(defaultStorage[key], parseData);
|
|
|
|
|
|
}
|
2023-01-01 15:18:32 +08:00
|
|
|
|
} catch (e) {
|
2023-04-07 12:13:19 +08:00
|
|
|
|
window.siyuan.storage[key] = defaultStorage[key];
|
2023-01-01 15:18:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else if (typeof response.data[key] === "undefined") {
|
2022-12-31 22:14:42 +08:00
|
|
|
|
window.siyuan.storage[key] = defaultStorage[key];
|
|
|
|
|
|
}
|
2023-01-01 15:18:32 +08:00
|
|
|
|
});
|
2023-01-04 15:37:37 +08:00
|
|
|
|
cb();
|
2023-03-22 15:22:45 +08:00
|
|
|
|
|
|
|
|
|
|
// 数据兼容,移除历史数据,3.8.4 移除
|
|
|
|
|
|
fetchPost("/api/storage/removeLocalStorageVals", {
|
2023-03-22 17:39:08 +08:00
|
|
|
|
app: Constants.SIYUAN_APPID,
|
2023-04-07 12:13:19 +08:00
|
|
|
|
keys: ["leftColumn", "local-searchkey", "local-searchedata", "local-searchekeys", "local-searchetabdata", "rightColumn", "topBar"]
|
2023-03-22 15:22:45 +08:00
|
|
|
|
});
|
2022-10-30 09:51:07 +08:00
|
|
|
|
});
|
2023-01-01 14:09:23 +08:00
|
|
|
|
};
|
2022-10-30 09:37:46 +08:00
|
|
|
|
|
2023-01-01 15:18:32 +08:00
|
|
|
|
export const setStorageVal = (key: string, val: any) => {
|
|
|
|
|
|
fetchPost("/api/storage/setLocalStorageVal", {
|
2023-01-01 14:10:43 +08:00
|
|
|
|
app: Constants.SIYUAN_APPID,
|
2023-01-01 15:18:32 +08:00
|
|
|
|
key,
|
|
|
|
|
|
val,
|
2022-10-30 09:51:07 +08:00
|
|
|
|
});
|
2022-10-30 09:52:18 +08:00
|
|
|
|
};
|