import {transaction} from "../../wysiwyg/transaction"; import {Constants} from "../../../constants"; export const getLayoutHTML = (data: IAV) => { let html = ""; const view = data.view as IAVGallery; if (data.viewType === "gallery") { let coverFromTitle = ""; if (view.coverFrom === 0) { coverFromTitle = window.siyuan.languages.calcOperatorNone; } else if (view.coverFrom === 1) { coverFromTitle = window.siyuan.languages.contentImage; } else { view.fields.find(item => { if (item.type === "mAsset" && item.id === view.coverFromAssetKeyID) { coverFromTitle = item.name; return true; } }); } html = ` `; } return `
${html}
`; }; export const bindLayoutEvent = (options: { protyle: IProtyle, data: IAV, menuElement: HTMLElement blockElement: Element }) => { const toggleTitleElement = options.menuElement.querySelector('.b3-switch[data-type="toggle-view-title"]') as HTMLInputElement; toggleTitleElement.addEventListener("change", () => { const avID = options.blockElement.getAttribute("data-av-id"); const blockID = options.blockElement.getAttribute("data-node-id"); const checked = toggleTitleElement.checked; transaction(options.protyle, [{ action: "hideAttrViewName", avID, blockID, data: !checked }], [{ action: "hideAttrViewName", avID, blockID, data: checked }]); if (checked) { options.blockElement.querySelector(".av__title").classList.remove("fn__none"); } else { // hide options.blockElement.querySelector(".av__title").classList.add("fn__none"); } if (options.data.viewType === "gallery") { const galleryElement = options.blockElement.querySelector(".av__gallery"); if (checked) { galleryElement.classList.remove("av__gallery--top"); } else { // hide galleryElement.classList.add("av__gallery--top"); } } }); if (options.data.viewType !== "gallery") { return; } const toggleFitElement = options.menuElement.querySelector('.b3-switch[data-type="toggle-gallery-fit"]') as HTMLInputElement; toggleFitElement.addEventListener("change", () => { const avID = options.blockElement.getAttribute("data-av-id"); const blockID = options.blockElement.getAttribute("data-node-id"); const checked = toggleFitElement.checked; transaction(options.protyle, [{ action: "setAttrViewFitImage", avID, blockID, data: checked }], [{ action: "setAttrViewFitImage", avID, blockID, data: !checked }]); options.blockElement.querySelectorAll(".av__gallery-img").forEach(item => { if (checked) { item.classList.add("av__gallery-img--fit"); } else { item.classList.remove("av__gallery-img--fit"); } }); }); const toggleIconElement = options.menuElement.querySelector('.b3-switch[data-type="toggle-gallery-icon"]') as HTMLInputElement; toggleIconElement.addEventListener("change", () => { const avID = options.blockElement.getAttribute("data-av-id"); const blockID = options.blockElement.getAttribute("data-node-id"); const checked = toggleIconElement.checked; transaction(options.protyle, [{ action: "setAttrViewShowIcon", avID, blockID, data: checked }], [{ action: "setAttrViewShowIcon", avID, blockID, data: !checked }]); }); const toggleWrapElement = options.menuElement.querySelector('.b3-switch[data-type="toggle-gallery-wrap"]') as HTMLInputElement; toggleWrapElement.addEventListener("change", () => { const avID = options.blockElement.getAttribute("data-av-id"); const blockID = options.blockElement.getAttribute("data-node-id"); const checked = toggleWrapElement.checked; transaction(options.protyle, [{ action: "setAttrViewWrapField", avID, blockID, data: checked }], [{ action: "setAttrViewWrapField", avID, blockID, data: !checked }]); }); }; export const updateLayout = (options: { data: IAV nodeElement: Element, protyle: IProtyle, target: HTMLElement }) => { if (options.target.classList.contains("av__layout-item--select")) { return; } const avID = options.nodeElement.getAttribute("data-av-id"); const blockID = options.nodeElement.getAttribute("data-node-id"); const layout = options.target.getAttribute("data-view-type"); transaction(options.protyle, [{ action: "changeAttrViewLayout", avID, blockID, layout }], [{ action: "changeAttrViewLayout", avID, blockID, layout: options.data.viewType }]); options.target.parentElement.querySelector(".av__layout-item--select").classList.remove("av__layout-item--select"); options.target.classList.add("av__layout-item--select"); };