Skip to content

Commit

Permalink
Merge branch 'release/v1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
freder committed Apr 24, 2023
2 parents fe31ee3 + 3553ff5 commit 5536844
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Changelog

## 1.5.0
### Added
- "Headings-only" mode will only list headings (#18)

## 1.4.0
### Added
- Setting for whether the first item should be selected when opening the palette or not (#12)
- Setting configuring the max. depth of the displayed items (#14)
- Setting for configuring the max. depth of the displayed items (#14)


## 1.3.1
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-jump-to-block",
"version": "1.4.0",
"version": "1.5.0",
"main": "dist/index.html",
"logseq": {
"id": "logseq-plugin-jump-to-block"
Expand Down
39 changes: 32 additions & 7 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { BlockEntity } from '@logseq/libs/dist/LSPlugin.user';
import type { InitalSelectionOption } from '../types';
import type { InitalSelectionOption, ModeOption } from '../types';

import React, { useEffect, useState, Fragment } from 'react';
import CommandPalette, { Command } from 'react-command-palette';
import * as R from 'ramda';
import { Global, css } from '@emotion/react';

import { prepareLabel } from '../utils';
import {
defaultMaxDepth,
headingRegex,
initialSelectionDefault,
modeDefault
} from '../constants';

// @ts-ignore
import theme from '../../node_modules/react-command-palette/dist/themes/sublime-theme';
import '../../node_modules/react-command-palette/dist/themes/sublime.css';
import { defaultMaxDepth, initialSelectionOptionDefault } from '../constants';


type PathItem = {
Expand Down Expand Up @@ -59,6 +64,7 @@ const selectionHandler = async (

const makeCommands = (
blocks: BlockEntity[],
mode: ModeOption,
maxDepth = Infinity
) => {
const items: Command[] = [];
Expand All @@ -67,11 +73,28 @@ const makeCommands = (
depth: number,
path: Array<PathItem>
) => {
if (depth > maxDepth) {
return;
if (mode === 'Default') {
if (depth > maxDepth) {
return;
}
}

const blockContent = (block.content || '').trim();

if (mode === 'Headings-only') {
// ignore non-headings
if (!headingRegex.test(blockContent)) {
return;
}
const level = R.takeWhile(
(c) => c === '#',
blockContent.split('')
).length;
if (level > maxDepth) {
return;
}
}

// ignore empty blocks
if (blockContent === '') { return; }

Expand Down Expand Up @@ -155,6 +178,8 @@ const makeStyles = () => {


function App() {
const mode: ModeOption = logseq.settings?.mode || modeDefault;

const [open, setOpen] = useState(false);
const [items, setItems] = useState<Command[]>([]);

Expand Down Expand Up @@ -194,7 +219,7 @@ function App() {
0,
logseq.settings?.maxDepth || defaultMaxDepth
);
const items = makeCommands(blocks, maxDepth);
const items = makeCommands(blocks, mode, maxDepth);
setItems(items);
setOpen(true);
} else {
Expand All @@ -207,10 +232,10 @@ function App() {
logseq.off('ui:visible:changed', visibilityHandler);
};
},
[]
[mode]
);

const initialSelection: InitalSelectionOption = logseq.settings?.initialSelection || initialSelectionOptionDefault;
const initialSelection: InitalSelectionOption = logseq.settings?.initialSelection || initialSelectionDefault;
const highlightFirstSuggestion = initialSelection === 'First block';
// TODO: implement 'Current block'
const defaultInputValue = '';
Expand Down
7 changes: 5 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { InitalSelectionOption } from './types';
import type { InitalSelectionOption, ModeOption } from './types';

export const initialSelectionOptionDefault: InitalSelectionOption = 'Nothing';
export const initialSelectionDefault: InitalSelectionOption = 'Nothing';
export const defaultMaxDepth = 3;
export const modeDefault: ModeOption = 'Headings-only';

export const headingRegex = /^#{1,6} (.*)/gi;
25 changes: 20 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import type {
SettingSchemaDesc,
SimpleCommandKeybinding
} from '@logseq/libs/dist/LSPlugin';
import type { InitalSelectionOption } from './types';
import type { InitalSelectionOption, ModeOption } from './types';

import React from 'react';
import ReactDOM from 'react-dom/client';
import '@logseq/libs';

import { makeToolbarIcon } from './toolbar';
import { defaultMaxDepth, initialSelectionOptionDefault } from './constants';
import {
defaultMaxDepth,
initialSelectionDefault,
modeDefault
} from './constants';
import App from './components/App';



const cmdKey = 'jumpToBlock';
const cmdLabel = 'Jump to block…';
const settingsKey = cmdKey;
const settingsLabel = cmdLabel;
const initialSelectionOptions: InitalSelectionOption[] = [
/* 'Current block', */ 'First block', 'Nothing'
];
const modeOptions: ModeOption[] = [
'Default', 'Headings-only'
];
const settings: SettingSchemaDesc[] = [
{
key: settingsKey,
Expand All @@ -29,6 +35,15 @@ const settings: SettingSchemaDesc[] = [
default: 'mod+t',
type: 'string',
},
{
key: 'mode',
title: 'Mode',
description: '',
default: modeDefault,
type: 'enum',
enumChoices: modeOptions as string[],
enumPicker: 'radio',
},
{
key: 'autoOpen',
title: 'Auto-open palette',
Expand All @@ -40,15 +55,15 @@ const settings: SettingSchemaDesc[] = [
key: 'initialSelection',
title: 'What to select when opening the palette',
description: '',
default: initialSelectionOptionDefault,
default: initialSelectionDefault,
type: 'enum',
enumChoices: initialSelectionOptions as string[],
enumPicker: 'radio',
},
{
key: 'maxDepth',
title: 'Maximum block depth',
description: 'Limits the depth of blocks to be shown in the palette (0 = root-level)',
description: 'Limits the depth of blocks to be shown in the palette.',
default: defaultMaxDepth,
type: 'number',
},
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// TODO: 'Current block'
export type InitalSelectionOption = 'First block' | 'Nothing';
export type ModeOption = 'Default' | 'Headings-only';

0 comments on commit 5536844

Please sign in to comment.