Vanessa 2023-07-31 11:31:50 +08:00
parent d0ee66c0fa
commit da03d8473c
3 changed files with 64 additions and 29 deletions

View file

@ -6,62 +6,69 @@ import {getDefaultOperatorByType, setFilter} from "./filter";
import {genCellValue} from "./cell"; import {genCellValue} from "./cell";
import {openMenuPanel} from "./openMenuPanel"; import {openMenuPanel} from "./openMenuPanel";
export const duplicateCol = (protyle: IProtyle, type: TAVCol, avID: string, colId: string, newValue: string) => { export const duplicateCol = (options: {
protyle: IProtyle,
type: TAVCol,
avID: string,
nodeID: string,
colId: string,
newValue: string
}) => {
const id = Lute.NewNodeID(); const id = Lute.NewNodeID();
const nameMatch = newValue.match(/^(.*) \((\d+)\)$/); const nameMatch = options.newValue.match(/^(.*) \((\d+)\)$/);
if (nameMatch) { if (nameMatch) {
newValue = `${nameMatch[1]} (${parseInt(nameMatch[2]) + 1})`; options.newValue = `${nameMatch[1]} (${parseInt(nameMatch[2]) + 1})`;
} else { } else {
newValue = `${newValue} (1)`; options.newValue = `${options.newValue} (1)`;
} }
if (["select", "mSelect"].includes(type)) { if (["select", "mSelect"].includes(options.type)) {
fetchPost("/api/av/renderAttributeView", {id: avID}, (response) => { fetchPost("/api/av/renderAttributeView", {id: options.avID, nodeID: options.nodeID}, (response) => {
const data = response.data as IAV; const data = response.data as IAV;
let colOptions; let colOptions;
data.view.columns.find((item) => { data.view.columns.find((item) => {
if (item.id === colId) { if (item.id === options.colId) {
colOptions = item.options; colOptions = item.options;
return true; return true;
} }
}); });
transaction(protyle, [{ transaction(options.protyle, [{
action: "addAttrViewCol", action: "addAttrViewCol",
name: newValue, name: options.newValue,
avID, avID: options.avID,
type, type: options.type,
id id
}, { }, {
action: "sortAttrViewCol", action: "sortAttrViewCol",
avID, avID: options.avID,
previousID: colId, previousID: options.colId,
id id
}, { }, {
action: "updateAttrViewColOptions", action: "updateAttrViewColOptions",
id, id,
avID, avID: options.avID,
data: colOptions data: colOptions
}], [{ }], [{
action: "removeAttrViewCol", action: "removeAttrViewCol",
id, id,
avID, avID: options.avID,
}]); }]);
}); });
} else { } else {
transaction(protyle, [{ transaction(options.protyle, [{
action: "addAttrViewCol", action: "addAttrViewCol",
name: newValue, name: options.newValue,
avID, avID: options.avID,
type, type: options.type,
id id
}, { }, {
action: "sortAttrViewCol", action: "sortAttrViewCol",
avID, avID: options.avID,
previousID: colId, previousID: options.colId,
id id
}], [{ }], [{
action: "removeAttrViewCol", action: "removeAttrViewCol",
id, id,
avID, avID: options.avID,
}]); }]);
} }
}; };
@ -255,6 +262,7 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl
const type = cellElement.getAttribute("data-dtype") as TAVCol; const type = cellElement.getAttribute("data-dtype") as TAVCol;
const colId = cellElement.getAttribute("data-col-id"); const colId = cellElement.getAttribute("data-col-id");
const avID = blockElement.getAttribute("data-av-id"); const avID = blockElement.getAttribute("data-av-id");
const nodeID = blockElement.getAttribute("data-node-id");
const menu = new Menu("av-header-cell", () => { const menu = new Menu("av-header-cell", () => {
const newValue = (window.siyuan.menus.menu.element.querySelector(".b3-text-field") as HTMLInputElement).value; const newValue = (window.siyuan.menus.menu.element.querySelector(".b3-text-field") as HTMLInputElement).value;
if (newValue === cellElement.textContent.trim()) { if (newValue === cellElement.textContent.trim()) {
@ -292,7 +300,10 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl
icon: "iconUp", icon: "iconUp",
label: window.siyuan.languages.asc, label: window.siyuan.languages.asc,
click() { click() {
fetchPost("/api/av/renderAttributeView", {id: avID}, (response) => { fetchPost("/api/av/renderAttributeView", {
id: avID,
nodeID
}, (response) => {
transaction(protyle, [{ transaction(protyle, [{
action: "setAttrViewSorts", action: "setAttrViewSorts",
avID: response.data.id, avID: response.data.id,
@ -312,7 +323,10 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl
icon: "iconDown", icon: "iconDown",
label: window.siyuan.languages.desc, label: window.siyuan.languages.desc,
click() { click() {
fetchPost("/api/av/renderAttributeView", {id: avID}, (response) => { fetchPost("/api/av/renderAttributeView", {
id: avID,
nodeID
}, (response) => {
transaction(protyle, [{ transaction(protyle, [{
action: "setAttrViewSorts", action: "setAttrViewSorts",
avID: response.data.id, avID: response.data.id,
@ -332,7 +346,10 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl
icon: "iconFilter", icon: "iconFilter",
label: window.siyuan.languages.filter, label: window.siyuan.languages.filter,
click() { click() {
fetchPost("/api/av/renderAttributeView", {id: avID}, (response) => { fetchPost("/api/av/renderAttributeView", {
id: avID,
nodeID
}, (response) => {
const avData = response.data as IAV; const avData = response.data as IAV;
let filter: IAVFilter; let filter: IAVFilter;
avData.view.filters.find((item) => { avData.view.filters.find((item) => {
@ -390,7 +407,14 @@ export const showColMenu = (protyle: IProtyle, blockElement: HTMLElement, cellEl
icon: "iconCopy", icon: "iconCopy",
label: window.siyuan.languages.duplicate, label: window.siyuan.languages.duplicate,
click() { click() {
duplicateCol(protyle, type, avID, colId, (window.siyuan.menus.menu.element.querySelector(".b3-text-field") as HTMLInputElement).value); duplicateCol({
protyle,
type,
avID,
nodeID,
colId,
newValue: (window.siyuan.menus.menu.element.querySelector(".b3-text-field") as HTMLInputElement).value
});
} }
}); });
menu.addItem({ menu.addItem({

View file

@ -23,7 +23,11 @@ export const openMenuPanel = (options: {
} }
window.siyuan.menus.menu.remove(); window.siyuan.menus.menu.remove();
const avID = options.blockElement.getAttribute("data-av-id"); const avID = options.blockElement.getAttribute("data-av-id");
fetchPost("/api/av/renderAttributeView", {id: avID}, (response) => { const nodeID = options.blockElement.getAttribute("data-node-id")
fetchPost("/api/av/renderAttributeView", {
id: avID,
nodeID
}, (response) => {
const data = response.data as IAV; const data = response.data as IAV;
let html; let html;
if (options.type === "config") { if (options.type === "config") {
@ -565,7 +569,14 @@ export const openMenuPanel = (options: {
} else if (type === "duplicateCol") { } else if (type === "duplicateCol") {
const colId = menuElement.firstElementChild.getAttribute("data-col-id"); const colId = menuElement.firstElementChild.getAttribute("data-col-id");
const colData = data.view.columns.find((item: IAVColumn) => item.id === colId); const colData = data.view.columns.find((item: IAVColumn) => item.id === colId);
duplicateCol(options.protyle, colData.type, avID, colId, colData.name); duplicateCol({
protyle: options.protyle,
type: colData.type,
avID,
colId,
nodeID,
newValue: colData.name
});
avPanelElement.remove(); avPanelElement.remove();
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();

View file

@ -20,7 +20,7 @@ export const avRender = (element: Element, cb?: () => void) => {
if (e.getAttribute("data-render") === "true") { if (e.getAttribute("data-render") === "true") {
return; return;
} }
fetchPost("/api/av/renderAttributeView", {id: e.getAttribute("data-av-id")}, (response) => { fetchPost("/api/av/renderAttributeView", {id: e.getAttribute("data-av-id"), nodeID: e.getAttribute("data-node-id")}, (response) => {
const data = response.data.view as IAVTable; const data = response.data.view as IAVTable;
// header // header
let tableHTML = '<div class="av__row av__row--header"><div class="av__firstcol"><svg style="height: 32px"><use xlink:href="#iconUncheck"></use></svg></div>'; let tableHTML = '<div class="av__row av__row--header"><div class="av__firstcol"><svg style="height: 32px"><use xlink:href="#iconUncheck"></use></svg></div>';