-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.tsx
227 lines (215 loc) · 6.76 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import "@logseq/libs";
import { IBatchBlock, SettingSchemaDesc } from "@logseq/libs/dist/LSPlugin.user";
import Sherlock from "sherlockjs";
import { getDateForPage, getDateForPageWithoutBrackets } from "logseq-dateutils";
import React from "react";
import ReactDOM from "react-dom";
import App, { FormValues } from "./App";
import { insertProperlyTemplatedBlock } from "./insertTemplatedBlock";
import { updateTemplates } from "./searchbar";
import SearchBar from "./searchbar";
import { handleClosePopup } from "./handleClosePopup";
import InsertionUI from "./inserterUI";
import { uuidRegex } from "./utils";
/*
* main entry
*/
export function renderApp (){
handleClosePopup()
ReactDOM.render(
<React.StrictMode>
<App/>
</React.StrictMode>,
document.getElementById("app")
);}
let settings: SettingSchemaDesc[] = [
{
key: "Keyboard-Shortcut",
type: "string",
title: "Keyboard Shortcut for Triggering Smartblocks",
description: "keyboard shortcut to trigger smartblock insertion window",
default: "mod+t"
}
]
async function checkTemplate(uuid) {
//Credits to Alex for this implementation https://github.com/QWxleA
//is block(uuid) on a template?
try {
let block = await logseq.Editor.getBlock(uuid);
let checkTPL =
block.properties && block.properties.template != undefined ? true : false;
let checkPRT =
block.parent != null && block.parent.id !== block.page.id ? true : false;
if (checkTPL === false && checkPRT === false) return false;
if (checkTPL === true) return true;
return await checkTemplate(block.parent.id);
} catch (error) {
console.log(error);
}
}
export var valueCount = 0;
export var valueArray: FormValues[] = [];
export var currentValueCount = 0;
export var currentValueArray = [];
export function editValueArray(value: FormValues[]) {
valueArray = value;
}
export function valueZero() {
valueArray = [];
}
async function main() {
updateTemplates()
// logseq.App.onCurrentGraphChanged(updateTemplates)
logseq.useSettingsSchema(settings);
logseq.provideModel({
async insertTemplatedBlock(e: any) {
const { blockUuid, template, sibling, location } = e.dataset;
let blockUuid2: string = blockUuid
console.log(location)
console.log(location.match(uuidRegex))
if (location == "" || location == "undefined"){
blockUuid2 = blockUuid
console.log(`THis is the fun${location}`)
console.log(location.match(uuidRegex))
}
else if (location.match(uuidRegex)!= null || (await logseq.Editor.getPage(location)!= undefined)){
blockUuid2 = location
console.log(`THis is the ${location}`)
}
else {
const parsedBlock = await Sherlock.parse(location);
const { isAllDay, eventTitle, startDate, endDate } = parsedBlock;
blockUuid2 = getDateForPageWithoutBrackets(startDate, (await logseq.App.getUserConfigs()).preferredDateFormat)
console.log(`THis is not the ${location}`)
}
console.log(blockUuid2)
insertProperlyTemplatedBlock(blockUuid2, template, sibling);
},
}),
logseq.provideStyle(`
.templater-btn {
border: 1px solid var(--ls-border-color);
white-space: initial;
padding: 2px 4px;
border-radius: 4px;
user-select: none;
cursor: default;
display: flex;
align-content: center;
}
.templater-btn:hover {
background-color: #defcf0;
border-color: #9ddbc7;
color: #0F9960;
}
`);
logseq.Editor.registerSlashCommand("Create Inline SmartBlock", async () => {
await logseq.Editor.insertAtEditingCursor(
`{{renderer :smartblockInline, }} `
);
// templaterBlock = await logseq.Editor.getCurrentBlock();
});
logseq.Editor.registerSlashCommand("Insert Smartblock", async (e) => {
updateTemplates();
handleClosePopup()
ReactDOM.render(
<React.StrictMode>
<SearchBar blockID={e.uuid} />
</React.StrictMode>,
document.getElementById("app")
);
logseq.showMainUI();
console.log("Insert Smartblock");
// templaterBlock = await logseq.Editor.getCurrentBlock();
});
logseq.Editor.registerSlashCommand(
"Create Inline SmartBlock(guided)",
async () => {
await logseq.Editor.insertAtEditingCursor(
`{{renderer :smartblockInline, template name, sibling?}} `
);
// templaterBlock = await logseq.Editor.getCurrentBlock();
}
);
logseq.App.registerCommandPalette({
key: 'Toggle Smartblock Inserter',
label: 'Select and insert Smartblock',
keybinding: {
binding: logseq.settings["Keyboard-Shortcut"],
mode: 'global',
}
}, async (e) => {
updateTemplates().then(()=>{
if (e.uuid != null){
updateTemplates();
handleClosePopup()
ReactDOM.render(
<React.StrictMode>
<SearchBar blockID={e.uuid} />
</React.StrictMode>,
document.getElementById("app")
)
logseq.showMainUI()
}
else {
logseq.UI.showMsg("Error: not in a block")
}
})
});
logseq.Editor.registerSlashCommand("Create SmartBlock Button", async (e) => {
updateTemplates();
ReactDOM.render(
<React.StrictMode>
<InsertionUI blockUUID = {e.uuid}/>
</React.StrictMode>,
document.getElementById("app")
)
logseq.showMainUI()
handleClosePopup()
// templaterBlock = await logseq.Editor.getCurrentBlock();
});
logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => {
updateTemplates();
var [type, template, title, sibling, location] = payload.arguments;
if (title == undefined) {
title = "New Template";
}
let realSiblings;
if (sibling == "true") {
realSiblings = true;
} else {
realSiblings = false;
}
if (type == ":smartblock") {
logseq.provideUI({
key: `${slot}`,
reset: true,
slot,
template: `
<button class="templater-btn" data-block-uuid="${payload.uuid}" data-sibling = ${sibling} data-template="${template}" data-title="${title}" data-location = "${location}"
data-on-click="insertTemplatedBlock">${title}</button>
`,
});
}
if (type == ":smartblockInline") {
if (!(await checkTemplate(payload.uuid))) {
logseq.Editor.updateBlock(payload.uuid, "");
await insertProperlyTemplatedBlock(payload.uuid, template, title).then(
() => {
logseq.Editor.updateBlock(payload.uuid, "");
}
);
} else {
logseq.provideUI({
key: `${slot}`,
reset: true,
slot,
template: `
<label class="templater-btn">Template: ${template}</label>
`,
});
}
}
});
}
logseq.ready(main).catch(console.error);