Skip to content

Commit

Permalink
Widget: alt-click to move cursor into widget
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Oct 16, 2024
1 parent 413855c commit 5765540
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
9 changes: 5 additions & 4 deletions web/cm_plugins/lua_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class LuaWidget extends WidgetType {
if (cacheItem) {
div.innerHTML = cacheItem.html;
if (cacheItem.html) {
attachWidgetEventHandlers(div, this.client);
attachWidgetEventHandlers(div, this.client, this.from);
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ export class LuaWidget extends WidgetType {
} else {
div.style.display = "inline";
}
attachWidgetEventHandlers(div, this.client);
attachWidgetEventHandlers(div, this.client, this.from);
this.client.setWidgetCache(
this.cacheKey,
{ height: div.clientHeight, html },
Expand Down Expand Up @@ -157,7 +157,7 @@ export class LuaWidget extends WidgetType {
}
div.innerHTML = html;
if (html) {
attachWidgetEventHandlers(div, this.client);
attachWidgetEventHandlers(div, this.client, this.from);
}
}

Expand Down Expand Up @@ -187,7 +187,8 @@ export class LuaWidget extends WidgetType {
override eq(other: WidgetType): boolean {
return (
other instanceof LuaWidget &&
other.bodyText === this.bodyText && other.cacheKey === this.cacheKey
other.bodyText === this.bodyText && other.cacheKey === this.cacheKey &&
this.from === other.from
);
}
}
5 changes: 3 additions & 2 deletions web/cm_plugins/markdown_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class MarkdownWidget extends WidgetType {
}

private attachListeners(div: HTMLElement, buttons?: CodeWidgetButton[]) {
attachWidgetEventHandlers(div, this.client);
attachWidgetEventHandlers(div, this.client, this.from);

if (!buttons) {
buttons = [];
Expand Down Expand Up @@ -225,7 +225,8 @@ export class MarkdownWidget extends WidgetType {
override eq(other: WidgetType): boolean {
return (
other instanceof MarkdownWidget &&
other.bodyText === this.bodyText && other.cacheKey === this.cacheKey
other.bodyText === this.bodyText && other.cacheKey === this.cacheKey &&
this.from === other.from
);
}
}
Expand Down
12 changes: 11 additions & 1 deletion web/cm_plugins/widget_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { parsePageRef } from "@silverbulletmd/silverbullet/lib/page_ref";
import type { Client } from "../client.ts";
import { tagPrefix } from "../../plugs/index/constants.ts";

export function attachWidgetEventHandlers(div: HTMLElement, client: Client) {
export function attachWidgetEventHandlers(
div: HTMLElement,
client: Client,
pos?: number,
) {
div.addEventListener("mousedown", (e) => {
if (e.altKey) {
// Move cursor there
client.editorView.dispatch({ selection: { anchor: pos! } });
client.editorView.focus();
e.preventDefault();
}
// CodeMirror overrides mousedown on parent elements to implement its own selection highlighting.
// That's nice, but not for markdown widgets, so let's not propagate the event to CodeMirror here.
e.stopPropagation();
Expand Down
4 changes: 2 additions & 2 deletions website/Space Lua.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> **warning** Warning
> **warning** Experimental
> This is a **highly experimental** feature still under active development. It is documented here primarily for the real early adopters as this feature develops.
Space Lua is a custom implementation of the [Lua programming language](https://lua.org/) embedded in SilverBullet.
Expand All @@ -25,7 +25,7 @@ Each `space-lua` block has its own local scope, however when functions and varia

A new syntax introduced with Space Lua is the `${lua expression}` syntax that you can use in your pages, this syntax will [[Live Preview]] to the evaluation of that expression.

Example: 10 + 2 = ${adder(10, 2)} (move into this value to see the expression using the just defined `adder` function to calculate this).
Example: 10 + 2 = ${adder(10, 2)} (Alt-click on this value to see the expression using the just defined `adder` function to calculate this).

## Widgets
The `${lua expression}` syntax can be used to implement simple widgets. If the lua expression evaluates to a simple string, it will live preview as that string rendered as simple markdown. However, if the expression returns a Lua table with specific keys, you can do some cooler stuff. The following keys are supported:
Expand Down

0 comments on commit 5765540

Please sign in to comment.