Skip to content

Commit 49c6005

Browse files
authored
feat: add remaining modifiers to NoteLookup (#1056)
* feat: Add horizontal split modifier to lookup v3 (#1045) * feat: add horizontal split modifier * chore: add tests for horizontal split modifier * fix: remove .only from test * feat: add effects modifier to lookup v3 (#1046) * feat: add copy note link button to lookup v3 * chore: add test for copy note link button * chore: add tests for journal and selection2link interactions * chore: add tests for scratch and selection2link interactions * chore: add tests for note modifiers and selectionExtract interactions * chore: add tests for multiselect and split interaction * chore: add tests for multiselect and copyNoteLink interaction * chore: fix typo * spike: fix test * chore: skip copyNoteLink basic test * chore: change button sequence to match v2 * fix: make note type modifier buttons idempotent * chore: enable skipped tests * chore: remove debug log * spike: copyNoteLink failing test * fix: addBehavior not applied to quickpick values * fix: remove unnecessary .show() calls * fix: properly check empty initial value when creating quickpick * fix: dispose quickpick before calling controller onHide
1 parent ca77763 commit 49c6005

File tree

11 files changed

+744
-59
lines changed

11 files changed

+744
-59
lines changed

packages/plugin-core/src/commands/CreateDailyJournal.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class CreateDailyJournalCommand extends BaseCommand<
2424
key = DENDRON_COMMANDS.CREATE_DAILY_JOURNAL_NOTE.key;
2525
async gatherInputs(): Promise<CommandInput | undefined> {
2626
const dailyJournalDomain = getWS().config.journal.dailyDomain;
27-
const fname = DendronClientUtilsV2.genNoteName("JOURNAL", {
27+
const { noteName: fname } = DendronClientUtilsV2.genNoteName("JOURNAL", {
2828
overrides: { domain: dailyJournalDomain },
2929
});
3030
return { title: fname };

packages/plugin-core/src/commands/LookupCommand.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ import { AnalyticsUtils } from "../utils/analytics";
88
import { BasicCommand } from "./base";
99

1010
export type LookupEffectType = "copyNoteLink" | "copyNoteRef" | "multiSelect";
11+
export enum LookupEffectTypeEnum {
12+
"copyNoteLink" = "copyNoteLink",
13+
"copyNoteRef" = "copyNoteRef",
14+
"multiSelect" = "multiSelect",
15+
}
1116
export type LookupFilterType = "directChildOnly";
1217
export type LookupSelectionType = "selection2link" | "selectionExtract";
1318
export type LookupNoteType = LookupNoteTypeEnum;
1419
export type LookupSplitType = "horizontal";
20+
export enum LookupSplitTypeEnum {
21+
"horizontal" = "horizontal",
22+
}
1523
export type LookupNoteExistBehavior = "open" | "overwrite";
1624
export enum LookupNoteTypeEnum {
1725
"journal" = "journal",

packages/plugin-core/src/commands/NoteLookupCommand.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
JournalBtn,
1414
ScratchBtn,
1515
MultiSelectBtn,
16+
CopyNoteLinkBtn,
1617
Selection2LinkBtn,
1718
SelectionExtractBtn,
19+
HorizontalSplitBtn,
1820
} from "../components/lookup/buttons";
1921
import { LookupControllerV3 } from "../components/lookup/LookupControllerV3";
2022
import {
@@ -39,15 +41,18 @@ import {
3941
LookupNoteTypeEnum,
4042
LookupSelectionType,
4143
LookupSelectionTypeEnum,
44+
LookupSplitType,
45+
LookupSplitTypeEnum,
4246
} from "./LookupCommand";
4347

44-
type CommandRunOpts = {
48+
export type CommandRunOpts = {
4549
initialValue?: string;
4650
noConfirm?: boolean;
4751
fuzzThreshold?: number;
4852
multiSelect?: boolean;
4953
noteType?: LookupNoteType;
5054
selectionType?: LookupSelectionType;
55+
splitType?: LookupSplitType;
5156
/**
5257
* NOTE: currently, only one filter is supported
5358
*/
@@ -137,14 +142,16 @@ export class NoteLookupCommand extends BaseCommand<
137142
),
138143
extraButtons: [
139144
//todo: mirror v2 button sequence
140-
JournalBtn.create(copts.noteType === LookupNoteTypeEnum.journal),
141-
ScratchBtn.create(copts.noteType === LookupNoteTypeEnum.scratch),
142145
MultiSelectBtn.create(copts.multiSelect),
146+
CopyNoteLinkBtn.create(),
143147
DirectChildFilterBtn.create(
144148
copts.filterMiddleware?.includes("directChildOnly")
145149
),
146-
Selection2LinkBtn.create(copts.selectionType === LookupSelectionTypeEnum.selection2link),
147150
SelectionExtractBtn.create(copts.selectionType === LookupSelectionTypeEnum.selectionExtract),
151+
Selection2LinkBtn.create(copts.selectionType === LookupSelectionTypeEnum.selection2link),
152+
JournalBtn.create(copts.noteType === LookupNoteTypeEnum.journal),
153+
ScratchBtn.create(copts.noteType === LookupNoteTypeEnum.scratch),
154+
HorizontalSplitBtn.create(copts.splitType === LookupSplitTypeEnum.horizontal),
148155
],
149156
});
150157
this._provider = new NoteLookupProvider("lookup", {

packages/plugin-core/src/components/lookup/LookupControllerV2.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,18 @@ export class LookupControllerV2 {
267267

268268
switch (noteResp?.type) {
269269
case "journal": {
270-
onUpdateValue = DendronClientUtilsV2.genNoteName("JOURNAL", {
270+
const { noteName } = DendronClientUtilsV2.genNoteName("JOURNAL", {
271271
overrides: { domain: quickPickValue },
272272
});
273+
onUpdateValue = noteName;
273274
onUpdateReason = "updatePickerBehavior:journal";
274275
break;
275276
}
276277
case "scratch": {
277-
onUpdateValue = DendronClientUtilsV2.genNoteName("SCRATCH", {
278+
const { noteName } = DendronClientUtilsV2.genNoteName("SCRATCH", {
278279
overrides: { domain: quickPickValue },
279280
});
281+
onUpdateValue = noteName;
280282
onUpdateReason = "updatePickerBehavior:scratch";
281283
break;
282284
}

packages/plugin-core/src/components/lookup/LookupControllerV3.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ export class LookupControllerV3 {
117117
PickerUtilsV2.refreshButtons({ quickpick, buttons, buttonsPrev });
118118
await PickerUtilsV2.refreshPickerBehavior({ quickpick, buttons });
119119
quickpick.onDidTriggerButton(this.onTriggerButton);
120-
quickpick.onDidHide(this.onHide);
120+
quickpick.onDidHide(() => {
121+
quickpick.dispose();
122+
this.onHide();
123+
});
121124
provider.provide(this);
122125
return { quickpick };
123126
}
@@ -160,14 +163,10 @@ export class LookupControllerV3 {
160163
}
161164

162165
onHide() {
163-
const { _quickpick: quickpick } = this;
164-
if (!quickpick) {
165-
return;
166-
}
167-
quickpick.dispose();
168166
this._quickpick = undefined;
169167
this._cancelTokenSource?.dispose();
170168
}
169+
171170

172171
onTriggerButton = async (btn: QuickInputButton) => {
173172
const { _quickpick: quickpick } = this;

packages/plugin-core/src/components/lookup/buttons.ts

+50-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from "../../commands/LookupCommand";
2020
import { clipboard, DendronClientUtilsV2, VSCodeUtils } from "../../utils";
2121
import { DendronQuickPickerV2 } from "./types";
22-
import { PickerUtilsV2 } from "./utils";
22+
import { NotePickerUtils, PickerUtilsV2 } from "./utils";
2323

2424
export type ButtonType =
2525
| LookupEffectType
@@ -83,7 +83,6 @@ function isSplitButton(button: DendronBtn) {
8383
return _.includes(["horizontal", "vertical"], button.type);
8484
}
8585

86-
// TODO: make it into a utils method?
8786
const selectionToNoteProps = async (opts: {
8887
selectionType: string;
8988
note: NoteProps;
@@ -93,7 +92,7 @@ const selectionToNoteProps = async (opts: {
9392
const { document, range } = resp || {};
9493
const { selectionType, note } = opts;
9594
const { selection, text } = VSCodeUtils.getSelection();
96-
//TODO: split these up?
95+
9796
switch(selectionType) {
9897
case "selectionExtract": {
9998
if (!_.isUndefined(document)) {
@@ -209,16 +208,25 @@ export class Selection2LinkBtn extends DendronBtn {
209208
});
210209
};
211210

212-
quickPick.rawValue = quickPick.value;
211+
quickPick.prevValue = quickPick.value;
213212
const { text } = VSCodeUtils.getSelection();
214213
const slugger = getSlugger();
215-
quickPick.value = [quickPick.value, slugger.slug(text!)].join(".");
214+
quickPick.selectionModifierValue = slugger.slug(text!);
215+
if (quickPick.noteModifierValue || quickPick.prefix) {
216+
quickPick.value = NotePickerUtils.getPickerValue(quickPick);
217+
} else {
218+
quickPick.value = [
219+
quickPick.rawValue,
220+
NotePickerUtils.getPickerValue(quickPick)
221+
].join(".");
222+
}
216223
return;
217224
}
218225

219226
async onDisable({ quickPick }: ButtonHandleOpts) {
220227
quickPick.selectionProcessFunc = undefined;
221-
quickPick.value = quickPick.rawValue;
228+
quickPick.selectionModifierValue = undefined;
229+
quickPick.value = NotePickerUtils.getPickerValue(quickPick);
222230
return;
223231
}
224232
}
@@ -262,18 +270,21 @@ export class JournalBtn extends DendronBtn {
262270
}
263271

264272
async onEnable({ quickPick }: ButtonHandleOpts) {
265-
quickPick.modifyPickerValueFunc = (value: string) => {
266-
return DendronClientUtilsV2.genNoteName("JOURNAL", {
267-
overrides: { domain: value },
268-
});
273+
quickPick.modifyPickerValueFunc = () => {
274+
return DendronClientUtilsV2.genNoteName("JOURNAL");
269275
};
270-
quickPick.rawValue = quickPick.value;
271-
quickPick.value = quickPick.modifyPickerValueFunc(quickPick.rawValue);
276+
const { noteName, prefix } = quickPick.modifyPickerValueFunc();
277+
quickPick.noteModifierValue = _.difference(noteName.split("."), prefix.split(".")).join(".");
278+
quickPick.prevValue = quickPick.value;
279+
quickPick.prefix = prefix;
280+
quickPick.value = NotePickerUtils.getPickerValue(quickPick);
272281
return;
273282
}
274283

275284
async onDisable({ quickPick }: ButtonHandleOpts) {
276285
quickPick.modifyPickerValueFunc = undefined;
286+
quickPick.noteModifierValue = undefined;
287+
quickPick.prevValue = quickPick.value;
277288
quickPick.value = quickPick.rawValue;
278289
}
279290
}
@@ -290,31 +301,47 @@ export class ScratchBtn extends DendronBtn {
290301
}
291302

292303
async onEnable({ quickPick }: ButtonHandleOpts) {
293-
quickPick.modifyPickerValueFunc = (value: string) => {
294-
return DendronClientUtilsV2.genNoteName("SCRATCH", {
295-
overrides: { domain: value },
296-
});
304+
quickPick.modifyPickerValueFunc = () => {
305+
return DendronClientUtilsV2.genNoteName("SCRATCH");
297306
};
298-
quickPick.rawValue = quickPick.value;
299-
quickPick.value = quickPick.modifyPickerValueFunc(quickPick.rawValue);
307+
quickPick.prevValue = quickPick.value;
308+
const { noteName, prefix } = quickPick.modifyPickerValueFunc();
309+
quickPick.noteModifierValue = _.difference(noteName.split("."), prefix.split(".")).join(".");
310+
quickPick.prefix = prefix;
311+
quickPick.value = NotePickerUtils.getPickerValue(quickPick);
300312
return;
301313
}
302314

303315
async onDisable({ quickPick }: ButtonHandleOpts) {
304316
quickPick.modifyPickerValueFunc = undefined;
317+
quickPick.noteModifierValue = undefined;
318+
quickPick.prevValue = quickPick.value;
305319
quickPick.value = quickPick.rawValue;
306320
}
307321
}
308-
class HorizontalSplitBtn extends DendronBtn {
322+
export class HorizontalSplitBtn extends DendronBtn {
309323
static create(pressed?: boolean) {
310-
return new DendronBtn({
324+
return new HorizontalSplitBtn({
311325
title: "Split Horizontal",
312326
iconOff: "split-horizontal",
313327
iconOn: "menu-selection",
314328
type: "horizontal",
315329
pressed,
316330
});
317331
}
332+
333+
async onEnable({ quickPick }: ButtonHandleOpts) {
334+
quickPick.showNote = async (uri) => vscode.window.showTextDocument(
335+
uri,
336+
{ viewColumn: vscode.ViewColumn.Beside }
337+
);
338+
return;
339+
}
340+
341+
async onDisable({ quickPick }: ButtonHandleOpts) {
342+
quickPick.showNote = async (uri) => vscode.window.showTextDocument(uri);
343+
return;
344+
}
318345
}
319346

320347
export class DirectChildFilterBtn extends DendronBtn {
@@ -366,9 +393,9 @@ export class MultiSelectBtn extends DendronBtn {
366393
}
367394
}
368395

369-
export class CopyNoteLinkButton extends DendronBtn {
396+
export class CopyNoteLinkBtn extends DendronBtn {
370397
static create(pressed?: boolean) {
371-
return new CopyNoteLinkButton({
398+
return new CopyNoteLinkBtn({
372399
title: "Copy Note Link",
373400
iconOff: "clippy",
374401
iconOn: "menu-selection",
@@ -429,7 +456,7 @@ export function createAllButtons(
429456
): DendronBtn[] {
430457
const buttons = [
431458
MultiSelectBtn.create(),
432-
CopyNoteLinkButton.create(),
459+
CopyNoteLinkBtn.create(),
433460
DirectChildFilterBtn.create(),
434461
SelectionExtractBtn.create(),
435462
Selection2LinkBtn.create(),

packages/plugin-core/src/components/lookup/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export type LookupControllerState = {
1414
};
1515

1616
type FilterQuickPickFunction = (items: NoteQuickInput[]) => NoteQuickInput[];
17-
type ModifyPickerValueFunc = (value: string) => string;
17+
type ModifyPickerValueFunc = (value?: string) => {
18+
noteName: string,
19+
prefix: string,
20+
};
1821
type SelectionProcessFunc = (note: NoteProps) => Promise<NoteProps | undefined>;
1922

2023
export type DendronQuickPickItemV2 = QuickPick<DNodePropsQuickInputV2>;
@@ -40,6 +43,9 @@ export type DendronQuickPickerV2 = DendronQuickPickItemV2 & {
4043
* Value before being modified
4144
*/
4245
rawValue: string;
46+
prefix: string;
47+
noteModifierValue?: string;
48+
selectionModifierValue?: string;
4349
onCreate?: (note: DNodeProps) => Promise<DNodeProps | undefined>;
4450
/**
4551
@deprecated, replace with filterResults

packages/plugin-core/src/components/lookup/utils.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ export class PickerUtilsV2 {
249249
quickPick.matchOnDetail = false;
250250
quickPick.sortByLabel = false;
251251
quickPick.showNote = async (uri) => window.showTextDocument(uri);
252-
if (initialValue) {
252+
if (initialValue !== undefined) {
253253
quickPick.rawValue = initialValue;
254+
quickPick.prefix = initialValue;
254255
quickPick.value = initialValue;
255-
}
256+
}
256257
return quickPick;
257258
}
258259

@@ -615,12 +616,12 @@ export class PickerUtilsV2 {
615616
// they don't modify state of the quickpick after onEnable.
616617
await Promise.all(
617618
buttonsDisabled.map((bt) => {
618-
bt.onDisable({ quickPick: opts.quickpick });
619+
return bt.onDisable({ quickPick: opts.quickpick });
619620
})
620621
);
621622
await Promise.all(
622623
buttonsEnabled.map((bt) => {
623-
bt.onEnable({ quickPick: opts.quickpick });
624+
return bt.onEnable({ quickPick: opts.quickpick });
624625
})
625626
);
626627
}
@@ -715,4 +716,12 @@ export class NotePickerUtils {
715716
Logger.info({ ctx, msg: "engine.query", profile });
716717
return updatedItems;
717718
}
719+
720+
static getPickerValue(picker: DendronQuickPickerV2) {
721+
return [
722+
picker.prefix,
723+
picker.noteModifierValue,
724+
picker.selectionModifierValue
725+
].filter((ent) => !_.isEmpty(ent)).join(".");
726+
}
718727
}

packages/plugin-core/src/test/suite-integ/LookupCommand.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ suite("selectionExtract", function () {
11061106
// (_.find(lc.state.buttons, {
11071107
// type: "multiSelect",
11081108
// }) as MultiSelectBtn).pressed = true;
1109-
// await lc.onTriggerButton(CopyNoteLinkButton.create(true));
1109+
// await lc.onTriggerButton(CopyNoteLinkBtn.create(true));
11101110
// assert.strictEqual(
11111111
// clipboardy.readSync(),
11121112
// "[[Foo|foo]]\n[[Ch1|foo.ch1]]"

0 commit comments

Comments
 (0)