-
-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
little cleanup: useEventListener hook (#2749)
- Loading branch information
Showing
13 changed files
with
161 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
//@ts-ignore | ||
import dialogPolyfill from "https://cdn.jsdelivr.net/npm/[email protected]/dist/dialog-polyfill.esm.min.js" | ||
|
||
import { useEffect, useLayoutEffect, useRef } from "../imports/Preact.js" | ||
import { useLayoutEffect, useMemo, useRef } from "../imports/Preact.js" | ||
|
||
/** | ||
* @returns {[import("../imports/Preact.js").Ref<HTMLDialogElement?>, () => void, () => void, () => void]} | ||
|
@@ -13,9 +13,11 @@ export const useDialog = () => { | |
if (dialog_ref.current != null) dialogPolyfill.registerDialog(dialog_ref.current) | ||
}, [dialog_ref.current]) | ||
|
||
const open = () => dialog_ref.current?.showModal() | ||
const close = () => dialog_ref.current?.close() | ||
const toggle = () => (dialog_ref.current?.open === true ? dialog_ref.current?.close() : dialog_ref.current?.showModal()) | ||
return useMemo(() => { | ||
const open = () => dialog_ref.current?.showModal() | ||
const close = () => dialog_ref.current?.close() | ||
const toggle = () => (dialog_ref.current?.open === true ? dialog_ref.current?.close() : dialog_ref.current?.showModal()) | ||
|
||
return [dialog_ref, open, close, toggle] | ||
return [dialog_ref, open, close, toggle] | ||
}, [dialog_ref]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useCallback, useEffect } from "../imports/Preact.js" | ||
|
||
export const useEventListener = (element, event_name, handler, deps) => { | ||
let handler_cached = useCallback(handler, deps) | ||
useEffect(() => { | ||
if (element == null) return | ||
element.addEventListener(event_name, handler_cached) | ||
return () => element.removeEventListener(event_name, handler_cached) | ||
}, [element, event_name, handler_cached]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { html, Component, useRef, useLayoutEffect, useState, useEffect } from "../imports/Preact.js" | ||
import { html, useRef, useLayoutEffect, useState, useEffect, useCallback } from "../imports/Preact.js" | ||
import { has_ctrl_or_cmd_pressed } from "../common/KeyboardShortcuts.js" | ||
import _ from "../imports/lodash.js" | ||
|
||
import "https://cdn.jsdelivr.net/gh/fonsp/[email protected]/lib/rebel-tag-input.mjs" | ||
|
||
//@ts-ignore | ||
import dialogPolyfill from "https://cdn.jsdelivr.net/npm/[email protected]/dist/dialog-polyfill.esm.min.js" | ||
import immer from "../imports/immer.js" | ||
import { useDialog } from "../common/useDialog.js" | ||
import { FeaturedCard } from "./welcome/FeaturedCard.js" | ||
import { useEventListener } from "../common/useEventListener.js" | ||
|
||
/** | ||
* @param {{ | ||
|
@@ -24,10 +24,6 @@ export const FrontMatterInput = ({ filename, remote_frontmatter, set_remote_fron | |
set_frontmatter(remote_frontmatter ?? {}) | ||
}, [remote_frontmatter]) | ||
|
||
// useEffect(() => { | ||
// console.log("New frontmatter:", frontmatter) | ||
// }, [frontmatter]) | ||
|
||
const fm_setter = (key) => (value) => | ||
set_frontmatter( | ||
immer((fm) => { | ||
|
@@ -41,43 +37,23 @@ export const FrontMatterInput = ({ filename, remote_frontmatter, set_remote_fron | |
set_frontmatter(remote_frontmatter ?? {}) | ||
close() | ||
} | ||
const submit = () => { | ||
const submit = useCallback(() => { | ||
set_remote_frontmatter(clean_data(frontmatter) ?? {}).then(() => | ||
alert("Frontmatter synchronized ✔\n\nThese parameters will be used in future exports.") | ||
) | ||
close() | ||
} | ||
|
||
const clean_data = (obj) => { | ||
let a = _.isPlainObject(obj) | ||
? Object.fromEntries( | ||
Object.entries(obj) | ||
.map(([key, val]) => [key, clean_data(val)]) | ||
.filter(([key, val]) => val != null) | ||
) | ||
: _.isArray(obj) | ||
? obj.map(clean_data).filter((x) => x != null) | ||
: obj | ||
|
||
return _.isEmpty(a) ? null : a | ||
} | ||
}, [clean_data, set_remote_frontmatter, frontmatter, close]) | ||
|
||
useLayoutEffect(() => { | ||
window.addEventListener("open pluto frontmatter", open) | ||
return () => { | ||
window.removeEventListener("open pluto frontmatter", open) | ||
} | ||
}, []) | ||
useEventListener(window, "open pluto frontmatter", open) | ||
|
||
useLayoutEffect(() => { | ||
const listener = (e) => { | ||
useEventListener( | ||
window, | ||
"keydown", | ||
(e) => { | ||
if (dialog_ref.current != null) if (dialog_ref.current.contains(e.target)) if (e.key === "Enter" && has_ctrl_or_cmd_pressed(e)) submit() | ||
} | ||
window.addEventListener("keydown", listener) | ||
return () => { | ||
window.removeEventListener("keydown", listener) | ||
} | ||
}, []) | ||
}, | ||
[dialog_ref, submit] | ||
) | ||
|
||
const frontmatter_with_defaults = { | ||
title: null, | ||
|
@@ -188,6 +164,20 @@ export const FrontMatterInput = ({ filename, remote_frontmatter, set_remote_fron | |
</dialog>` | ||
} | ||
|
||
const clean_data = (obj) => { | ||
let a = _.isPlainObject(obj) | ||
? Object.fromEntries( | ||
Object.entries(obj) | ||
.map(([key, val]) => [key, clean_data(val)]) | ||
.filter(([key, val]) => val != null) | ||
) | ||
: _.isArray(obj) | ||
? obj.map(clean_data).filter((x) => x != null) | ||
: obj | ||
|
||
return _.isEmpty(a) ? null : a | ||
} | ||
|
||
const special_field_names = ["tags", "date", "license", "url", "color"] | ||
|
||
const field_type = (name) => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.