Skip to content

Commit

Permalink
🎨 fix #9409
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Oct 14, 2023
1 parent 879fdd8 commit c8924e3
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 144 deletions.
3 changes: 3 additions & 0 deletions app/src/editor/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export const openFileById = async (options: {
afterOpen?: () => void
}) => {
const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
if (response.code === -1) {
return;
}
if (response.code === 3) {
showMessage(response.msg);
return;
Expand Down
16 changes: 14 additions & 2 deletions app/src/layout/dock/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ export const openBacklink = async (options: {
if (!wnd) {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
const newWnd = wnd.split("lr");
if (!options.rootId) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
options.rootId = response.data.rootID;
options.useBlockId = response.data.rootID !== response.data.id;
options.title = response.data.name || "Untitled";
} else if (!options.title) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
options.title = response.data.name || "Untitled";
}
const newWnd = wnd.split("lr");
newWnd.addTab(new Tab({
icon: "iconLink",
title: options.title,
Expand Down Expand Up @@ -82,16 +88,22 @@ export const openGraph = async (options: {
if (!wnd) {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
const newWnd = wnd.split("lr");
if (!options.rootId) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
options.rootId = response.data.rootID;
options.useBlockId = response.data.rootID !== response.data.id;
options.title = response.data.name || "Untitled";
} else if (!options.title) {
const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
options.title = response.data.name || "Untitled";
}
const newWnd = wnd.split("lr");
newWnd.addTab(new Tab({
icon: "iconGraph",
title: options.title,
Expand Down
289 changes: 148 additions & 141 deletions app/src/menus/protyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,37 +155,39 @@ export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
const id = nodeElement.getAttribute("data-node-id");
let oldHTML = nodeElement.outerHTML;
window.siyuan.menus.menu.remove();
window.siyuan.menus.menu.append(new MenuItem({
label: `<input style="margin: 4px 0" class="b3-text-field fn__block" placeholder="${window.siyuan.languages.anchor}">`,
bind(menuItemElement) {
const inputElement = menuItemElement.querySelector("input");
inputElement.value = element.getAttribute("data-subtype") === "d" ? "" : element.textContent;
inputElement.addEventListener("input", () => {
if (inputElement.value) {
// 不能使用 textContent,否则 < 会变为 &lt;
element.innerHTML = Lute.EscapeHTMLStr(inputElement.value);
} else {
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
element.innerHTML = response.data;
});
}
element.setAttribute("data-subtype", inputElement.value ? "s" : "d");
});
inputElement.addEventListener("keydown", (event) => {
if (event.isComposing) {
return;
}
if (event.key === "Enter" && !event.isComposing) {
window.siyuan.menus.menu.remove();
} else if (electronUndo(event)) {
return;
}
});
}
}).element);
window.siyuan.menus.menu.append(new MenuItem({
type: "separator"
}).element);
if (!protyle.disabled) {
window.siyuan.menus.menu.append(new MenuItem({
label: `<input style="margin: 4px 0" class="b3-text-field fn__block" placeholder="${window.siyuan.languages.anchor}">`,
bind(menuItemElement) {
const inputElement = menuItemElement.querySelector("input");
inputElement.value = element.getAttribute("data-subtype") === "d" ? "" : element.textContent;
inputElement.addEventListener("input", () => {
if (inputElement.value) {
// 不能使用 textContent,否则 < 会变为 &lt;
element.innerHTML = Lute.EscapeHTMLStr(inputElement.value);
} else {
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
element.innerHTML = response.data;
});
}
element.setAttribute("data-subtype", inputElement.value ? "s" : "d");
});
inputElement.addEventListener("keydown", (event) => {
if (event.isComposing) {
return;
}
if (event.key === "Enter" && !event.isComposing) {
window.siyuan.menus.menu.remove();
} else if (electronUndo(event)) {
return;
}
});
}
}).element);
window.siyuan.menus.menu.append(new MenuItem({
type: "separator"
}).element);
}
/// #if !MOBILE
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.openBy,
Expand Down Expand Up @@ -282,105 +284,107 @@ export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
}).element);
window.siyuan.menus.menu.append(new MenuItem({type: "separator"}).element);
/// #endif
let submenu: IMenu[] = [];
if (element.getAttribute("data-subtype") === "s") {
submenu.push({
label: window.siyuan.languages.turnToDynamic,
click() {
element.setAttribute("data-subtype", "d");
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
element.innerHTML = response.data;
if (!protyle.disabled) {
let submenu: IMenu[] = [];
if (element.getAttribute("data-subtype") === "s") {
submenu.push({
label: window.siyuan.languages.turnToDynamic,
click() {
element.setAttribute("data-subtype", "d");
fetchPost("/api/block/getRefText", {id: refBlockId}, (response) => {
element.innerHTML = response.data;
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
});
focusByRange(protyle.toolbar.range);
}
});
} else {
submenu.push({
label: window.siyuan.languages.turnToStatic,
click() {
element.setAttribute("data-subtype", "s");
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
});
focusByRange(protyle.toolbar.range);
}
});
}
submenu = submenu.concat([{
label: window.siyuan.languages.text,
click() {
element.outerHTML = `${element.innerHTML}<wbr>`;
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}
}, {
label: "*",
click() {
element.setAttribute("data-subtype", "s");
element.textContent = "*";
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByRange(protyle.toolbar.range);
}
});
} else {
submenu.push({
label: window.siyuan.languages.turnToStatic,
}, {
label: window.siyuan.languages.text + " *",
click() {
element.insertAdjacentHTML("beforebegin", element.innerHTML + " ");
element.setAttribute("data-subtype", "s");
element.textContent = "*";
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByRange(protyle.toolbar.range);
}
});
}
submenu = submenu.concat([{
label: window.siyuan.languages.text,
click() {
element.outerHTML = `${element.innerHTML}<wbr>`;
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}
}, {
label: "*",
click() {
element.setAttribute("data-subtype", "s");
element.textContent = "*";
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByRange(protyle.toolbar.range);
}
}, {
label: window.siyuan.languages.text + " *",
click() {
element.insertAdjacentHTML("beforebegin", element.innerHTML + " ");
element.setAttribute("data-subtype", "s");
element.textContent = "*";
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByRange(protyle.toolbar.range);
}
}, {
label: window.siyuan.languages.link,
icon: "iconLink",
click() {
element.outerHTML = `<span data-type="a" data-href="siyuan://blocks/${element.getAttribute("data-id")}">${element.innerHTML}</span><wbr>`;
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}, {
label: window.siyuan.languages.link,
icon: "iconLink",
click() {
element.outerHTML = `<span data-type="a" data-href="siyuan://blocks/${element.getAttribute("data-id")}">${element.innerHTML}</span><wbr>`;
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}
}]);
if (element.parentElement.textContent.trim() === element.textContent.trim() && element.parentElement.tagName === "DIV") {
submenu.push({
label: window.siyuan.languages.blockEmbed,
icon: "iconSQL",
click() {
const html = `<div data-content="select * from blocks where id='${refBlockId}'" data-node-id="${id}" data-type="NodeBlockQueryEmbed" class="render-node" updated="${dayjs().format("YYYYMMDDHHmmss")}">${nodeElement.querySelector(".protyle-attr").outerHTML}</div>`;
nodeElement.outerHTML = html;
updateTransaction(protyle, id, html, oldHTML);
blockRender(protyle, protyle.wysiwyg.element);
}
});
}
}]);
if (element.parentElement.textContent.trim() === element.textContent.trim() && element.parentElement.tagName === "DIV") {
submenu.push({
label: window.siyuan.languages.blockEmbed,
icon: "iconSQL",
label: window.siyuan.languages.defBlock,
click() {
fetchPost("/api/block/swapBlockRef", {
refID: id,
defID: refBlockId,
includeChildren: false
});
}
});
submenu.push({
label: window.siyuan.languages.defBlockChildren,
click() {
const html = `<div data-content="select * from blocks where id='${refBlockId}'" data-node-id="${id}" data-type="NodeBlockQueryEmbed" class="render-node" updated="${dayjs().format("YYYYMMDDHHmmss")}">${nodeElement.querySelector(".protyle-attr").outerHTML}</div>`;
nodeElement.outerHTML = html;
updateTransaction(protyle, id, html, oldHTML);
blockRender(protyle, protyle.wysiwyg.element);
fetchPost("/api/block/swapBlockRef", {
refID: id,
defID: refBlockId,
includeChildren: true
});
}
});
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.turnInto,
icon: "iconRefresh",
submenu
}).element);
}
submenu.push({
label: window.siyuan.languages.defBlock,
click() {
fetchPost("/api/block/swapBlockRef", {
refID: id,
defID: refBlockId,
includeChildren: false
});
}
});
submenu.push({
label: window.siyuan.languages.defBlockChildren,
click() {
fetchPost("/api/block/swapBlockRef", {
refID: id,
defID: refBlockId,
includeChildren: true
});
}
});
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.turnInto,
icon: "iconRefresh",
submenu
}).element);
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.copy,
icon: "iconCopy",
Expand All @@ -389,18 +393,19 @@ export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
writeText(`((${refBlockId} ${quote}${element.textContent}${quote}))`);
}
}).element);
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.remove,
icon: "iconTrashcan",
click() {
element.insertAdjacentHTML("afterend", "<wbr>");
element.remove();
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}
}).element);

