Skip to content

Commit

Permalink
Fix issues with date/time triggers conflicting
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed May 30, 2024
1 parent 4cf4c1e commit 6f754da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/components/Editor/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { insertBlankLine } from '@codemirror/commands';
import { EditorSelection, Extension, Prec } from '@codemirror/state';
import { EditorView, ViewUpdate, keymap, placeholder as placeholderExt } from '@codemirror/view';
import classcat from 'classcat';
import { Editor, EditorPosition, Platform } from 'obsidian';
import { EditorPosition, Editor as ObsidianEditor, Platform } from 'obsidian';
import { MutableRefObject, useContext, useEffect, useRef } from 'preact/compat';
import { KanbanView } from 'src/KanbanView';
import { StateManager } from 'src/StateManager';
Expand All @@ -12,6 +12,7 @@ import { KanbanContext } from '../context';
import { c, noop } from '../helpers';
import { EditState, isEditing } from '../types';
import { datePlugins, stateManagerField } from './dateWidget';
import { matchDateTrigger, matchTimeTrigger } from './suggest';

interface MarkdownEditorProps {
editorRef?: MutableRefObject<EditorView>;
Expand Down Expand Up @@ -56,7 +57,10 @@ function getEditorAppProxy(view: KanbanView) {
});
}

function getMarkdownController(view: KanbanView, getEditor: () => Editor): Record<any, any> {
function getMarkdownController(
view: KanbanView,
getEditor: () => ObsidianEditor
): Record<any, any> {
return {
app: view.app,
showSearch: noop,
Expand Down Expand Up @@ -113,9 +117,11 @@ export function MarkdownEditor({

showTasksPluginAutoSuggest(
cursor: EditorPosition,
editor: Editor,
editor: ObsidianEditor,
lineHasGlobalFilter: boolean
) {
if (matchTimeTrigger(stateManager.getSetting('time-trigger'), editor, cursor)) return false;
if (matchDateTrigger(stateManager.getSetting('date-trigger'), editor, cursor)) return false;
if (lineHasGlobalFilter && cursor.line === 0) return true;
return undefined;
}
Expand Down
28 changes: 15 additions & 13 deletions src/components/Editor/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import { c, escapeRegExpStr } from '../helpers';
import { applyDate, constructDatePicker, toNextMonth, toPreviousMonth } from './datepicker';
import { Instance } from './flatpickr/types/instance';

export function matchTimeTrigger(timeTrigger: string, editor: Editor, cursor: EditorPosition) {
const textCtx = (editor.getLine(cursor.line) || '').slice(0, cursor.ch);
const timeTriggerRegex = new RegExp(`(?:^|\\s)${escapeRegExpStr(timeTrigger)}{?([^}]*)$`);
return textCtx.match(timeTriggerRegex);
}

export function matchDateTrigger(dateTrigger: string, editor: Editor, cursor: EditorPosition) {
const textCtx = (editor.getLine(cursor.line) || '').slice(0, cursor.ch);
const dateTriggerRegex = new RegExp(`(?:^|\\s)${escapeRegExpStr(dateTrigger)}{?([^}]*)$`);
return textCtx.match(dateTriggerRegex);
}

export class DateSuggest extends EditorSuggest<[]> {
plugin: KanbanPlugin;
app: App;
Expand Down Expand Up @@ -116,14 +128,9 @@ export class DateSuggest extends EditorSuggest<[]> {
const stateManager = this.plugin.getStateManager(file);
if (!stateManager) return null;

const line = editor.getLine(cursor.line);
if (!line) return null;

const dateTrigger = stateManager.getSetting('date-trigger');
const textCtx = line.slice(0, cursor.ch);

const isMatch = new RegExp(`(?:^|\\s)${escapeRegExpStr(dateTrigger)}$`).test(textCtx);
if (!isMatch) return null;
const match = matchDateTrigger(dateTrigger, editor, cursor);
if (!match) return null;

return {
start: { line: cursor.line, ch: cursor.ch - dateTrigger.length },
Expand Down Expand Up @@ -158,12 +165,7 @@ export class TimeSuggest extends EditorSuggest<string> {
if (!stateManager) return null;

const timeTrigger = stateManager.getSetting('time-trigger');
const timeTriggerRegex = new RegExp(
`(?:^|\\s)${escapeRegExpStr(timeTrigger as string)}{?([^}]*)$`
);
const textCtx = (editor.getLine(cursor.line) || '').slice(0, cursor.ch);
const match = textCtx.match(timeTriggerRegex);

const match = matchTimeTrigger(timeTrigger, editor, cursor);
if (!match) return null;

this.times = buildTimeArray(stateManager);
Expand Down
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ export default class KanbanPlugin extends Plugin {
MarkdownEditor: any;

async onload() {
this.MarkdownEditor = getEditorClass(this.app);

await this.loadSettings();

this.registerEditorSuggest(new DateSuggest(this.app, this));
this.MarkdownEditor = getEditorClass(this.app);

this.registerEditorSuggest(new TimeSuggest(this.app, this));
this.registerEditorSuggest(new DateSuggest(this.app, this));

this.registerEvent(
this.app.workspace.on('window-open', (_: any, win: Window) => {
Expand Down

0 comments on commit 6f754da

Please sign in to comment.