Skip to content

Commit

Permalink
Skip banner functionality for Canvas views (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
noatpad committed Sep 20, 2023
1 parent 4eac881 commit 818c93b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/editing/extensions/bannerExtender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EditorState, StateEffect, Transaction } from '@codemirror/state';
import isEqual from 'lodash/isEqual';
import { editorInfoField } from 'obsidian';
import { extractBannerDataFromState, shouldDisplayBanner } from 'src/bannerData';
import bannerField from './bannerField';
import {
Expand All @@ -12,13 +13,15 @@ import {
upsertBannerEffect
} from './utils';

// Get an upsert/remove effect depending if banner data should be displayed
const getEffectFromData = (state: EditorState): StateEffect<BannerMetadata> => {
const bannerData = extractBannerDataFromState(state);
return shouldDisplayBanner(bannerData)
? upsertBannerEffect.of(bannerData)
: removeBannerEffect.of(null);
};

// Helper to check if banner data change between transactions
const didBannerDataChange = (transaction: Transaction): boolean => {
const { docChanged, state } = transaction;
if (!docChanged) return false;
Expand All @@ -33,6 +36,9 @@ effects to `bannerField` */
const bannerExtender = EditorState.transactionExtender.of((transaction) => {
const { effects, state } = transaction;

// Only run on Markdown panes (skips Canvas views)
if (state.field(editorInfoField).leaf === undefined) return null;

if (hasEffect(effects, openNoteEffect)) {
console.log('open note!');
const newEffects = [];
Expand Down
2 changes: 1 addition & 1 deletion src/editing/extensions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getBanner = (state: EditorState) => {

export const destroyBanner = (state: EditorState) => {
const { id } = state.field(editorInfoField).leaf;
const banner = getBanner(state);
const banner = leafBannerMap[id];
banner.$destroy();
delete leafBannerMap[id];
};

0 comments on commit 818c93b

Please sign in to comment.