if (!protyle.disabled) {
window.siyuan.menus.menu.append(new MenuItem({
label: window.siyuan.languages.remove,
icon: "iconTrashcan",
click() {
element.insertAdjacentHTML("afterend", "<wbr>");
element.remove();
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
focusByWbr(nodeElement, protyle.toolbar.range);
}
}).element);
}
if (protyle?.app?.plugins) {
emitOpenMenu({
plugins: protyle.app.plugins,
Expand All @@ -419,20 +424,22 @@ export const refMenu = (protyle: IProtyle, element: HTMLElement) => {
y: rect.top + 26,
h: 26
});
window.siyuan.menus.menu.element.querySelector("input").select();
window.siyuan.menus.menu.removeCB = () => {
if (nodeElement.outerHTML !== oldHTML) {
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
oldHTML = nodeElement.outerHTML;
}
const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0);
if (currentRange && !protyle.element.contains(currentRange.startContainer)) {
protyle.toolbar.range.selectNodeContents(element);
protyle.toolbar.range.collapse(false);
focusByRange(protyle.toolbar.range);
}
};
if (!protyle.disabled) {
window.siyuan.menus.menu.element.querySelector("input").select();
window.siyuan.menus.menu.removeCB = () => {
if (nodeElement.outerHTML !== oldHTML) {
nodeElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));
updateTransaction(protyle, id, nodeElement.outerHTML, oldHTML);
oldHTML = nodeElement.outerHTML;
}
const currentRange = getSelection().rangeCount === 0 ? undefined : getSelection().getRangeAt(0);
if (currentRange && !protyle.element.contains(currentRange.startContainer)) {
protyle.toolbar.range.selectNodeContents(element);
protyle.toolbar.range.collapse(false);
focusByRange(protyle.toolbar.range);
}
};
}
};

export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
Expand Down Expand Up @@ -1664,7 +1671,7 @@ export const setFold = (protyle: IProtyle, nodeElement: Element, isOpen?: boolea
focusBlock(nodeElement, undefined, false);
}
}
nodeElement.querySelectorAll(".img--select, .av__cell--select, .av__row--select").forEach((item:HTMLElement) => {
nodeElement.querySelectorAll(".img--select, .av__cell--select, .av__row--select").forEach((item: HTMLElement) => {
if (item.classList.contains("av__row--select")) {
item.classList.remove("av__row--select");
item.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconUncheck");
Expand Down
Loading

0 comments on commit c8924e3

Please sign in to comment.