Skip to content

Commit ddfbb25

Browse files
committed
Add 'reuseSplitView' support to createOpenOrDeleteNoteCallbackUrl() and add openNoteInSplitViewIfNotOpenAlready()
1 parent a9dab4a commit ddfbb25

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

helpers/HTMLView.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,12 @@ export function replaceMarkdownLinkWithHTMLLink(str: string): string {
709709

710710
export async function sendToHTMLWindow(windowId: string, actionType: string, data: any = {}, updateInfo: string = ''): Promise<any> {
711711
try {
712+
// If HTMLView API isn't available (older platforms / builds), quietly skip sending
713+
if (typeof HTMLView === 'undefined' || typeof HTMLView.runJavaScript !== 'function') {
714+
logWarn('sendToHTMLWindow', `HTMLView API is not available on this platform/build; skipping message "${actionType}" to HTML window`)
715+
return
716+
}
717+
712718
const windowExists = isHTMLWindowOpen(windowId)
713719
if (!windowExists) logWarn(`sendToHTMLWindow`, `Window ${windowId} does not exist; setting NPWindowID = undefined`)
714720
const windowIdToSend = windowExists ? windowId : undefined // for iphone/ipad you have to send undefined

helpers/NPVersions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function usersVersionHas(feature: string): boolean {
3636
availableReminderLists: '3.20.0', // Dec 2025, macOS build 1469
3737
showInMainWindow: '3.20.0', // Dec 2025, macOS build 1469
3838
showInMainWindowOniOS: '3.20.1', // Jan 2026, iOS build 1380
39+
reuseSplitView: '3.20.1', // Jan 2026, macOS build 1479ish
3940
}
4041

4142
// Check if the user's version meets the requirement for the requested feature

helpers/NPWindows.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { clo, logDebug, logError, logInfo, logWarn } from '@helpers/dev'
88
import { usersVersionHas } from '@helpers/NPVersions'
99
import { caseInsensitiveMatch, caseInsensitiveStartsWith } from '@helpers/search'
1010
import { inputIntegerBounded } from '@helpers/userInput'
11+
import { createOpenOrDeleteNoteCallbackUrl } from '@helpers/general'
1112

1213
// ----------------------------------------------------------------------------
1314
// Types
@@ -647,6 +648,33 @@ export async function openNoteInNewSplitIfNeeded(filename: string): Promise<bool
647648
return !!res
648649
}
649650

651+
/**
652+
* Open a note in a split view using x-callback-url, but only if it is not already open in any Editor window.
653+
* Uses the 'reuseSplitView' openType so that a single split view is reused where possible.
654+
* Note: This is in place of `await Editor.openNoteByFilename(note.filename, true, 0, 0, false, false)` which doesn't have reuseSplitView option. (Yet.)
655+
* @author @jgclark
656+
* @param {string} filename - filename of the note to open
657+
* @returns {boolean} true if a new split view was opened, false if the note was already open
658+
*/
659+
export function openNoteInSplitViewIfNotOpenAlready(filename: string, callingFunctionName?: string): boolean {
660+
try {
661+
if (noteOpenInEditor(filename)) {
662+
logDebug('openNoteInSplitViewIfNotOpenAlready', `(for ${callingFunctionName ?? '?'}) Note '${filename}' is already open in an Editor window. Skipping.`)
663+
return false
664+
}
665+
666+
const splitOpenType = usersVersionHas('reuseSplitView') ? 'reuseSplitView' : 'splitView'
667+
const callbackUrl = createOpenOrDeleteNoteCallbackUrl(filename, 'filename', null, splitOpenType, false)
668+
logDebug('openNoteInSplitViewIfNotOpenAlready', `(for ${callingFunctionName ?? '?'}) splitOpenType: ${splitOpenType} openNote in Editor callbackUrl: ${callbackUrl}`)
669+
NotePlan.openURL(callbackUrl)
670+
logDebug('openNoteInSplitViewIfNotOpenAlready', `(for ${callingFunctionName ?? '?'}) after x-callback call to openNote`)
671+
return true
672+
} catch (error) {
673+
logError('openNoteInSplitViewIfNotOpenAlready', `openNoteInSplitViewIfNotOpenAlready: Error: ${error.message}`)
674+
return false
675+
}
676+
}
677+
650678
/**
651679
* Open a calendar note in a split editor, and (optionally) move insertion point to 'cursorPointIn'
652680
* @author @jgclark

helpers/general.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export function returnNoteLink(noteTitle: string, heading: string | null = ''):
277277
* @param {string} titleOrFilename - title of the note or the filename
278278
* @param {string} paramType - 'title' | 'filename' | 'date' (default is 'title')
279279
* @param {string | null} heading - heading inside of note (optional)
280-
* @param {string} openType - 'subWindow' | 'splitView' | 'useExistingSubWindow' (default: null)
280+
* @param {string} openType - 'subWindow' | 'splitView' | 'reuseSplitView' | 'useExistingSubWindow' (default: null)
281281
* @param {boolean} isDeleteNote - whether this is actually a deleteNote
282282
* @param {string} blockID - the blockID if this is a line link (includes the ^) -- only works with title (not filename)
283283
* @returns {string} the x-callback-url string
@@ -288,7 +288,7 @@ export function createOpenOrDeleteNoteCallbackUrl(
288288
titleOrFilename: string,
289289
paramType: 'title' | 'filename' | 'date' = 'title',
290290
heading: string | null = '',
291-
openType: 'subWindow' | 'splitView' | 'useExistingSubWindow' | null = null,
291+
openType: 'subWindow' | 'splitView' | 'reuseSplitView' | 'useExistingSubWindow' | null = null,
292292
isDeleteNote: boolean = false,
293293
blockID: string = '',
294294
): string {
@@ -300,7 +300,10 @@ export function createOpenOrDeleteNoteCallbackUrl(
300300
const head = heading && heading.length ? encodePlusParens(heading.replace('#', '')) : ''
301301
// console.log(`createOpenOrDeleteNoteCallbackUrl: ${xcb}${titleOrFilename}${head ? `&heading=${head}` : ''}`)
302302
const encodedTitleOrFilename = encodePlusParens(titleOrFilename)
303-
const openAs = openType && ['subWindow', 'splitView', 'useExistingSubWindow'].includes(openType) ? `&${openType}=yes` : ''
303+
let openAs = openType && ['subWindow', 'splitView', 'reuseSplitView', 'useExistingSubWindow'].includes(openType) ? `&${openType}=yes` : ''
304+
if (openType === 'reuseSplitView') {
305+
openAs += '&splitView=yes' // special case for reuseSplitView, which needs both
306+
}
304307
let retVal = ''
305308
if (isLineLink) {
306309
retVal = `${xcb}${encodedTitleOrFilename}${encodeURIComponent(blockID)}`

0 commit comments

Comments
 (0)