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-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;
|
|
|
|
|
|
} else if (window.siyuan.config.system.container === "android" && window.JSAndroid) {
|
|
|
|
|
|
window.JSAndroid.openExternal(uri);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
window.open(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-10-30 09:51:07 +08:00
|
|
|
|
export const readText = async () => {
|
2022-10-07 11:00:52 +08:00
|
|
|
|
if ("android" === window.siyuan.config.system.container && window.JSAndroid) {
|
|
|
|
|
|
return window.JSAndroid.readClipboard();
|
|
|
|
|
|
}
|
|
|
|
|
|
return navigator.clipboard.readText();
|
2022-10-08 15:23:56 +08:00
|
|
|
|
};
|
2022-10-07 11:00:52 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
export const writeText = async (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,这里需要先处理移动端复制
|
|
|
|
|
|
if ("android" === window.siyuan.config.system.container && window.JSAndroid) {
|
|
|
|
|
|
window.JSAndroid.writeClipboard(text);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2022-06-16 14:03:01 +08:00
|
|
|
|
if ("ios" === window.siyuan.config.system.container && window.webkit?.messageHandlers) {
|
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) {
|
|
|
|
|
|
if (window.siyuan.config.system.container === "ios" && window.webkit?.messageHandlers) {
|
|
|
|
|
|
window.webkit.messageHandlers.setClipboard.postMessage(text);
|
|
|
|
|
|
} else if (window.siyuan.config.system.container === "android" && window.JSAndroid) {
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 用户 iPhone 点击延迟/需要双击的处理
|
|
|
|
|
|
export const getEventName = () => {
|
|
|
|
|
|
if (navigator.userAgent.indexOf("iPhone") > -1) {
|
|
|
|
|
|
return "touchstart";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return "click";
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 区别 mac 上的 ctrl 和 meta
|
|
|
|
|
|
export const isCtrl = (event: KeyboardEvent) => {
|
|
|
|
|
|
if (isMac()) {
|
|
|
|
|
|
// mac
|
|
|
|
|
|
if (event.metaKey && !event.ctrlKey) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (!event.metaKey && event.ctrlKey) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const isMac = () => {
|
|
|
|
|
|
return navigator.platform.toUpperCase().indexOf("MAC") > -1;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
|
|
"⇧": "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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const hotKey2Electron = (key: string) => {
|
|
|
|
|
|
let electronKey = "";
|
|
|
|
|
|
if (key.indexOf("⌘") > -1) {
|
|
|
|
|
|
electronKey += "CommandOrControl+";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (key.indexOf("⇧") > -1) {
|
|
|
|
|
|
electronKey += "Shift+";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (key.indexOf("⌥") > -1) {
|
|
|
|
|
|
electronKey += "Alt+";
|
|
|
|
|
|
}
|
|
|
|
|
|
return electronKey + key.substr(key.length - 1);
|
|
|
|
|
|
};
|
2022-10-30 09:37:46 +08:00
|
|
|
|
|
|
|
|
|
|
export const setLocalStorage = () => {
|
2022-10-30 09:51:07 +08:00
|
|
|
|
fetchPost("/api/system/getLocalStorage", undefined, (response) => {
|
|
|
|
|
|
Object.keys(response.data).forEach(item => {
|
|
|
|
|
|
window.localStorage.setItem(item, response.data[item]);
|
2022-10-30 09:52:18 +08:00
|
|
|
|
});
|
2022-10-30 09:51:07 +08:00
|
|
|
|
});
|
2022-10-30 09:52:18 +08:00
|
|
|
|
};
|
2022-10-30 09:37:46 +08:00
|
|
|
|
|
2022-10-30 09:51:07 +08:00
|
|
|
|
export const exportLocalStorage = (cb: () => void) => {
|
2022-10-30 10:10:55 +08:00
|
|
|
|
fetchPost("/api/system/setLocalStorage", {val: window.localStorage}, () => {
|
2022-10-30 09:51:07 +08:00
|
|
|
|
cb();
|
|
|
|
|
|
});
|
2022-10-30 09:52:18 +08:00
|
|
|
|
};
|