A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
1import {openSearchAV} from "./relation";
2import {transaction} from "../../wysiwyg/transaction";
3import {focusByRange} from "../../util/selection";
4import {hasClosestBlock} from "../../util/hasClosest";
5import * as dayjs from "dayjs";
6
7export const addFilesToDatabase = (fileLiElements: Element[]) => {
8 const srcs: IOperationSrcs[] = [];
9 fileLiElements.forEach(item => {
10 const id = item.getAttribute("data-node-id");
11 if (id) {
12 srcs.push({
13 itemID: Lute.NewNodeID(),
14 id,
15 isDetached: false
16 });
17 }
18 });
19 if (srcs.length > 0) {
20 openSearchAV("", fileLiElements[0] as HTMLElement, (listItemElement) => {
21 const avID = listItemElement.dataset.avId;
22 const viewID = listItemElement.dataset.viewId;
23 transaction(undefined, [{
24 action: "insertAttrViewBlock",
25 ignoreDefaultFill: viewID ? false : true,
26 viewID,
27 avID,
28 srcs,
29 blockID: listItemElement.dataset.blockId
30 }, {
31 action: "doUpdateUpdated",
32 id: listItemElement.dataset.blockId,
33 data: dayjs().format("YYYYMMDDHHmmss"),
34 }]);
35 });
36 }
37};
38
39export const addEditorToDatabase = (protyle: IProtyle, range: Range, type?: string) => {
40 if ((range && protyle.title?.editElement?.contains(range.startContainer)) || type === "title") {
41 openSearchAV("", protyle.breadcrumb.element, (listItemElement) => {
42 const avID = listItemElement.dataset.avId;
43 const viewID = listItemElement.dataset.viewId;
44 transaction(protyle, [{
45 action: "insertAttrViewBlock",
46 ignoreDefaultFill: viewID ? false : true,
47 viewID,
48 avID,
49 srcs: [{
50 itemID: Lute.NewNodeID(),
51 id: protyle.block.rootID,
52 isDetached: false
53 }],
54 blockID: listItemElement.dataset.blockId
55 }, {
56 action: "doUpdateUpdated",
57 id: listItemElement.dataset.blockId,
58 data: dayjs().format("YYYYMMDDHHmmss"),
59 }], [{
60 action: "removeAttrViewBlock",
61 srcIDs: [protyle.block.rootID],
62 avID,
63 }]);
64 focusByRange(range);
65 });
66 } else {
67 let targetElement: HTMLElement;
68 const ids: string[] = [];
69 protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select").forEach((item: HTMLElement) => {
70 if (!targetElement) {
71 targetElement = item;
72 }
73 ids.push(item.getAttribute("data-node-id"));
74 });
75 if (!targetElement) {
76 const nodeElement = hasClosestBlock(range.startContainer);
77 if (nodeElement) {
78 targetElement = nodeElement;
79 ids.push(nodeElement.getAttribute("data-node-id"));
80 }
81 }
82 if (!targetElement) {
83 targetElement = protyle.wysiwyg.element;
84 ids.push(protyle.block.rootID);
85 }
86 openSearchAV("", targetElement, (listItemElement) => {
87 const srcIDs: string[] = [];
88 const srcs: IOperationSrcs[] = [];
89 ids.forEach(item => {
90 srcIDs.push(item);
91 srcs.push({
92 itemID: Lute.NewNodeID(),
93 id: item,
94 isDetached: false
95 });
96 });
97 const avID = listItemElement.dataset.avId;
98 const viewID = listItemElement.dataset.viewId;
99 transaction(protyle, [{
100 action: "insertAttrViewBlock",
101 ignoreDefaultFill: viewID ? false : true,
102 viewID,
103 avID,
104 srcs,
105 blockID: listItemElement.dataset.blockId
106 }, {
107 action: "doUpdateUpdated",
108 id: listItemElement.dataset.blockId,
109 data: dayjs().format("YYYYMMDDHHmmss"),
110 }], [{
111 action: "removeAttrViewBlock",
112 srcIDs,
113 avID,
114 }]);
115 focusByRange(range);
116 });
117 }
118};