Skip to content

Commit d201bb7

Browse files
committed
Add triggerLoop to the template code
1 parent 77f06d5 commit d201bb7

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/templates/np.plugin.starter/src/NPTriggers-Hooks.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import pluginJson from '../plugin.json' // gives you access to the contents of p
55
import { log, logError, logDebug, timer, clo, JSP } from '@helpers/dev'
66
import { updateSettingData, pluginUpdated } from '@helpers/NPConfiguration'
77
import { showMessage } from '@helpers/userInput'
8+
import { isTriggerLoop } from '@helpers/NPFrontMatter'
89

910
/**
1011
* NOTEPLAN PER-NOTE TRIGGERS
@@ -27,16 +28,8 @@ export async function onOpen(note: TNote): Promise<void> {
2728
// Try to guard against infinite loops of opens/refreshing
2829
// You can delete this code if you are sure that your onOpen trigger will not cause an infinite loop
2930
// But the safest thing to do is put your code inside the if loop below to ensure it runs no more than once every 15s
30-
const now = new Date()
31-
if (Editor?.note?.changedDate) {
32-
const lastEdit = new Date(Editor?.note?.changedDate)
33-
if (now.getTime() - lastEdit.getTime() > 15000) {
34-
logDebug(pluginJson, `onOpen ${timer(lastEdit)} since last edit`)
35-
// Put your code here or call a function that does the work
36-
} else {
37-
logDebug(pluginJson, `onOpen: Only ${timer(lastEdit)} since last edit (hasn't been 15s)`)
38-
}
39-
}
31+
if (Editor?.note && isTriggerLoop(Editor.note)) return
32+
// Put your code here or call a function that does the work
4033
} catch (error) {
4134
logError(pluginJson, `onOpen: ${JSP(error)}`)
4235
}
@@ -50,6 +43,8 @@ export async function onEditorWillSave() {
5043
try {
5144
logDebug(pluginJson, `${pluginJson['plugin.id']} :: onEditorWillSave running with note in Editor:"${String(Editor.filename)}"`)
5245
// Put your code here or call a function that does the work
46+
if (Editor?.note && isTriggerLoop(Editor.note)) return
47+
// Put your code here or call a function that does the work
5348
// Note: as stated in the documentation, if you want to change any content in the Editor
5449
// before the file is written, you should NOT use the *note* variable here to change content
5550
// Instead, use Editor.* commands (e.g. Editor.insertTextAtCursor()) or Editor.updateParagraphs()

0 commit comments

Comments
 (0)