Skip to content

Commit 50834e6

Browse files
committed
chore(toc): re-expose public typedef
1 parent 2414af5 commit 50834e6

13 files changed

Lines changed: 78 additions & 71 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@svelte-put/toc': patch
3+
---
4+
5+
re-expose public typedef (following https://github.com/Rich-Harris/dts-buddy/pull/82)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { TocRootEventAttributes } from '../attributes';
2+
import { TocItem } from '../types.public';
3+
4+
export type TocRootAction = import('svelte/action').Action<
5+
HTMLElement,
6+
undefined,
7+
TocRootEventAttributes
8+
>;
9+
export type TocLinkAction = import('svelte/action').Action<
10+
HTMLAnchorElement,
11+
TocItem | string | undefined
12+
>;
13+

packages/toc/src/actions/internals.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ATTRIBUTES } from '../attributes/index.js';
1+
import { ATTRIBUTES } from '../constants.js';
22

33
/**
44
* @package
55
* @param {HTMLElement} element
6-
* @param {import('../types').TocAnchorConfig} anchor
6+
* @param {import('../types.public').TocAnchorConfig} anchor
77
* @param {string} tocId
88
* @returns {HTMLAnchorElement | undefined}
99
*/
@@ -104,19 +104,19 @@ export function processAnchor(element, anchor, tocId) {
104104
/**
105105
* @package
106106
* @param {HTMLElement} element
107-
* @param {import('../types').TocObserveConfig} observe
107+
* @param {import('../types.public').TocObserveConfig} observe
108108
* @param {string} tocId
109109
* @param {(activeTocItemId?: string) => void} updateActiveTocItem
110110
* @param {Record<number, IntersectionObserver>} observerPool
111-
* @returns {import('../types').TocItem['observe']}
111+
* @returns {import('../types.public').TocItem['observe']}
112112
*/
113113
export function processObserve(element, observe, tocId, updateActiveTocItem, observerPool) {
114114
if (!observe.enabled) return undefined;
115115
const parentElement = element.parentElement;
116-
/** @type {Exclude<import('../types').TocObserveConfig['strategy'], 'auto'>} */
116+
/** @type {Exclude<import('../types.public').TocObserveConfig['strategy'], 'auto'>} */
117117
let rStrategy;
118118
const userDefinedStrategy =
119-
/** @type {import('../types').TocObserveConfig['strategy']} */ (
119+
/** @type {import('../types.public').TocObserveConfig['strategy']} */ (
120120
element.getAttribute(ATTRIBUTES.strategy)
121121
) || observe.strategy;
122122
if (typeof userDefinedStrategy !== 'number' && userDefinedStrategy !== 'auto') {
@@ -249,3 +249,4 @@ function slugify(text) {
249249
.replace(/^-+/, '')
250250
.replace(/-+/g, '-');
251251
}
252+

packages/toc/src/actions/link.svelte.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { on } from 'svelte/events';
22

3-
import { ATTRIBUTES } from '../attributes/index.js';
3+
import { ATTRIBUTES } from '../constants.js';
44

55
/**
66
* @param {import('../toc.svelte.js').Toc} toc
7-
* @returns {import('./types.js').TocLinkAction}
7+
* @returns {import('./actions').TocLinkAction}
88
*/
99
export function createTocLinkAction(toc) {
1010
// eslint-disable-next-line no-undef

packages/toc/src/actions/root.svelte.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { tick } from 'svelte';
22

3-
import { ATTRIBUTES } from '../attributes/index.js';
4-
import { DEFAULT_TOC_CONFIG } from '../constants.js';
3+
import { DEFAULT_TOC_CONFIG, ATTRIBUTES } from '../constants.js';
54

65
import {
76
extractElementText,
@@ -12,7 +11,7 @@ import {
1211

1312
/**
1413
* @param {import('../toc.svelte.js').Toc} toc
15-
* @returns {import('./types.js').TocRootAction}
14+
* @returns {import('./actions').TocRootAction}
1615
*/
1716
export function createTocRootAction(toc) {
1817
// eslint-disable-next-line no-undef
@@ -91,7 +90,7 @@ export function createTocRootAction(toc) {
9190
const { anchor, observe, scrollMarginTop } = toc.config;
9291
/** @type {HTMLElement[]} */
9392
const elements = Array.from(node.querySelectorAll(selector));
94-
/** @type {Promise<import('../types').TocItem['observe']>[]} */
93+
/** @type {Promise<import('../types.public').TocItem['observe']>[]} */
9594
const observePromises = [];
9695

9796
node.toggleAttribute(ATTRIBUTES.observeActiveId, true);
@@ -121,7 +120,7 @@ export function createTocRootAction(toc) {
121120
new Promise((resolve) => {
122121
const rObserve = processObserve(element, observe, tocId, updateActiveTocItem, intersectionObservers);
123122
if (toc.items.has(tocId)) {
124-
const tocItem = /** @type {import('../types').TocItem} */ (toc.items.get(tocId));
123+
const tocItem = /** @type {import('../types.public').TocItem} */ (toc.items.get(tocId));
125124
toc.items.set(tocId, { ...tocItem, observe: rObserve });
126125
}
127126

@@ -157,3 +156,4 @@ export function createTocRootAction(toc) {
157156
};
158157
};
159158
}
159+

packages/toc/src/actions/types.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/toc/src/attributes/attributes.d.ts renamed to packages/toc/src/attributes.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Toc } from '../toc.svelte.js';
2-
import type { TocObserveConfig } from '../types';
1+
import type { Toc } from './toc.svelte.js';
2+
import type { TocObserveConfig } from './types.public';
33

44
/**
55
* Data attributes to override `toc` behavior per matching element
6-
*
76
*/
87
export interface TocElementDataAttributes {
98
/** whether to ignore this element when searching for matching elements */
@@ -96,10 +95,10 @@ export interface TocReferenceMarkerDataAttributes {
9695
export interface TocDataAttributes extends TocElementDataAttributes, TocObserveDataAttributes, TocReferenceMarkerDataAttributes {}
9796

9897
/**
99-
* ambient typing for `toc` event handlers
100-
*
98+
* event attributes on toc root, set by `toc.actions.root`
10199
*/
102-
export interface TocEventAttributes {
100+
export interface TocRootEventAttributes {
103101
ontocinit?: (event: CustomEvent<Toc>) => void;
104102
ontocchange?: (event: CustomEvent<Toc>) => void;
105103
}
104+

packages/toc/src/attributes/index.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/toc/src/constants.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
import { ATTRIBUTES } from './attributes/index.js';
1+
/**
2+
* all relevant data attribute name literals
3+
* @package
4+
*/
5+
export const ATTRIBUTES = /** @type {Record<string, keyof (import('./attributes').TocDataAttributes)>} */({
6+
// markers from `@svelte-put/preprocess-auo-slug`
7+
autoslug: 'data-auto-slug',
8+
autoSlugAnchor: 'data-auto-slug-anchor',
9+
autoSlugAnchorPosition: 'data-auto-slug-anchor-position',
10+
// markers
11+
toc: 'data-toc',
12+
anchor: 'data-toc-anchor',
13+
root: 'data-toc-root',
14+
// customization per matching element
15+
id: 'data-toc-id',
16+
ignore: 'data-toc-ignore',
17+
strategy: 'data-toc-strategy',
18+
threshold: 'data-toc-threshold',
19+
// tracking information for `IntersectionObserver`
20+
observeFor: 'data-toc-observe-for',
21+
observeThrottled: 'data-toc-observe-throttled',
22+
observeActiveId: 'data-toc-observe-active-id',
23+
// for elements that `use:toclink`
24+
linkFor: 'data-toc-link-for',
25+
linkActive: 'data-toc-link-active',
26+
});
227

328
/**
429
* The default {@link TocConfig} for `toc` action
530
*/
6-
export const DEFAULT_TOC_CONFIG = /** @satisfies {import('./types.js').TocConfig} */ ({
31+
export const DEFAULT_TOC_CONFIG = /** @satisfies {import('./types.public').TocConfig} */ ({
732
selector: ':where(h1, h2, h3, h4, h5, h6)',
833
ignore: '.toc-exclude',
934
scrollMarginTop: 0,
@@ -28,3 +53,4 @@ export const DEFAULT_TOC_CONFIG = /** @satisfies {import('./types.js').TocConfig
2853
},
2954
},
3055
});
56+

packages/toc/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
// Copyright (c) Quang Phan. All rights reserved. Licensed under the MIT license.
22

33
export { Toc } from './toc.svelte.js';
4+
export * from './types.public.js';
5+

0 commit comments

Comments
 (0)