@astryxdesign/cli 0.7.16 → 0.7.17-canary.g0135e03
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -86
- package/api/build/_adapter.d.mts +12 -0
- package/api/build/_adapter.mjs +51 -34
- package/api/build/blocks/blocks.d.mts +7 -5
- package/api/build/blocks/blocks.mjs +113 -26
- package/api/build/build.doc.mjs +4 -3
- package/api/build/build.type.d.mts +2 -2
- package/api/build/build.type.mjs +3 -3
- package/api/build/kit/kind.d.mts +2 -1
- package/api/build/kit/kind.mjs +199 -10
- package/api/build/kit/kit.mjs +6 -4
- package/api/build/kit/rank.d.mts +4 -2
- package/api/build/kit/rank.mjs +125 -24
- package/api/build/kit/start.d.mts +5 -4
- package/api/build/kit/start.mjs +12 -8
- package/api/build/kit/weights.d.mts +39 -3
- package/api/build/kit/weights.json +1 -1
- package/api/build/kit/weights.mjs +340 -43
- package/api/build/kit/word-model.d.mts +23 -0
- package/api/build/kit/word-model.mjs +46 -0
- package/api/build/kit/words.d.mts +48 -0
- package/api/build/kit/words.mjs +90 -1
- package/api/component/__fixtures__/resolutions/modules.txt +4 -5
- package/api/component/list/list.d.mts +1 -1
- package/api/component/list/list.mjs +48 -6
- package/api/discover/_adapter.d.mts +1 -1
- package/api/discover/search/search.mjs +3 -2
- package/api/docs/_adapter.d.mts +26 -2
- package/api/docs/_adapter.mjs +219 -6
- package/api/docs/docs.d.mts +3 -0
- package/api/docs/docs.doc.mjs +7 -1
- package/api/docs/docs.mjs +12 -0
- package/api/docs/docs.type.d.mts +5 -0
- package/api/docs/docs.type.mjs +2 -0
- package/api/docs/node/node.mjs +1 -1
- package/api/doctor/_adapter.d.mts +27 -5
- package/api/doctor/_adapter.mjs +83 -10
- package/api/doctor/built-checks.mjs +40 -14
- package/api/doctor/doctor.d.mts +4 -3
- package/api/doctor/doctor.mjs +14 -7
- package/api/doctor/theme-checks.mjs +121 -17
- package/api/icon/_records.mjs +2 -1
- package/api/index.d.mts +2 -0
- package/api/index.mjs +2 -0
- package/api/integration/_adapter.d.mts +44 -0
- package/api/integration/_adapter.mjs +77 -1
- package/api/integration/authoring-checks.mjs +6 -6
- package/api/integration/example-scope.d.mts +14 -0
- package/api/integration/example-scope.mjs +44 -0
- package/api/integration/example-scope.type.d.mts +29 -0
- package/api/integration/example-scope.type.mjs +25 -0
- package/api/integration/integrationExampleScope.doc.d.mts +11 -0
- package/api/integration/integrationExampleScope.doc.mjs +59 -0
- package/api/integration/validate-integration.mjs +19 -1
- package/api/integration/validate-integration.type.d.mts +9 -0
- package/api/integration/validate-integration.type.mjs +1 -1
- package/api/integration/validateIntegration.doc.mjs +1 -1
- package/api/render/_adapter.mjs +591 -195
- package/api/render/render.doc.mjs +11 -3
- package/api/render/render.type.d.mts +52 -0
- package/api/render/render.type.mjs +31 -1
- package/api/search/ORDERING.md +1 -1
- package/api/search/_adapter.d.mts +9 -0
- package/api/search/_adapter.mjs +85 -0
- package/api/search/_candidate-score.mjs +51 -32
- package/api/search/_fit-order.mjs +3 -3
- package/api/search/_gather-components.d.mts +3 -2
- package/api/search/_gather-components.mjs +12 -3
- package/api/search/_lexical.d.mts +5 -0
- package/api/search/_lexical.mjs +8 -2
- package/api/search/_result.d.mts +11 -9
- package/api/search/_result.mjs +4 -4
- package/api/search/_score.d.mts +6 -1
- package/api/search/_score.mjs +15 -3
- package/api/search/search-index.mjs +7 -1
- package/api/search/search.d.mts +1 -1
- package/api/theme/_adapter.mjs +12 -1
- package/api/theme/build/build.d.mts +122 -1
- package/api/theme/build/build.mjs +412 -84
- package/api/theme/build/core-interception.d.mts +10 -0
- package/api/theme/build/core-interception.mjs +10 -0
- package/api/theme/build/font-warning.d.mts +23 -3
- package/api/theme/build/font-warning.mjs +54 -5
- package/api/theme/build/icon-imports.mjs +25 -2
- package/api/theme/theme.type.d.mts +10 -0
- package/api/theme/theme.type.mjs +7 -1
- package/api/theme-edit/_adapter.d.mts +103 -3
- package/api/theme-edit/_adapter.mjs +961 -82
- package/api/theme-edit/_catalog.mjs +19 -4
- package/api/theme-edit/_check.d.mts +63 -24
- package/api/theme-edit/_check.mjs +2467 -362
- package/api/theme-edit/_color.d.mts +10 -0
- package/api/theme-edit/_color.mjs +49 -1
- package/api/theme-edit/_compiler.mjs +89 -6
- package/api/theme-edit/_context.d.mts +3 -3
- package/api/theme-edit/_context.mjs +165 -54
- package/api/theme-edit/_css.d.mts +14 -0
- package/api/theme-edit/_css.mjs +46 -1
- package/api/theme-edit/_edit.d.mts +31 -4
- package/api/theme-edit/_edit.mjs +2433 -204
- package/api/theme-edit/_how-commands.d.mts +72 -1
- package/api/theme-edit/_how-commands.mjs +759 -9
- package/api/theme-edit/_how-question.d.mts +82 -5
- package/api/theme-edit/_how-question.mjs +6473 -553
- package/api/theme-edit/_how.d.mts +2 -1
- package/api/theme-edit/_how.mjs +237 -41
- package/api/theme-edit/_marks.d.mts +31 -5
- package/api/theme-edit/_marks.mjs +57 -5
- package/api/theme-edit/_name-rule.d.mts +11 -0
- package/api/theme-edit/_name-rule.mjs +12 -0
- package/api/theme-edit/_normal.d.mts +292 -0
- package/api/theme-edit/_normal.mjs +1454 -0
- package/api/theme-edit/_palette.mjs +10 -4
- package/api/theme-edit/_part-words.d.mts +31 -0
- package/api/theme-edit/_part-words.mjs +126 -0
- package/api/theme-edit/_parts.d.mts +18 -1
- package/api/theme-edit/_parts.mjs +246 -23
- package/api/theme-edit/_preview-app.d.mts +7 -1
- package/api/theme-edit/_preview-app.mjs +776 -187
- package/api/theme-edit/_preview.d.mts +100 -14
- package/api/theme-edit/_preview.mjs +582 -139
- package/api/theme-edit/_project.d.mts +4 -0
- package/api/theme-edit/_project.mjs +3 -1
- package/api/theme-edit/_settings.d.mts +417 -42
- package/api/theme-edit/_settings.mjs +3262 -424
- package/api/theme-edit/_tables.d.mts +15 -0
- package/api/theme-edit/_tables.mjs +63 -1
- package/api/theme-edit/_vet.d.mts +20 -0
- package/api/theme-edit/_vet.mjs +47 -2
- package/api/theme-edit/theme-edit.type.d.mts +10 -1
- package/api/theme-edit/theme-edit.type.mjs +4 -1
- package/api/theme-edit/themeEditHow.doc.mjs +1 -1
- package/api/theme-edit/themeEditNew.doc.d.mts +0 -1
- package/api/theme-edit/themeEditNew.doc.mjs +7 -4
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +2 -2
- package/api/upgrade/run/run.mjs +4 -3
- package/assets/codemods/integration-discovery.mjs +2 -1
- package/assets/codemods/run-codemod.mjs +87 -7
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
- package/assets/component-list.generated.json +1 -0
- package/assets/docs/author-a-theme.doc.mjs +1 -0
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
- package/assets/docs/tree/checks.doc.mjs +1 -1
- package/assets/docs/tree/cli-command.doc.mjs +2 -3
- package/assets/docs/tree/render-pages.doc.mjs +44 -3
- package/assets/docs/tree/single-component.doc.mjs +4 -0
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/search-catalog.generated.json +2 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
- package/assets/templates/blocks/components/Hooks/useContainerRevealHookUsage.tsx +2 -2
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
- package/assets/templates/pages/ai-chat-landing/template.doc.mjs +5 -1
- package/assets/templates/pages/blank/template.doc.mjs +1 -1
- package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
- package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
- package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
- package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
- package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
- package/assets/templates/pages/documentation/template.doc.mjs +4 -1
- package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
- package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
- package/assets/templates/pages/editor/template.doc.mjs +2 -8
- package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
- package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
- package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
- package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
- package/assets/templates/pages/ide/page.tsx +24 -2
- package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
- package/assets/templates/pages/library/template.doc.mjs +4 -1
- package/assets/templates/pages/login/template.doc.mjs +2 -1
- package/assets/templates/pages/login-card/template.doc.mjs +2 -1
- package/assets/templates/pages/login-split/template.doc.mjs +2 -1
- package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
- package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
- package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
- package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
- package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
- package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
- package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
- package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
- package/assets/templates/pages/table-page/template.doc.mjs +5 -4
- package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
- package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
- package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
- package/assets/theme.template.ts +6 -0
- package/authoring/debug/parse.d.mts +3 -3
- package/authoring/doctypes/_schema.d.mts +3 -3
- package/authoring/doctypes/command/command.doc.mjs +1 -1
- package/authoring/doctypes/component/categories.d.mts +37 -0
- package/authoring/doctypes/component/categories.mjs +54 -0
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +9 -0
- package/authoring/example/example.doc.d.mts +12 -0
- package/authoring/example/example.doc.mjs +69 -0
- package/authoring/example/preview.d.mts +92 -0
- package/authoring/example/preview.mjs +994 -0
- package/authoring/example/type.ts +102 -0
- package/authoring/index.d.mts +1 -0
- package/authoring/index.d.ts +12 -0
- package/authoring/index.mjs +3 -1
- package/clients/cli/bin/astryx.mjs +6 -9
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/component.doc.mjs +1 -1
- package/clients/cli/commands/component.mjs +22 -3
- package/clients/cli/commands/docs.doc.mjs +7 -1
- package/clients/cli/commands/docs.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +32 -14
- package/clients/cli/commands/experimental/render.mjs +42 -1
- package/clients/cli/commands/experimental/theme.mjs +358 -63
- package/clients/cli/commands/experimental-render.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +87 -1
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +10 -3
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +2 -1
- package/clients/cli/commands/experimental-theme.doc.mjs +5 -1
- package/clients/cli/commands/search.doc.mjs +2 -1
- package/clients/cli/commands/search.mjs +6 -1
- package/clients/cli/commands/template.mjs +4 -2
- package/clients/cli/commands/theme-build.doc.mjs +5 -1
- package/clients/cli/commands/theme.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +317 -47
- package/clients/cli/commands/upgrade.mjs +42 -7
- package/clients/cli/index.mjs +1 -1
- package/clients/cli/lib/cli-error.mjs +2 -1
- package/clients/cli/lib/component-format.mjs +205 -98
- package/clients/cli/lib/hook-format.mjs +9 -19
- package/clients/cli/lib/json-shim.mjs +92 -21
- package/clients/cli/lib/manifest.mjs +3 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/config/theme-state.d.mts +13 -1
- package/foundation/config/theme-state.mjs +47 -7
- package/foundation/discovery/authoring-self-docs.mjs +1 -0
- package/foundation/discovery/component-discovery.d.mts +11 -0
- package/foundation/discovery/component-discovery.mjs +56 -8
- package/foundation/discovery/component-replacement.mjs +130 -1
- package/foundation/discovery/copied-passages.mjs +2 -1
- package/foundation/discovery/doc-types.mjs +4 -3
- package/foundation/discovery/entry-name-files.d.mts +43 -0
- package/foundation/discovery/entry-name-files.mjs +264 -0
- package/foundation/discovery/hook-discovery.d.mts +8 -2
- package/foundation/discovery/hook-discovery.mjs +22 -8
- package/foundation/discovery/icon-definition-discovery.mjs +2 -1
- package/foundation/discovery/icon-set-checks.mjs +2 -1
- package/foundation/discovery/icon-set-discovery.mjs +2 -1
- package/foundation/discovery/integration-doc-files.mjs +2 -1
- package/foundation/discovery/package-exports.d.mts +10 -0
- package/foundation/discovery/package-exports.mjs +56 -3
- package/foundation/discovery/prop-drift.mjs +9 -9
- package/foundation/discovery/runner-allowlist.d.mts +1 -0
- package/foundation/discovery/runner-allowlist.mjs +2 -0
- package/foundation/discovery/stored-component-list.d.mts +43 -0
- package/foundation/discovery/stored-component-list.mjs +90 -0
- package/foundation/discovery/stored-search-catalog.d.mts +115 -0
- package/foundation/discovery/stored-search-catalog.mjs +297 -0
- package/foundation/discovery/stored-self-audit.d.mts +26 -10
- package/foundation/discovery/stored-self-audit.mjs +38 -18
- package/foundation/discovery/template-adapter.d.mts +53 -1
- package/foundation/discovery/template-adapter.mjs +176 -12
- package/foundation/discovery/template-icons.mjs +2 -1
- package/foundation/discovery/theme-discovery.mjs +31 -4
- package/foundation/discovery/theme-imports.d.mts +14 -0
- package/foundation/discovery/theme-imports.mjs +43 -1
- package/foundation/discovery/theming-targets.mjs +3 -2
- package/foundation/doc-compiler/import.mjs +7 -1
- package/foundation/doc-compiler/inputs.mjs +3 -2
- package/foundation/doc-compiler/parse-readable.mjs +4 -1
- package/foundation/doc-compiler/stored-docs.d.mts +22 -0
- package/foundation/doc-compiler/stored-docs.mjs +49 -0
- package/foundation/env/semver.mjs +3 -1
- package/foundation/fonts/font-stylesheet.d.mts +31 -0
- package/foundation/fonts/font-stylesheet.mjs +115 -0
- package/foundation/fs/cache-eviction.d.mts +21 -0
- package/foundation/fs/cache-eviction.mjs +73 -0
- package/foundation/fs/exact-codec.d.mts +12 -2
- package/foundation/fs/exact-codec.mjs +23 -6
- package/foundation/fs/file-protection.mjs +4 -3
- package/foundation/fs/gather-cache.d.mts +62 -4
- package/foundation/fs/gather-cache.mjs +512 -91
- package/foundation/fs/jiti-cache.d.mts +19 -1
- package/foundation/fs/jiti-cache.mjs +130 -6
- package/foundation/fs/kept-tree.d.mts +60 -0
- package/foundation/fs/kept-tree.mjs +154 -0
- package/foundation/fs/listing-cache.d.mts +7 -0
- package/foundation/fs/listing-cache.mjs +10 -0
- package/foundation/fs/module-loader.d.mts +24 -2
- package/foundation/fs/module-loader.mjs +94 -21
- package/foundation/fs/paths.d.mts +18 -0
- package/foundation/fs/paths.mjs +15 -2
- package/foundation/fs/read-record.d.mts +9 -0
- package/foundation/fs/read-record.mjs +19 -11
- package/foundation/fs/stop-cleanup.d.mts +12 -0
- package/foundation/fs/stop-cleanup.mjs +66 -0
- package/foundation/identity/provider-identity.d.mts +6 -0
- package/foundation/identity/provider-identity.mjs +10 -1
- package/foundation/integrations/contribution-inventory.mjs +4 -3
- package/foundation/integrations/example-code.d.mts +63 -0
- package/foundation/integrations/example-code.mjs +416 -0
- package/foundation/integrations/public-imports.d.mts +9 -0
- package/foundation/integrations/public-imports.mjs +3 -2
- package/foundation/integrations/validate-contributions.mjs +17 -45
- package/foundation/render/embedded-files.d.mts +6 -0
- package/foundation/render/embedded-files.mjs +25 -5
- package/foundation/render/embedded-imports.d.mts +39 -7
- package/foundation/render/embedded-imports.mjs +66 -27
- package/foundation/render/file-header.d.mts +66 -0
- package/foundation/render/file-header.mjs +118 -0
- package/foundation/render/host-answer.d.mts +18 -0
- package/foundation/render/host-answer.mjs +46 -0
- package/foundation/render/host-exit.d.mts +1 -0
- package/foundation/render/host-exit.mjs +6 -1
- package/foundation/render/html-reader.d.mts +55 -2
- package/foundation/render/html-reader.mjs +145 -26
- package/foundation/render/markup-edits.d.mts +57 -0
- package/foundation/render/markup-edits.mjs +182 -0
- package/foundation/render/page-host.mjs +29 -12
- package/foundation/render/page-worker.mjs +121 -13
- package/foundation/render/picture-chrome.d.mts +4 -0
- package/foundation/render/picture-chrome.mjs +185 -0
- package/foundation/render/picture-playwright.d.mts +4 -0
- package/foundation/render/picture-playwright.mjs +34 -0
- package/foundation/render/rebuild.mjs +2 -1
- package/foundation/render/render-page.d.mts +35 -3
- package/foundation/render/render-page.mjs +641 -96
- package/foundation/render/source-block.d.mts +85 -6
- package/foundation/render/source-block.mjs +211 -20
- package/foundation/render/ts-paths.d.mts +69 -6
- package/foundation/render/ts-paths.mjs +326 -40
- package/foundation/response/error-codes.d.mts +3 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +12 -1
- package/foundation/response/error-codes.mjs +11 -1
- package/foundation/response/response-types.doc.mjs +2 -2
- package/foundation/text/compare-text.d.mts +14 -0
- package/foundation/text/compare-text.mjs +68 -0
- package/foundation/text/string-utils.d.mts +3 -2
- package/foundation/text/string-utils.mjs +18 -4
- package/foundation/text/suggest-order.d.mts +9 -0
- package/foundation/text/suggest-order.mjs +15 -1
- package/foundation/xle/expand.mjs +2 -1
- package/host/index.mjs +8 -6
- package/package.json +7 -6
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @position api/theme-edit — storage and compile for the theme commands
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {EDITOR_GENERATED_MARK} from './_marks.mjs';
|
|
13
|
+
import {APP_COPY_MARK, EDITOR_GENERATED_MARK} from './_marks.mjs';
|
|
14
14
|
import {parse as parseModule} from '@babel/parser';
|
|
15
15
|
import {createHash} from 'node:crypto';
|
|
16
16
|
import * as fs from 'node:fs';
|
|
@@ -25,8 +25,19 @@ import {AstryxError} from '../error.mjs';
|
|
|
25
25
|
import {logger} from '../logger.mjs';
|
|
26
26
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
27
27
|
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
28
|
-
import {themeBuildForReport} from '../theme/build/build.mjs';
|
|
29
28
|
import {
|
|
29
|
+
loadThemeExport,
|
|
30
|
+
loadThemeInput,
|
|
31
|
+
oneColorMode,
|
|
32
|
+
themeBuildForReport,
|
|
33
|
+
} from '../theme/build/build.mjs';
|
|
34
|
+
import {
|
|
35
|
+
fontShorthandFamilyList,
|
|
36
|
+
namedFontFamilies,
|
|
37
|
+
} from '../theme/build/font-warning.mjs';
|
|
38
|
+
import {declarationValues} from '../../foundation/fonts/font-stylesheet.mjs';
|
|
39
|
+
import {
|
|
40
|
+
MODE_WORDS,
|
|
30
41
|
AUTO_SPREAD,
|
|
31
42
|
CORNERS,
|
|
32
43
|
DIALS,
|
|
@@ -46,6 +57,7 @@ import {
|
|
|
46
57
|
WRAPPED_PARTS,
|
|
47
58
|
CARD_LIKE,
|
|
48
59
|
} from './_tables.mjs';
|
|
60
|
+
import {booleanWord, cleanDialValue, typoLimit, typoNear} from './_normal.mjs';
|
|
49
61
|
import {
|
|
50
62
|
alphaOf,
|
|
51
63
|
colorProblem,
|
|
@@ -81,8 +93,10 @@ import {
|
|
|
81
93
|
baseModule,
|
|
82
94
|
baseResolved,
|
|
83
95
|
baseThemeFile,
|
|
96
|
+
installLine,
|
|
84
97
|
} from './_project.mjs';
|
|
85
98
|
import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
99
|
+
import {compareText} from '../../foundation/text/compare-text.mjs';
|
|
86
100
|
|
|
87
101
|
// The palette reads the tokens the own source sets through this module.
|
|
88
102
|
setOwnTokenReader((cwd, slug) => ownSourceTokenValues(cwd, slug));
|
|
@@ -116,13 +130,13 @@ setOwnTokenReader((cwd, slug) => ownSourceTokenValues(cwd, slug));
|
|
|
116
130
|
/** What lifted secondary text sits on: the contrast check measures it there. */
|
|
117
131
|
const LIFT_GROUND_TOKENS = new Set(LIFT_PARTS.map(([, , g]) => g));
|
|
118
132
|
|
|
119
|
-
/** @typedef {{project: import('./_project.mjs').ThemeProject, C: import('./_compiler.mjs').Compiler, unchecked?: string | null, uncheckedCode?: string | null, wrote?: Map<string, string | null>, base?: {light: Record<string, string>, dark: Record<string, string>, components?: Record<string, any>}, keepStops?: Record<string, Record<'light' | 'dark', {family: string, stop: number, alpha?: string}>> | null, keepRadii?: Record<string, string> | null, derived?: Record<string, Array<{property: string, vars?: string[], replaces?: boolean, expand?: string}>>}} Ctx */
|
|
133
|
+
/** @typedef {{project: import('./_project.mjs').ThemeProject, C: import('./_compiler.mjs').Compiler, unchecked?: string | null, uncheckedCode?: string | null, wrote?: Map<string, string | null>, base?: {light: Record<string, string>, dark: Record<string, string>, components?: Record<string, any>}, keepStops?: Record<string, Record<'light' | 'dark', {family: string, stop: number, alpha?: string}>> | null, keepRadii?: Record<string, string> | null, sizeNotes?: string[], compileNotes?: string[], derived?: Record<string, Array<{property: string, vars?: string[], replaces?: boolean, expand?: string}>>}} Ctx */
|
|
120
134
|
/** @typedef {import('./_parts.mjs').PaintSide} PaintSide */
|
|
121
135
|
/** @typedef {import('./_parts.mjs').TextPair} TextPair */
|
|
122
136
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
137
|
+
import {NAME_RULE} from './_name-rule.mjs';
|
|
138
|
+
|
|
139
|
+
export {NAME_RULE};
|
|
126
140
|
|
|
127
141
|
/** A theme name: lowercase letters, digits and dashes, starting with a letter, 64 characters at most. @param {string} s */
|
|
128
142
|
export const slugOk = s =>
|
|
@@ -197,20 +211,35 @@ export function themeFiles(cwd, slug) {
|
|
|
197
211
|
}
|
|
198
212
|
|
|
199
213
|
/** Files a system or a tool puts in any folder; a folder of only these is empty. */
|
|
200
|
-
export const FOLDER_JUNK = new Set([
|
|
214
|
+
export const FOLDER_JUNK = new Set([
|
|
215
|
+
'.DS_Store',
|
|
216
|
+
'.gitkeep',
|
|
217
|
+
'Thumbs.db',
|
|
218
|
+
'desktop.ini',
|
|
219
|
+
]);
|
|
201
220
|
|
|
202
221
|
/**
|
|
203
222
|
* Whether `dir` holds nothing of anyone's: absent, empty, or only system
|
|
204
223
|
* files (what `git rm -r` or a branch switch leaves). A path that is not a
|
|
205
224
|
* folder, or one that does not read, is not empty.
|
|
206
225
|
* @param {string} dir
|
|
226
|
+
* @returns {boolean}
|
|
207
227
|
*/
|
|
208
228
|
export function folderIsEmpty(dir) {
|
|
209
229
|
if (!fs.existsSync(dir)) return true;
|
|
210
230
|
try {
|
|
231
|
+
// A folder holding only empty folders (an empty tokens/) holds nothing
|
|
232
|
+
// either; a link is never followed.
|
|
211
233
|
return (
|
|
212
234
|
fs.lstatSync(dir).isDirectory() &&
|
|
213
|
-
fs
|
|
235
|
+
fs
|
|
236
|
+
.readdirSync(dir)
|
|
237
|
+
.every(
|
|
238
|
+
n =>
|
|
239
|
+
FOLDER_JUNK.has(n) ||
|
|
240
|
+
(fs.lstatSync(path.join(dir, n)).isDirectory() &&
|
|
241
|
+
folderIsEmpty(path.join(dir, n))),
|
|
242
|
+
)
|
|
214
243
|
);
|
|
215
244
|
} catch {
|
|
216
245
|
return false;
|
|
@@ -290,34 +319,17 @@ export function editorWrote(cwd, slug, file) {
|
|
|
290
319
|
}
|
|
291
320
|
|
|
292
321
|
/**
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
322
|
+
* Whether the pictures record names this file and it is unchanged since the
|
|
323
|
+
* editor wrote it. Only such a file is ever deleted: a stamp alone is not
|
|
324
|
+
* enough, since a byte copy of a picture (preview-before.png) carries the
|
|
325
|
+
* same stamp and is the person's.
|
|
297
326
|
* @param {string} cwd
|
|
298
327
|
* @param {string} slug
|
|
299
328
|
* @param {string} file
|
|
300
329
|
*/
|
|
301
|
-
export function
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
return false;
|
|
305
|
-
/** @type {Buffer} */
|
|
306
|
-
let bytes;
|
|
307
|
-
try {
|
|
308
|
-
bytes = fs.readFileSync(file);
|
|
309
|
-
} catch {
|
|
310
|
-
return false;
|
|
311
|
-
}
|
|
312
|
-
// The width in the PNG header: the editor takes a 1280-pixel window.
|
|
313
|
-
if (
|
|
314
|
-
bytes.length < 24 ||
|
|
315
|
-
bytes.toString('latin1', 12, 16) !== 'IHDR' ||
|
|
316
|
-
bytes.readUInt32BE(16) !== 1280
|
|
317
|
-
)
|
|
318
|
-
return false;
|
|
319
|
-
recordWritten(cwd, slug, [file]);
|
|
320
|
-
return true;
|
|
330
|
+
export function editorRecorded(cwd, slug, file) {
|
|
331
|
+
const was = readWrittenRecord(cwd, slug)[path.basename(file)];
|
|
332
|
+
return Boolean(was) && was === contentHash(file);
|
|
321
333
|
}
|
|
322
334
|
|
|
323
335
|
/**
|
|
@@ -470,7 +482,7 @@ export function editedThemes(cwd, order = 'name') {
|
|
|
470
482
|
};
|
|
471
483
|
return names
|
|
472
484
|
.map(d => ({d, t: when(d)}))
|
|
473
|
-
.sort((a, b) => b.t - a.t || a.d
|
|
485
|
+
.sort((a, b) => b.t - a.t || compareText(a.d, b.d))
|
|
474
486
|
.map(x => x.d);
|
|
475
487
|
}
|
|
476
488
|
|
|
@@ -485,21 +497,51 @@ export const shown = (cwd, f) =>
|
|
|
485
497
|
* @param {number} [n]
|
|
486
498
|
*/
|
|
487
499
|
export function nearest(x, list, n = 3) {
|
|
500
|
+
// A phrase that negates a word (not round, no caps, unbold,
|
|
501
|
+
// rounded-none) never has that word as its nearest: it asks the opposite.
|
|
502
|
+
// A typo of a listed keyword that itself starts with no- or not-
|
|
503
|
+
// (`not-alowed` for not-allowed, `no-repat` for no-repeat) is no
|
|
504
|
+
// negation: it is read as a typo.
|
|
505
|
+
if (
|
|
506
|
+
negates(x) &&
|
|
507
|
+
![...list].some(c => /^(no|not)-/.test(c) && typoNear(x, c))
|
|
508
|
+
)
|
|
509
|
+
return [];
|
|
510
|
+
// A long input (a pasted stylesheet or blob) holds many names inside
|
|
511
|
+
// it: it is near nothing.
|
|
512
|
+
if (x.length > 64) return [];
|
|
513
|
+
// Only within the editor's one typo limit (typoLimit: none under four
|
|
514
|
+
// letters, so only the name itself in other letters is near those),
|
|
515
|
+
// nearest by edit distance first (brand-drak is brand-dark, not brand). A
|
|
516
|
+
// name that only holds the typed word inside it is no typo of it.
|
|
517
|
+
const limit = typoLimit(x);
|
|
518
|
+
const typed = x.toLowerCase();
|
|
488
519
|
return [...list]
|
|
489
520
|
.filter(c => c.length > 2)
|
|
490
521
|
.map(
|
|
491
522
|
c =>
|
|
492
523
|
/** @type {[string, number]} */ ([
|
|
493
524
|
c,
|
|
494
|
-
|
|
525
|
+
levenshteinDistance(typed, c.toLowerCase(), limit, {swaps: true}),
|
|
495
526
|
]),
|
|
496
527
|
)
|
|
497
|
-
.filter(([,
|
|
528
|
+
.filter(([, d]) => d <= limit)
|
|
498
529
|
.sort((a, b) => a[1] - b[1])
|
|
499
530
|
.slice(0, n)
|
|
500
531
|
.map(([c]) => c);
|
|
501
532
|
}
|
|
502
533
|
|
|
534
|
+
/**
|
|
535
|
+
* Whether typed words negate what they name (`not round`, `no caps`,
|
|
536
|
+
* `anything but blue`, `unbold`, `non-bold`, `rounded-none`).
|
|
537
|
+
* @param {string} x
|
|
538
|
+
*/
|
|
539
|
+
export function negates(x) {
|
|
540
|
+
return /(^|[\s-])(not|no|non|without|never|less)([\s-]|$)|anything but|^un(round|bold|rais|outlin|cap)|-none$/i.test(
|
|
541
|
+
String(x).trim(),
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
|
|
503
545
|
/**
|
|
504
546
|
* A refusal: nothing was changed, and the suggestions lead with the command
|
|
505
547
|
* that does what the person wanted.
|
|
@@ -577,6 +619,100 @@ export function typedThemeName(s) {
|
|
|
577
619
|
);
|
|
578
620
|
}
|
|
579
621
|
|
|
622
|
+
/**
|
|
623
|
+
* The theme names a name typed the way the app shows it may mean, most
|
|
624
|
+
* likely first: a path with doubled slashes or `../` (`themes//t`), a file
|
|
625
|
+
* name with or without its folder (`brand.theme.ts`, `tTheme.ts`), the app
|
|
626
|
+
* copy's export (`tTheme`), camelCase (`brandDark`) and spaces
|
|
627
|
+
* (`Brand Dark`) each read as dashes. The caller picks the first that
|
|
628
|
+
* exists, else the first.
|
|
629
|
+
* @param {string} s
|
|
630
|
+
* @returns {string[]}
|
|
631
|
+
*/
|
|
632
|
+
export function themeNameReadings(s) {
|
|
633
|
+
const base = unlabeledThemeName(String(s ?? ''))
|
|
634
|
+
.replace(/[\\/]+/g, '/')
|
|
635
|
+
.replace(/^(\.\.?\/)+/, '')
|
|
636
|
+
.replace(/^.*themes\//i, '')
|
|
637
|
+
.replace(/\/.*$/, '')
|
|
638
|
+
.replace(
|
|
639
|
+
/\.(theme\.ts|settings\.(json|ts)|history\.json|css\.d\.ts|d\.ts|doc\.mjs|fonts\.css|css|mjs|js|tsx?)$/i,
|
|
640
|
+
'',
|
|
641
|
+
)
|
|
642
|
+
.trim();
|
|
643
|
+
const noTheme = base.replace(/([a-z0-9])Theme$/, '$1');
|
|
644
|
+
const read = [base, noTheme === base ? '' : noTheme]
|
|
645
|
+
.flatMap(x => [kebab(x), x.toLowerCase()])
|
|
646
|
+
.filter(x => /[a-z]/i.test(x))
|
|
647
|
+
.map(x => slugFor(x));
|
|
648
|
+
// A module name in any letters (brandtheme, brand-theme) last: only
|
|
649
|
+
// when the theme it names exists does it win.
|
|
650
|
+
const moduleNames = read
|
|
651
|
+
.map(n => n.replace(/-?theme$/, ''))
|
|
652
|
+
.filter(n => n && slugOk(n));
|
|
653
|
+
return [...new Set([...read, ...moduleNames])];
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* A vibrancy written as set reads typed input: a share of one (0.5) or a
|
|
658
|
+
* percent (50%) is that much of the 0-100 scale, as a whole number. Any
|
|
659
|
+
* other dial's value, or a vibrancy that is no such number, comes back as
|
|
660
|
+
* it was.
|
|
661
|
+
* @param {string} dial
|
|
662
|
+
* @param {string} v
|
|
663
|
+
* @returns {string}
|
|
664
|
+
*/
|
|
665
|
+
export function vibrancyMeant(dial, v) {
|
|
666
|
+
if (dial !== 'vibrancy') return v;
|
|
667
|
+
const m = /^(\d*\.?\d+)\s*(%?)$/.exec(v.trim());
|
|
668
|
+
if (!m) return v;
|
|
669
|
+
const n = Number(m[1]);
|
|
670
|
+
const scaled = !m[2] && n > 0 && n < 1 ? n * 100 : n;
|
|
671
|
+
return scaled <= 100 ? String(Math.round(scaled)) : v;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* The mode and base a damaged theme file meant, read the way typed input
|
|
676
|
+
* is: other letters, stray spaces, `_` or a space for a dash (`Dark-Only`,
|
|
677
|
+
* ` Neutral`), and a base written as its package name
|
|
678
|
+
* (`@astryxdesign/theme-neutral`). Each is the cleaned text even when it is
|
|
679
|
+
* no value the editor takes; the caller checks it.
|
|
680
|
+
* @param {any} data
|
|
681
|
+
* @returns {{mode: string, like: string}}
|
|
682
|
+
*/
|
|
683
|
+
export function fieldsMeant(data) {
|
|
684
|
+
const MODES = ['both', 'light-only', 'dark-only'];
|
|
685
|
+
const typed = String(data?.mode ?? '')
|
|
686
|
+
.trim()
|
|
687
|
+
.toLowerCase()
|
|
688
|
+
.replace(/[\s_]+/g, '-');
|
|
689
|
+
// A mode word (light, night) is that one mode, as `set` reads it; a typo
|
|
690
|
+
// of a mode is that mode.
|
|
691
|
+
const word = /** @type {Record<string, string>} */ ({
|
|
692
|
+
light: 'light-only',
|
|
693
|
+
day: 'light-only',
|
|
694
|
+
dark: 'dark-only',
|
|
695
|
+
night: 'dark-only',
|
|
696
|
+
})[typed];
|
|
697
|
+
const near = (/** @type {string} */ t, /** @type {string[]} */ list) => {
|
|
698
|
+
const hits = list.filter(x => typoNear(t, x));
|
|
699
|
+
return hits.length === 1 ? hits[0] : null;
|
|
700
|
+
};
|
|
701
|
+
const mode = MODES.includes(typed)
|
|
702
|
+
? typed
|
|
703
|
+
: (word ?? (typed ? near(typed, MODES) : null) ?? typed);
|
|
704
|
+
const likeTyped = String(data?.like ?? '')
|
|
705
|
+
.trim()
|
|
706
|
+
.toLowerCase()
|
|
707
|
+
// The package's name or the name with `theme` (theme-neutral,
|
|
708
|
+
// neutral-theme) is the base.
|
|
709
|
+
.replace(/^(@astryxdesign\/)?theme-|-theme$/g, '');
|
|
710
|
+
const like = LIKE.includes(likeTyped)
|
|
711
|
+
? likeTyped
|
|
712
|
+
: ((likeTyped ? near(likeTyped, LIKE) : null) ?? likeTyped);
|
|
713
|
+
return {mode, like};
|
|
714
|
+
}
|
|
715
|
+
|
|
580
716
|
/**
|
|
581
717
|
* The settings of a theme, or a refusal naming the command that makes it.
|
|
582
718
|
* @param {string} cwd
|
|
@@ -605,9 +741,22 @@ export function loadTheme(
|
|
|
605
741
|
);
|
|
606
742
|
const plain = typedThemeName(slug);
|
|
607
743
|
const named = slugFor(plain);
|
|
744
|
+
// brandDark is brand-dark.
|
|
745
|
+
const camelWords = slugFor(
|
|
746
|
+
unlabeledThemeName(String(slug ?? '')).replace(
|
|
747
|
+
/([a-z0-9])([A-Z])/g,
|
|
748
|
+
'$1-$2',
|
|
749
|
+
),
|
|
750
|
+
);
|
|
608
751
|
const exists = (/** @type {string} */ n) =>
|
|
609
752
|
Boolean(n) && fs.existsSync(themeFiles(cwd, n).settings);
|
|
610
|
-
|
|
753
|
+
// The module or export name (brandTheme, the app copy's brandTheme.ts)
|
|
754
|
+
// names the theme brand: one reader for every way a name is typed.
|
|
755
|
+
const readings = themeNameReadings(slug);
|
|
756
|
+
const fixed =
|
|
757
|
+
[named, whole, camelWords, ...readings].find(exists) ??
|
|
758
|
+
readings[0] ??
|
|
759
|
+
named;
|
|
611
760
|
const same = fs.existsSync(themeFiles(cwd, fixed).settings) ? fixed : null;
|
|
612
761
|
// A list where one theme goes (t,u or "t;u"): one theme at a time,
|
|
613
762
|
// each that exists named; never a new theme made of the list. A name
|
|
@@ -685,14 +834,18 @@ export function loadTheme(
|
|
|
685
834
|
// none): the settings were lost after.
|
|
686
835
|
const stopped =
|
|
687
836
|
fs.existsSync(path.join(f.dir, NEW_MARKER)) && !fs.existsSync(f.history);
|
|
688
|
-
if (!same && (
|
|
837
|
+
if (!same && editorMadeFolder(cwd, slug)) {
|
|
838
|
+
const remake = remakeLines(cwd, slug);
|
|
689
839
|
refuse(
|
|
690
840
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
841
|
+
`${
|
|
842
|
+
stopped
|
|
843
|
+
? `themes/${slug} was left by a \`new\` that was stopped part way, so it has no settings yet. Run new again to finish it; files you added there are kept`
|
|
844
|
+
: `themes/${slug} has lost its settings (${slug}.settings.json is gone), but the editor made it. Run new to make it again: your own files there are kept${fs.existsSync(f.history) ? `, and \`undo ${slug}\` then steps back to the settings from before the last change (the last change itself is not kept)` : ''}`
|
|
845
|
+
}; ${remake.base ? `it is made again on ${remake.base}, the base it was built on` : 'it starts on no base (the base it was built on cannot be read)'}.`,
|
|
846
|
+
{run: remake.lines},
|
|
695
847
|
);
|
|
848
|
+
}
|
|
696
849
|
// A folder copied or renamed by hand from an editor theme: its files
|
|
697
850
|
// still carry the old name (t.settings.json in themes/t2).
|
|
698
851
|
const copied =
|
|
@@ -741,6 +894,30 @@ export function loadTheme(
|
|
|
741
894
|
`themes/${slug} is an empty folder: there is no theme ${slug} yet. new makes it there.`,
|
|
742
895
|
{run: [`${P} new ${slug}`]},
|
|
743
896
|
);
|
|
897
|
+
// The editor's own app copy left behind when themes/<slug>/ is gone
|
|
898
|
+
// (a git clean, a wrong rm, a branch switch): `new` makes the theme
|
|
899
|
+
// again and writes the copy over.
|
|
900
|
+
const copyName = `${camel(slug)}Theme`;
|
|
901
|
+
if (
|
|
902
|
+
!same &&
|
|
903
|
+
other &&
|
|
904
|
+
other[1] === `src/themes/${slug}` &&
|
|
905
|
+
!fs.existsSync(f.dir) &&
|
|
906
|
+
[`${copyName}.ts`, `${copyName}.doc.mjs`, `${slug}.fonts.css`].some(n => {
|
|
907
|
+
try {
|
|
908
|
+
return fs
|
|
909
|
+
.readFileSync(path.join(other[0], n), 'utf8')
|
|
910
|
+
.includes(APP_COPY_MARK);
|
|
911
|
+
} catch {
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
})
|
|
915
|
+
)
|
|
916
|
+
refuse(
|
|
917
|
+
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
918
|
+
`themes/${slug}/, the theme's source folder, is missing; src/themes/${slug}/ is the editor's app copy of it, left behind. \`new ${slug}\` makes the theme again and writes the copy over (its settings are lost: restore themes/${slug}/ from version control to keep them).`,
|
|
919
|
+
{run: [`${P} new ${slug}`]},
|
|
920
|
+
);
|
|
744
921
|
if (!same && (other || LIKE.includes(slug)))
|
|
745
922
|
refuse(
|
|
746
923
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
@@ -752,7 +929,18 @@ export function loadTheme(
|
|
|
752
929
|
...(fs.existsSync(themeFiles(cwd, `my-${slug}`).settings)
|
|
753
930
|
? [`${P} show my-${slug}`]
|
|
754
931
|
: []),
|
|
755
|
-
|
|
932
|
+
...(LIKE.includes(slug)
|
|
933
|
+
? [
|
|
934
|
+
...onBuiltIn(
|
|
935
|
+
cwd,
|
|
936
|
+
slug,
|
|
937
|
+
`${P} new ${usableNewName(cwd, `my-${slug}`)} --like ${slug}`,
|
|
938
|
+
),
|
|
939
|
+
...(baseInstalled(cwd, slug)
|
|
940
|
+
? []
|
|
941
|
+
: [`${P} new ${usableNewName(cwd, `my-${slug}`)}`]),
|
|
942
|
+
]
|
|
943
|
+
: [`${P} new ${usableNewName(cwd, `my-${slug}`)}`]),
|
|
756
944
|
],
|
|
757
945
|
},
|
|
758
946
|
);
|
|
@@ -760,7 +948,24 @@ export function loadTheme(
|
|
|
760
948
|
ERROR_CODES.ERR_UNKNOWN_THEME,
|
|
761
949
|
`There is no theme "${slug}" made by this command (no themes/${slug}/${slug}.settings.json).`,
|
|
762
950
|
(() => {
|
|
763
|
-
|
|
951
|
+
// A doubled or trailing dash (brand--dark, t--): the name as new
|
|
952
|
+
// cleans it, the theme of that name when there is one, never a new
|
|
953
|
+
// theme new refuses.
|
|
954
|
+
const cleaned = String(slug)
|
|
955
|
+
.replace(/-{2,}/g, '-')
|
|
956
|
+
.replace(/^-+|-+$/g, '');
|
|
957
|
+
const cleanedHere =
|
|
958
|
+
cleaned !== slug &&
|
|
959
|
+
slugOk(cleaned) &&
|
|
960
|
+
fs.existsSync(themeFiles(cwd, cleaned).settings)
|
|
961
|
+
? cleaned
|
|
962
|
+
: null;
|
|
963
|
+
const near = same
|
|
964
|
+
? [same]
|
|
965
|
+
: cleanedHere
|
|
966
|
+
? [cleanedHere]
|
|
967
|
+
: nearest(slug, have);
|
|
968
|
+
const newName = usableNewName(cwd, cleaned || slug);
|
|
764
969
|
// A theme near the name comes first; a new theme of the name as
|
|
765
970
|
// typed only after.
|
|
766
971
|
return {
|
|
@@ -768,8 +973,8 @@ export function loadTheme(
|
|
|
768
973
|
run: same
|
|
769
974
|
? [`${P} show ${same}`]
|
|
770
975
|
: near.length
|
|
771
|
-
? [`${P} show ${near[0]}`, `${P} new ${
|
|
772
|
-
: [`${P} new ${
|
|
976
|
+
? [`${P} show ${near[0]}`, `${P} new ${newName}`]
|
|
977
|
+
: [`${P} new ${newName}`],
|
|
773
978
|
};
|
|
774
979
|
})(),
|
|
775
980
|
);
|
|
@@ -780,44 +985,77 @@ export function loadTheme(
|
|
|
780
985
|
refuseUnreadableFiles(cwd, slug);
|
|
781
986
|
// undo is offered only when there is a step to go back to.
|
|
782
987
|
const steps = readHistory(f.history).steps.length;
|
|
988
|
+
// Made again, the theme keeps the base it was built on when that can be
|
|
989
|
+
// read; else the text says it starts on no base.
|
|
990
|
+
const remake = remakeLines(cwd, slug);
|
|
991
|
+
const remakeText = `make the theme again with ${remake.lines.at(-1)}, which keeps the damaged file beside it and every file of yours${remake.base ? `, on ${remake.base}, the base it was built on` : ', on no base (the base it was built on cannot be read)'}`;
|
|
992
|
+
const remakeRun = remake.lines.map(name => ({
|
|
993
|
+
name,
|
|
994
|
+
reason: 'do this instead',
|
|
995
|
+
}));
|
|
783
996
|
const recover = steps
|
|
784
997
|
? {
|
|
785
998
|
text: `, or step back to the settings before the last change with ${P} undo ${slug} (the last change is then lost)`,
|
|
786
999
|
run: [{name: `${P} undo ${slug}`, reason: 'do this instead'}],
|
|
787
1000
|
}
|
|
788
1001
|
: {
|
|
789
|
-
text: `; or
|
|
790
|
-
run:
|
|
1002
|
+
text: `; or ${remakeText}`,
|
|
1003
|
+
run: remakeRun,
|
|
791
1004
|
};
|
|
792
1005
|
/** @type {any} */
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
data = JSON.parse(
|
|
796
|
-
fs.readFileSync(f.settings, 'utf8').replace(/^\uFEFF/, ''),
|
|
797
|
-
);
|
|
798
|
-
} catch {
|
|
1006
|
+
const data = settingsJson(fs.readFileSync(f.settings, 'utf8'));
|
|
1007
|
+
if (data === undefined)
|
|
799
1008
|
throw new AstryxError(
|
|
800
1009
|
`themes/${slug}/${slug}.settings.json is not valid JSON. Fix it by hand${recover.text}.`,
|
|
801
1010
|
recover.run,
|
|
802
1011
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
803
1012
|
);
|
|
804
|
-
}
|
|
805
1013
|
// Valid JSON of the wrong shape ([], {}, settings: null) is damage like
|
|
806
1014
|
// invalid JSON: `new` makes the theme again, keeping the file aside.
|
|
807
1015
|
if (!settingsShapeOk(data))
|
|
808
1016
|
throw new AstryxError(
|
|
809
|
-
`themes/${slug}/${slug}.settings.json is damaged: ${settingsProblem(data) ?? 'it is not a settings object'}. Fix it by hand${steps ? `, step back to the settings before the last change with ${P} undo ${slug},` : ''} or
|
|
1017
|
+
`themes/${slug}/${slug}.settings.json is damaged: ${settingsProblem(data) ?? 'it is not a settings object'}. Fix it by hand${steps ? `, step back to the settings before the last change with ${P} undo ${slug},` : ''} or ${remakeText}. Nothing was changed.`,
|
|
1018
|
+
[...recover.run.filter(r => !/ new /.test(r.name)), ...remakeRun],
|
|
1019
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
1020
|
+
);
|
|
1021
|
+
readEntryWords(data);
|
|
1022
|
+
// `like: "none"`, the word for no base, is no base.
|
|
1023
|
+
if (data.like === 'none') delete data.like;
|
|
1024
|
+
// The folder names the theme: a name missing or changed by hand in the
|
|
1025
|
+
// file is the folder's, and the next save writes it back.
|
|
1026
|
+
data.name = slug;
|
|
1027
|
+
const problem = settingsProblem(data, skip);
|
|
1028
|
+
// An entry whose mode or source means nothing: `undo` with its name
|
|
1029
|
+
// removes that entry only; a step back loses the last change.
|
|
1030
|
+
const damaged = problem ? damagedEntry(data, problem) : null;
|
|
1031
|
+
if (damaged)
|
|
1032
|
+
throw new AstryxError(
|
|
1033
|
+
`themes/${slug}/${slug}.settings.json: ${problem}. Remove that setting with ${P} undo ${slug} ${undoArgs(damagedUndo(damaged))}; nothing else needs to change${steps ? `, or step back to the settings before the last change with ${P} undo ${slug} (the last change is then lost)` : ''}. Nothing was changed.`,
|
|
810
1034
|
[
|
|
811
|
-
|
|
812
|
-
|
|
1035
|
+
{
|
|
1036
|
+
name: `${P} undo ${slug} ${undoArgs(damagedUndo(damaged))}`,
|
|
1037
|
+
reason: 'do this instead: removes only that setting',
|
|
1038
|
+
},
|
|
1039
|
+
...(steps
|
|
1040
|
+
? [
|
|
1041
|
+
{
|
|
1042
|
+
name: `${P} undo ${slug}`,
|
|
1043
|
+
reason:
|
|
1044
|
+
'or step back to the settings before the last change (the last change is then lost)',
|
|
1045
|
+
},
|
|
1046
|
+
]
|
|
1047
|
+
: []),
|
|
813
1048
|
],
|
|
814
1049
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
815
1050
|
);
|
|
816
|
-
const problem = settingsProblem(data, skip);
|
|
817
1051
|
// A value made under looser rules is not damage: the command that
|
|
818
1052
|
// changes or removes that setting still works, with the other bad ones
|
|
819
1053
|
// left out of the build, so each listed fix runs on its own.
|
|
820
|
-
|
|
1054
|
+
// A setting listed twice is named as such, with keep and remove lines,
|
|
1055
|
+
// even when one of its values is bad: removing the bad one by its name
|
|
1056
|
+
// would remove both.
|
|
1057
|
+
const listedTwice = Boolean(problem?.includes(' is listed twice ('));
|
|
1058
|
+
const all = problem && !listedTwice ? badSettings(data) : [];
|
|
821
1059
|
if (
|
|
822
1060
|
problem &&
|
|
823
1061
|
skip &&
|
|
@@ -854,43 +1092,126 @@ export function loadTheme(
|
|
|
854
1092
|
})),
|
|
855
1093
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
856
1094
|
);
|
|
857
|
-
|
|
1095
|
+
// A damaged mode or base is fixed first: the message names it, so the
|
|
1096
|
+
// suggestions set it (another bad value is named on the next run).
|
|
1097
|
+
// (Not an entry named mode or base inside the settings list.)
|
|
1098
|
+
const fieldBroken =
|
|
1099
|
+
(/^(it has no mode|mode ")/.test(problem ?? '') &&
|
|
1100
|
+
!['both', 'light-only', 'dark-only'].includes(data?.mode)) ||
|
|
1101
|
+
(/^base "/.test(problem ?? '') &&
|
|
1102
|
+
data?.like != null &&
|
|
1103
|
+
!LIKE.includes(data.like));
|
|
1104
|
+
const bad = problem && !fieldBroken && !listedTwice ? badSetting(data) : null;
|
|
858
1105
|
if (problem && bad && settingsProblem(data, bad.thing) !== problem) {
|
|
859
|
-
//
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
1106
|
+
// The value it holds, cleaned as typed input is (stray or invisible
|
|
1107
|
+
// spaces, other letters, a pasted `corners: pill`), when the dial takes
|
|
1108
|
+
// that; else a value it takes, for a dial; a color for a color token.
|
|
1109
|
+
const cleaned =
|
|
1110
|
+
DIALS[bad.thing] && typeof bad.value === 'string'
|
|
1111
|
+
? vibrancyMeant(bad.thing, cleanDialValue(bad.thing, bad.value))
|
|
1112
|
+
: null;
|
|
1113
|
+
// Vibrancy saturates the colors the color dials make: with none, set
|
|
1114
|
+
// refuses it as having no effect, so it is not offered.
|
|
1115
|
+
const vibrancyActs =
|
|
1116
|
+
bad.thing !== 'vibrancy' ||
|
|
1117
|
+
(Array.isArray(data.settings) ? data.settings : []).some(
|
|
1118
|
+
(/** @type {any} */ x) =>
|
|
1119
|
+
x && x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
1120
|
+
);
|
|
1121
|
+
const meant =
|
|
1122
|
+
cleaned &&
|
|
1123
|
+
cleaned !== bad.value &&
|
|
1124
|
+
vibrancyActs &&
|
|
1125
|
+
!valueProblem({...bad, value: cleaned})
|
|
1126
|
+
? cleaned
|
|
1127
|
+
: null;
|
|
1128
|
+
// Only the value it holds, cleaned: never a stock value it did not hold.
|
|
1129
|
+
// A vibrancy with no color to saturate is one set refuses: undo only.
|
|
1130
|
+
const example =
|
|
1131
|
+
bad.thing === 'vibrancy' &&
|
|
1132
|
+
!data.settings.some(
|
|
1133
|
+
(/** @type {any} */ x) =>
|
|
1134
|
+
x !== bad && x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
1135
|
+
)
|
|
1136
|
+
? null
|
|
1137
|
+
: meant;
|
|
1138
|
+
// The entry rides on the refusal: a change reads its value as set reads
|
|
1139
|
+
// a typed one, and offers that repair beside the undo (see _edit).
|
|
1140
|
+
throw Object.assign(
|
|
1141
|
+
new AstryxError(
|
|
1142
|
+
`themes/${slug}/${slug}.settings.json: ${problem}, a value the editor no longer accepts. Remove or change that setting; nothing else needs to change. Nothing was changed.`,
|
|
1143
|
+
[
|
|
1144
|
+
{
|
|
1145
|
+
name: `${P} undo ${slug} ${undoArgs(bad)}`,
|
|
1146
|
+
reason: 'do this instead',
|
|
1147
|
+
},
|
|
1148
|
+
// Only a set that replaces this very entry: `mode` and `base` set
|
|
1149
|
+
// the theme's own fields, not an entry in the list.
|
|
1150
|
+
...(example && !['mode', 'base'].includes(bad.thing)
|
|
1151
|
+
? [
|
|
1152
|
+
{
|
|
1153
|
+
name: `${P} set ${slug} ${setArgs(bad, example)}`,
|
|
1154
|
+
reason: meant
|
|
1155
|
+
? 'or set the value it holds, cleaned'
|
|
1156
|
+
: 'or give it a value it takes',
|
|
1157
|
+
},
|
|
1158
|
+
]
|
|
1159
|
+
: []),
|
|
1160
|
+
],
|
|
1161
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
1162
|
+
),
|
|
1163
|
+
{damaged: bad, aboutSaved: true},
|
|
1164
|
+
);
|
|
1165
|
+
}
|
|
1166
|
+
// A bad mode or base is fixed by setting it.
|
|
1167
|
+
// A value written in other letters (Dark-Only, Neutral) is the one meant.
|
|
1168
|
+
const {mode: modeMeant, like: likeMeant} = fieldsMeant(data);
|
|
1169
|
+
const fieldFix = /^(it has no mode|mode ")/.test(problem ?? '')
|
|
1170
|
+
? `${P} set ${slug} mode ${['both', 'light-only', 'dark-only'].includes(modeMeant) ? modeMeant : 'both'}`
|
|
1171
|
+
: /^base "/.test(problem ?? '')
|
|
1172
|
+
? `${P} set ${slug} base ${LIKE.includes(likeMeant) && baseInstalled(cwd, likeMeant) ? likeMeant : (builtOnBase(cwd, slug) ?? 'none')}`
|
|
1173
|
+
: null;
|
|
1174
|
+
// A damaged mode or base and a bad value in the list: setting the field
|
|
1175
|
+
// is refused while the list holds the bad value, but fixing that entry
|
|
1176
|
+
// saves the field as meant too, so its fix comes first. Never a bare
|
|
1177
|
+
// undo, which would throw away the last real change.
|
|
1178
|
+
const alsoBad = fieldFix ? badSetting(data) : null;
|
|
1179
|
+
if (problem && fieldFix && alsoBad) {
|
|
1180
|
+
const cleaned =
|
|
1181
|
+
DIALS[alsoBad.thing] && typeof alsoBad.value === 'string'
|
|
1182
|
+
? vibrancyMeant(
|
|
1183
|
+
alsoBad.thing,
|
|
1184
|
+
cleanDialValue(alsoBad.thing, alsoBad.value),
|
|
1185
|
+
)
|
|
864
1186
|
: null;
|
|
1187
|
+
const meant =
|
|
1188
|
+
cleaned &&
|
|
1189
|
+
cleaned !== alsoBad.value &&
|
|
1190
|
+
!['mode', 'base'].includes(alsoBad.thing) &&
|
|
1191
|
+
!valueProblem({...alsoBad, value: cleaned})
|
|
1192
|
+
? cleaned
|
|
1193
|
+
: null;
|
|
1194
|
+
const field = /^base "/.test(problem) ? 'base' : 'mode';
|
|
1195
|
+
const fieldValue = fieldFix.split(' ').pop();
|
|
865
1196
|
throw new AstryxError(
|
|
866
|
-
`themes/${slug}/${slug}.settings.json: ${problem},
|
|
1197
|
+
`themes/${slug}/${slug}.settings.json: ${problem}, and ${settingsProblem(data, alsoBad.thing) ?? `${alsoBad.thing} "${alsoBad.value}" is not a value it takes`}. Fix that setting first: ${meant ? 'either command below does' : 'the command below does'}, and saves the ${field} as ${fieldValue}. Nothing was changed.`,
|
|
867
1198
|
[
|
|
868
|
-
|
|
869
|
-
...(example
|
|
1199
|
+
...(meant
|
|
870
1200
|
? [
|
|
871
1201
|
{
|
|
872
|
-
name: `${P} set ${slug} ${
|
|
873
|
-
reason: '
|
|
1202
|
+
name: `${P} set ${slug} ${setArgs(alsoBad, meant)}`,
|
|
1203
|
+
reason: 'do this instead: set the value it holds, cleaned',
|
|
874
1204
|
},
|
|
875
1205
|
]
|
|
876
1206
|
: []),
|
|
1207
|
+
{
|
|
1208
|
+
name: `${P} undo ${slug} ${undoArgs(alsoBad)}`,
|
|
1209
|
+
reason: meant ? 'or remove that setting' : 'do this instead',
|
|
1210
|
+
},
|
|
877
1211
|
],
|
|
878
1212
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
879
1213
|
);
|
|
880
1214
|
}
|
|
881
|
-
// A bad mode or base is fixed by setting it.
|
|
882
|
-
// A value written in other letters (Dark-Only, Neutral) is the one meant.
|
|
883
|
-
const modeMeant = String(data?.mode ?? '')
|
|
884
|
-
.trim()
|
|
885
|
-
.toLowerCase();
|
|
886
|
-
const likeMeant = String(data?.like ?? '')
|
|
887
|
-
.trim()
|
|
888
|
-
.toLowerCase();
|
|
889
|
-
const fieldFix = /^(it has no mode|mode ")/.test(problem ?? '')
|
|
890
|
-
? `${P} set ${slug} mode ${['both', 'light-only', 'dark-only'].includes(modeMeant) ? modeMeant : 'both'}`
|
|
891
|
-
: /^base "/.test(problem ?? '')
|
|
892
|
-
? `${P} set ${slug} base ${LIKE.includes(likeMeant) && baseInstalled(cwd, likeMeant) ? likeMeant : 'none'}`
|
|
893
|
-
: null;
|
|
894
1215
|
// A setting listed twice: setting it to one of its values, or undoing
|
|
895
1216
|
// it, keeps one entry or none.
|
|
896
1217
|
const twice = problem?.includes(' is listed twice (')
|
|
@@ -900,7 +1221,7 @@ export function loadTheme(
|
|
|
900
1221
|
throw new AstryxError(
|
|
901
1222
|
`themes/${slug}/${slug}.settings.json: ${problem}. ${fieldFix ? 'Set it again' : twice ? `Set it to one of its values or undo it, or fix it by hand${recover.text}` : `Fix it by hand${recover.text}`}. Nothing was changed.`,
|
|
902
1223
|
fieldFix
|
|
903
|
-
? [{name: fieldFix, reason: 'do this instead'}
|
|
1224
|
+
? [{name: fieldFix, reason: 'do this instead'}]
|
|
904
1225
|
: twice
|
|
905
1226
|
? [
|
|
906
1227
|
...twice.values
|
|
@@ -911,7 +1232,7 @@ export function loadTheme(
|
|
|
911
1232
|
)
|
|
912
1233
|
.slice(0, 2)
|
|
913
1234
|
.map(v => ({
|
|
914
|
-
name: `${P} set ${slug} ${
|
|
1235
|
+
name: `${P} set ${slug} ${setArgs(twice.setting, String(v))}`,
|
|
915
1236
|
reason: `keep ${v}`,
|
|
916
1237
|
})),
|
|
917
1238
|
{
|
|
@@ -926,6 +1247,21 @@ export function loadTheme(
|
|
|
926
1247
|
return data;
|
|
927
1248
|
}
|
|
928
1249
|
|
|
1250
|
+
/**
|
|
1251
|
+
* A command that builds on a built-in theme, as it runs here: after the
|
|
1252
|
+
* line that installs that theme's package when the project does not have
|
|
1253
|
+
* it, so the first line offered always works.
|
|
1254
|
+
* @param {string} cwd
|
|
1255
|
+
* @param {string} base a built-in theme
|
|
1256
|
+
* @param {string} line the command on that base
|
|
1257
|
+
* @returns {string[]}
|
|
1258
|
+
*/
|
|
1259
|
+
export function onBuiltIn(cwd, base, line) {
|
|
1260
|
+
return baseInstalled(cwd, base)
|
|
1261
|
+
? [line]
|
|
1262
|
+
: [installLine(cwd, [`@astryxdesign/theme-${base}`]), line];
|
|
1263
|
+
}
|
|
1264
|
+
|
|
929
1265
|
/**
|
|
930
1266
|
* The first setting listed more than once, with every value it is listed
|
|
931
1267
|
* with, in order.
|
|
@@ -961,9 +1297,11 @@ export function settingsProblem(
|
|
|
961
1297
|
) {
|
|
962
1298
|
if (!data || typeof data !== 'object' || Array.isArray(data))
|
|
963
1299
|
return 'the file is not a settings object';
|
|
964
|
-
//
|
|
1300
|
+
// A command that changes or removes a setting (or sets mode or base)
|
|
1301
|
+
// puts a damaged mode or base back to its default itself, with a note, so
|
|
1302
|
+
// two damaged fields never send each fix to the other.
|
|
965
1303
|
const fixing = typeof skip === 'string' ? skip : skip?.thing;
|
|
966
|
-
const field = fixing
|
|
1304
|
+
const field = Boolean(fixing);
|
|
967
1305
|
if (!field && !['both', 'light-only', 'dark-only'].includes(data.mode))
|
|
968
1306
|
return data.mode == null
|
|
969
1307
|
? 'it has no mode'
|
|
@@ -980,28 +1318,329 @@ export function settingsProblem(
|
|
|
980
1318
|
return `lifted is ${JSON.stringify(data.lifted)}, not a list of text (the build writes it; remove the field and the next build writes it again)`;
|
|
981
1319
|
/** @type {Map<string, any>} */
|
|
982
1320
|
const keys = new Map();
|
|
1321
|
+
/** How many entries each key has. @type {Map<string, number>} */
|
|
1322
|
+
const twins = new Map();
|
|
1323
|
+
for (const s of data.settings)
|
|
1324
|
+
if (s && typeof s.thing === 'string')
|
|
1325
|
+
twins.set(keyOf(s), (twins.get(keyOf(s)) ?? 0) + 1);
|
|
983
1326
|
for (const s of data.settings) {
|
|
984
1327
|
// A value or scope that is not text is a bad setting `undo` removes;
|
|
985
1328
|
// a setting with no name is damage.
|
|
986
1329
|
if (!s || typeof s.thing !== 'string' || !('value' in s))
|
|
987
1330
|
return 'a setting has no thing or value';
|
|
1331
|
+
if (removes(skip) && skips(s, skip) && entryDamage(s)) continue;
|
|
988
1332
|
if (!['dial', 'token', 'property'].includes(s.source))
|
|
989
|
-
return
|
|
1333
|
+
return s.source == null || s.source === ''
|
|
1334
|
+
? `${s.thing} has no source (dial, token or property)`
|
|
1335
|
+
: `${s.thing} has source ${JSON.stringify(s.source)}, not dial, token or property`;
|
|
990
1336
|
if (s.mode != null && s.mode !== 'light' && s.mode !== 'dark')
|
|
991
|
-
return `${s.thing} has mode
|
|
1337
|
+
return `${s.thing} has mode ${JSON.stringify(s.mode)}, not light or dark`;
|
|
1338
|
+
if (s.only != null && s.only !== true)
|
|
1339
|
+
return `${s.thing} has only ${JSON.stringify(s.only)}, not true or false`;
|
|
1340
|
+
if (s.only === true && !onlyFits(s))
|
|
1341
|
+
return `${describe({...s, only: undefined, value: '?'}).replace(' = ?', '')} has only, which is for one member of a group styled alike (${AUTO_SPREAD.join(', ')})`;
|
|
992
1342
|
const key = keyOf(s);
|
|
993
1343
|
// The command that changes or removes a setting listed twice keeps one
|
|
994
|
-
// value, or none, so it fixes the list.
|
|
1344
|
+
// value, or none, so it fixes the list. An entry with a twin is named
|
|
1345
|
+
// as listed twice, even when one value is bad: an undo of that setting
|
|
1346
|
+
// would remove both.
|
|
995
1347
|
if (keys.has(key) && !skips(s, skip))
|
|
996
1348
|
return `${describe({...s, value: '?'}).replace(' = ?', '')} is listed twice (${JSON.stringify(keys.get(key).value)}, ${JSON.stringify(s.value)})`;
|
|
997
1349
|
keys.set(key, s);
|
|
998
|
-
if (skips(s, skip))
|
|
999
|
-
|
|
1000
|
-
|
|
1350
|
+
if (!skips(s, skip) && (twins.get(key) ?? 0) < 2) {
|
|
1351
|
+
const problem = valueProblem(s);
|
|
1352
|
+
if (problem) return problem;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
return null;
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* The built-in base the editor last built a theme on, read from the
|
|
1360
|
+
* generated module's header (`// Built on @astryxdesign/theme-neutral …`),
|
|
1361
|
+
* else from the newest undo step, or null when neither can be read. What a damaged base in
|
|
1362
|
+
* the settings file is put back to, and what a theme made again keeps.
|
|
1363
|
+
* @param {string} cwd
|
|
1364
|
+
* @param {string} slug
|
|
1365
|
+
* @returns {string | null}
|
|
1366
|
+
*/
|
|
1367
|
+
export function builtOnBase(cwd, slug) {
|
|
1368
|
+
const f = themeFiles(cwd, slug);
|
|
1369
|
+
/** @type {string | null | undefined} */
|
|
1370
|
+
let header;
|
|
1371
|
+
try {
|
|
1372
|
+
header = /\/\/ Built on @astryxdesign\/theme-([a-z0-9-]+) /.exec(
|
|
1373
|
+
fs.readFileSync(f.settingsTs, 'utf8'),
|
|
1374
|
+
)?.[1];
|
|
1375
|
+
} catch {
|
|
1376
|
+
header = null;
|
|
1377
|
+
}
|
|
1378
|
+
// With the module gone too, the newest undo step names the base.
|
|
1379
|
+
const steps = readHistory(f.history).steps;
|
|
1380
|
+
const stepped = steps.length
|
|
1381
|
+
? settingsJson(steps[steps.length - 1])?.like
|
|
1382
|
+
: null;
|
|
1383
|
+
return (
|
|
1384
|
+
[header, stepped].find(b => typeof b === 'string' && LIKE.includes(b)) ??
|
|
1385
|
+
null
|
|
1386
|
+
);
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
/**
|
|
1390
|
+
* The `new` that makes a theme again in place, on the base it was built on
|
|
1391
|
+
* as far as can still be read: the settings file's base (in any case), else
|
|
1392
|
+
* the base the generated module was built on. With none, it says the theme
|
|
1393
|
+
* starts on no base.
|
|
1394
|
+
* @param {string} cwd
|
|
1395
|
+
* @param {string} slug
|
|
1396
|
+
* @returns {{lines: string[], base: string | null}}
|
|
1397
|
+
*/
|
|
1398
|
+
export function remakeLines(cwd, slug) {
|
|
1399
|
+
const f = themeFiles(cwd, slug);
|
|
1400
|
+
const read = (/** @type {string} */ file) => {
|
|
1401
|
+
try {
|
|
1402
|
+
return fs.readFileSync(file, 'utf8');
|
|
1403
|
+
} catch {
|
|
1404
|
+
return '';
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
const saved = settingsJson(read(f.settings));
|
|
1408
|
+
const typed =
|
|
1409
|
+
saved && typeof saved.like === 'string'
|
|
1410
|
+
? dialWordOf('base', saved.like)
|
|
1411
|
+
: null;
|
|
1412
|
+
const base =
|
|
1413
|
+
[typed, builtOnBase(cwd, slug)].find(b => b && LIKE.includes(b)) ?? null;
|
|
1414
|
+
return {
|
|
1415
|
+
base,
|
|
1416
|
+
lines: base
|
|
1417
|
+
? onBuiltIn(cwd, base, `${P} new ${slug} --like ${base}`)
|
|
1418
|
+
: [`${P} new ${slug}`],
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* A dial's own word typed in any case (ROOMY, MORE, Both, NONE): the word,
|
|
1424
|
+
* or null when the value is no word of that dial. The one case rule for
|
|
1425
|
+
* every dial, typed or saved. On the font dial several words are a family
|
|
1426
|
+
* list, whose names keep their letters; one word is the preset.
|
|
1427
|
+
* @param {string} thing
|
|
1428
|
+
* @param {string} value
|
|
1429
|
+
* @returns {string | null}
|
|
1430
|
+
*/
|
|
1431
|
+
export function dialWordOf(thing, value) {
|
|
1432
|
+
if (!DIALS[thing]) return null;
|
|
1433
|
+
const words = new Set(
|
|
1434
|
+
String(DIALS[thing].values ?? '')
|
|
1435
|
+
.split('|')
|
|
1436
|
+
.filter(w => /^[a-z][a-z-]*$/.test(w)),
|
|
1437
|
+
);
|
|
1438
|
+
const lower = value.trim().toLowerCase();
|
|
1439
|
+
const read = thing === 'font' ? [lower] : lower.split(/\s+/);
|
|
1440
|
+
return lower && read.every(w => words.has(w)) ? lower : null;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/**
|
|
1444
|
+
* Read each entry's mode and source as the editor writes them: a mode in
|
|
1445
|
+
* other letters or words (`Dark`, `night`, `day`) as light or dark, a
|
|
1446
|
+
* source in other letters (`Dial`) as written, and a missing source from
|
|
1447
|
+
* the thing (a dial name is a dial, a `--` name a token, anything with a
|
|
1448
|
+
* part a property). Each change is kept as a note for the next command,
|
|
1449
|
+
* which saves the entry that way. A value that means nothing is left for
|
|
1450
|
+
* the check to name.
|
|
1451
|
+
* @param {any} data
|
|
1452
|
+
*/
|
|
1453
|
+
function readEntryWords(data) {
|
|
1454
|
+
/** @type {string[]} */
|
|
1455
|
+
const notes = [];
|
|
1456
|
+
for (const s of Array.isArray(data?.settings) ? data.settings : []) {
|
|
1457
|
+
if (!s || typeof s !== 'object' || typeof s.thing !== 'string') continue;
|
|
1458
|
+
if (typeof s.mode === 'string' && s.mode !== 'light' && s.mode !== 'dark') {
|
|
1459
|
+
const read = MODE_WORDS[s.mode.trim().toLowerCase()];
|
|
1460
|
+
if (read) {
|
|
1461
|
+
notes.push(
|
|
1462
|
+
`${s.thing} was saved with mode ${JSON.stringify(s.mode)}, read as ${read}`,
|
|
1463
|
+
);
|
|
1464
|
+
s.mode = read;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
// A part saved in other letters, with spaces around it, or as a list
|
|
1468
|
+
// of that one part (`Card`, ` card`, `["card"]`) is that part: part
|
|
1469
|
+
// names are lower case, so every reader matches it as one scope.
|
|
1470
|
+
{
|
|
1471
|
+
const one =
|
|
1472
|
+
Array.isArray(s.on) && s.on.length === 1 && typeof s.on[0] === 'string'
|
|
1473
|
+
? s.on[0]
|
|
1474
|
+
: s.on;
|
|
1475
|
+
const part =
|
|
1476
|
+
typeof one === 'string' && /^\s*[a-z][a-z0-9-]*\s*$/i.test(one)
|
|
1477
|
+
? one.trim().toLowerCase()
|
|
1478
|
+
: null;
|
|
1479
|
+
if (part && part !== s.on) {
|
|
1480
|
+
notes.push(
|
|
1481
|
+
`${s.thing} was saved on ${JSON.stringify(s.on)}, read as ${part}`,
|
|
1482
|
+
);
|
|
1483
|
+
s.on = part;
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
// A dial's word saved in other letters (`Pill`) is that word.
|
|
1487
|
+
if (
|
|
1488
|
+
['dial', undefined, null, ''].includes(s.source) ||
|
|
1489
|
+
String(s.source).toLowerCase() === 'dial'
|
|
1490
|
+
) {
|
|
1491
|
+
const word =
|
|
1492
|
+
typeof s.value === 'string' ? dialWordOf(s.thing, s.value) : null;
|
|
1493
|
+
if (word && word !== s.value) {
|
|
1494
|
+
notes.push(
|
|
1495
|
+
`${s.thing} was saved as ${JSON.stringify(s.value)}, read as ${word}`,
|
|
1496
|
+
);
|
|
1497
|
+
s.value = word;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
// A headings word saved under the other half (or a tracking word with
|
|
1501
|
+
// no half) is filed under the half the word belongs to.
|
|
1502
|
+
if (
|
|
1503
|
+
s.thing === 'headings' &&
|
|
1504
|
+
typeof s.value === 'string' &&
|
|
1505
|
+
(s.axis == null || s.axis === 'weight' || s.axis === 'tracking')
|
|
1506
|
+
) {
|
|
1507
|
+
const half = Object.hasOwn(HEADING_TRACKING, s.value)
|
|
1508
|
+
? 'tracking'
|
|
1509
|
+
: Object.hasOwn(HEADING_WEIGHTS, s.value)
|
|
1510
|
+
? 'weight'
|
|
1511
|
+
: null;
|
|
1512
|
+
if (half && (s.axis ?? 'weight') !== half) {
|
|
1513
|
+
notes.push(
|
|
1514
|
+
`headings "${s.value}" was saved under ${s.axis ?? 'weight'}, read as ${half}`,
|
|
1515
|
+
);
|
|
1516
|
+
if (half === 'tracking') s.axis = 'tracking';
|
|
1517
|
+
else delete s.axis;
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
// `only` is true or absent; a yes or no written as text (`"true"`,
|
|
1521
|
+
// `"yes"`, `"off"`) is read as what it says by the one yes/no reader,
|
|
1522
|
+
// since the build reads any other value as true.
|
|
1523
|
+
if (typeof s.only === 'string' && booleanWord(s.only) != null) {
|
|
1524
|
+
const read = booleanWord(s.only) === true;
|
|
1525
|
+
notes.push(
|
|
1526
|
+
`${s.thing} was saved with only ${JSON.stringify(s.only)}, read as ${read}`,
|
|
1527
|
+
);
|
|
1528
|
+
if (read) s.only = true;
|
|
1529
|
+
else delete s.only;
|
|
1530
|
+
} else if (s.only === false) delete s.only;
|
|
1531
|
+
if (!['dial', 'token', 'property'].includes(s.source)) {
|
|
1532
|
+
const typed = typeof s.source === 'string' ? s.source.trim() : '';
|
|
1533
|
+
const read = typed
|
|
1534
|
+
? ['dial', 'token', 'property'].find(x => x === typed.toLowerCase())
|
|
1535
|
+
: s.source == null || s.source === ''
|
|
1536
|
+
? DIALS[s.thing]
|
|
1537
|
+
? 'dial'
|
|
1538
|
+
: s.thing.startsWith('--')
|
|
1539
|
+
? 'token'
|
|
1540
|
+
: s.on
|
|
1541
|
+
? 'property'
|
|
1542
|
+
: undefined
|
|
1543
|
+
: undefined;
|
|
1544
|
+
if (read) {
|
|
1545
|
+
notes.push(
|
|
1546
|
+
typed
|
|
1547
|
+
? `${s.thing} was saved with source ${JSON.stringify(s.source)}, read as ${read}`
|
|
1548
|
+
: `${s.thing} was saved with no source, read as ${read}`,
|
|
1549
|
+
);
|
|
1550
|
+
s.source = read;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1001
1553
|
}
|
|
1554
|
+
ENTRY_NOTES.set(data, notes);
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/** @type {WeakMap<object, string[]>} */
|
|
1558
|
+
const ENTRY_NOTES = new WeakMap();
|
|
1559
|
+
|
|
1560
|
+
/**
|
|
1561
|
+
* What loading read differently from the file in the theme's entries (a
|
|
1562
|
+
* mode or source in other letters, a missing source): one note each.
|
|
1563
|
+
* @param {object} data the theme as {@link loadTheme} returned it
|
|
1564
|
+
* @returns {string[]}
|
|
1565
|
+
*/
|
|
1566
|
+
export function entryNotes(data) {
|
|
1567
|
+
return ENTRY_NOTES.get(data) ?? [];
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Which part of an entry means nothing after reading: its source, mode or
|
|
1572
|
+
* `only`, or null.
|
|
1573
|
+
* @param {any} s
|
|
1574
|
+
* @returns {'mode' | 'source' | 'only' | null}
|
|
1575
|
+
*/
|
|
1576
|
+
export function entryDamage(s) {
|
|
1577
|
+
if (!s || typeof s.thing !== 'string') return null;
|
|
1578
|
+
if (!['dial', 'token', 'property'].includes(s.source)) return 'source';
|
|
1579
|
+
if (s.mode != null && s.mode !== 'light' && s.mode !== 'dark') return 'mode';
|
|
1580
|
+
if (s.only != null && (s.only !== true || !onlyFits(s))) return 'only';
|
|
1002
1581
|
return null;
|
|
1003
1582
|
}
|
|
1004
1583
|
|
|
1584
|
+
/**
|
|
1585
|
+
* Whether `only` fits a setting: it names one member of a group styled
|
|
1586
|
+
* alike (a text input of inputs), as `set --only` takes it.
|
|
1587
|
+
* @param {any} s
|
|
1588
|
+
*/
|
|
1589
|
+
function onlyFits(s) {
|
|
1590
|
+
const fam = typeof s.on === 'string' ? familyOf(s.on) : null;
|
|
1591
|
+
return Boolean(
|
|
1592
|
+
fam && !FAMILIES[s.on] && AUTO_SPREAD.includes(/** @type {string} */ (fam)),
|
|
1593
|
+
);
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* The entry as an undo names it: without the mode or `only` that means
|
|
1598
|
+
* nothing (an undo without them removes that entry).
|
|
1599
|
+
* @param {any} s
|
|
1600
|
+
*/
|
|
1601
|
+
function damagedUndo(s) {
|
|
1602
|
+
const damage = entryDamage(s);
|
|
1603
|
+
return {
|
|
1604
|
+
...s,
|
|
1605
|
+
mode: damage === 'mode' ? undefined : s.mode,
|
|
1606
|
+
only: damage === 'only' ? undefined : s.only,
|
|
1607
|
+
};
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
/**
|
|
1611
|
+
* The entry a settings problem names when that problem is its mode or
|
|
1612
|
+
* `only`, or null.
|
|
1613
|
+
* @param {any} data
|
|
1614
|
+
* @param {string} problem
|
|
1615
|
+
*/
|
|
1616
|
+
function damagedEntry(data, problem) {
|
|
1617
|
+
return (
|
|
1618
|
+
(Array.isArray(data?.settings) ? data.settings : []).find(
|
|
1619
|
+
(/** @type {any} */ s) =>
|
|
1620
|
+
(entryDamage(s) === 'source' &&
|
|
1621
|
+
typeof s.source === 'string' &&
|
|
1622
|
+
s.source !== '' &&
|
|
1623
|
+
problem.startsWith(`${s.thing} has source `)) ||
|
|
1624
|
+
(entryDamage(s) === 'mode' &&
|
|
1625
|
+
problem.startsWith(`${s.thing} has mode `)) ||
|
|
1626
|
+
(entryDamage(s) === 'only' &&
|
|
1627
|
+
(problem.startsWith(`${s.thing} has only `) ||
|
|
1628
|
+
problem.includes(' has only, which is for one member'))),
|
|
1629
|
+
) ?? null
|
|
1630
|
+
);
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/**
|
|
1634
|
+
* Whether the command removes the setting it names (an undo): only that
|
|
1635
|
+
* takes an entry whose mode or source means nothing.
|
|
1636
|
+
* @param {string | Partial<Setting> | undefined} skip
|
|
1637
|
+
*/
|
|
1638
|
+
function removes(skip) {
|
|
1639
|
+
return Boolean(
|
|
1640
|
+
skip && typeof skip === 'object' && /** @type {any} */ (skip).removing,
|
|
1641
|
+
);
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1005
1644
|
/**
|
|
1006
1645
|
* Whether a setting is the one a command changes or removes: by thing, or
|
|
1007
1646
|
* by the whole identity (part, variant, state, mode) when one is given.
|
|
@@ -1016,7 +1655,8 @@ function skips(s, skip) {
|
|
|
1016
1655
|
(s.on ?? '') === (skip.on ?? '') &&
|
|
1017
1656
|
(s.when ?? '') === (skip.when ?? '') &&
|
|
1018
1657
|
(s.state ?? '') === (skip.state ?? '') &&
|
|
1019
|
-
(s.mode ?? '') === (skip.mode ?? '')
|
|
1658
|
+
((s.mode ?? '') === (skip.mode ?? '') ||
|
|
1659
|
+
(removes(skip) && !skip.mode && entryDamage(s) === 'mode'))
|
|
1020
1660
|
);
|
|
1021
1661
|
}
|
|
1022
1662
|
|
|
@@ -1040,12 +1680,41 @@ export function badSettings(data) {
|
|
|
1040
1680
|
export function undoArgs(x) {
|
|
1041
1681
|
return [
|
|
1042
1682
|
quoteCommandArgument(String(x.thing)),
|
|
1683
|
+
// The tracking half of headings is named, so the undo reaches it even
|
|
1684
|
+
// when it is listed twice (a bare `undo headings` then reads the weight
|
|
1685
|
+
// half).
|
|
1686
|
+
...(x.thing === 'headings' && x.axis === 'tracking' ? ['tracking'] : []),
|
|
1687
|
+
...scopeFlags(x),
|
|
1688
|
+
].join(' ');
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* The words of a `set` that gives a setting's scope a value: set reads a
|
|
1693
|
+
* headings half from its word, so no half is named.
|
|
1694
|
+
* @param {import('./_settings.mjs').Setting} x
|
|
1695
|
+
* @param {string} value
|
|
1696
|
+
*/
|
|
1697
|
+
export function setArgs(x, value) {
|
|
1698
|
+
return [
|
|
1699
|
+
quoteCommandArgument(String(x.thing)),
|
|
1700
|
+
quoteCommandArgument(value),
|
|
1701
|
+
...scopeFlags(x),
|
|
1702
|
+
].join(' ');
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
/**
|
|
1706
|
+
* The flags that name a setting's scope, as set and undo take them.
|
|
1707
|
+
* @param {import('./_settings.mjs').Setting} x
|
|
1708
|
+
* @returns {string[]}
|
|
1709
|
+
*/
|
|
1710
|
+
function scopeFlags(x) {
|
|
1711
|
+
return [
|
|
1043
1712
|
...(x.on ? [`--on ${quoteCommandArgument(String(x.on))}`] : []),
|
|
1044
1713
|
...(x.when ? [`--when ${quoteCommandArgument(String(x.when))}`] : []),
|
|
1045
1714
|
...(x.state ? [`--state ${quoteCommandArgument(String(x.state))}`] : []),
|
|
1046
1715
|
...(x.mode ? [`--mode ${quoteCommandArgument(String(x.mode))}`] : []),
|
|
1047
1716
|
...(x.only ? ['--only'] : []),
|
|
1048
|
-
]
|
|
1717
|
+
];
|
|
1049
1718
|
}
|
|
1050
1719
|
|
|
1051
1720
|
/**
|
|
@@ -1111,6 +1780,17 @@ export function valueProblem(s) {
|
|
|
1111
1780
|
const seed = colorProblem(s.value);
|
|
1112
1781
|
if (seed) return `${bad} ${seed.message}`;
|
|
1113
1782
|
}
|
|
1783
|
+
// `axis` names the half of headings a value sets; no other dial has
|
|
1784
|
+
// one, and each half takes only its own words.
|
|
1785
|
+
if (s.axis != null && s.thing !== 'headings')
|
|
1786
|
+
return `${s.thing} has an axis (${JSON.stringify(s.axis)}), which only headings takes`;
|
|
1787
|
+
if (s.thing === 'headings') {
|
|
1788
|
+
if (s.axis != null && s.axis !== 'weight' && s.axis !== 'tracking')
|
|
1789
|
+
return `headings has axis ${JSON.stringify(s.axis)}, not weight or tracking`;
|
|
1790
|
+
const half = s.axis === 'tracking' ? HEADING_TRACKING : HEADING_WEIGHTS;
|
|
1791
|
+
if (!Object.hasOwn(half, s.value))
|
|
1792
|
+
return `${bad} is not a ${s.axis ?? 'weight'} headings takes (${Object.keys(half).join('|')})`;
|
|
1793
|
+
}
|
|
1114
1794
|
const words = d.values.split('|').filter(w => !w.startsWith('<'));
|
|
1115
1795
|
const ok =
|
|
1116
1796
|
s.thing === 'corners'
|
|
@@ -1133,6 +1813,33 @@ export function valueProblem(s) {
|
|
|
1133
1813
|
return null;
|
|
1134
1814
|
}
|
|
1135
1815
|
|
|
1816
|
+
/**
|
|
1817
|
+
* One entry of the top-level `tokens` map in a theme's own source, as
|
|
1818
|
+
* written: its token (null for a computed key no read of the file can
|
|
1819
|
+
* resolve), the key as typed, the line that sets it, and its value's shape.
|
|
1820
|
+
* A `scope` entry names where a scope (`components`, `onDark`, `onLight`,
|
|
1821
|
+
* `adaptations`) starts; a `scoped` entry is a token one sets, with its
|
|
1822
|
+
* scope path (`components.button.base`). A `duplicate` entry names a
|
|
1823
|
+
* second `tokens` key (with every line); a
|
|
1824
|
+
* `typography` entry is one typography field (its dotted key and value);
|
|
1825
|
+
* an `unresolved` entry says the map holds something only a build reads.
|
|
1826
|
+
* @typedef {{token: string | null, key: string, line: number | null, kind: 'text' | 'number' | 'pair' | 'object' | 'other' | 'duplicate' | 'typography' | 'unresolved' | 'scope' | 'scoped', scope?: string, value?: any, halves?: Array<string | number | undefined>, lines?: Array<number | null>, file?: string, overridden?: boolean, expression?: boolean, notToken?: boolean}} OwnTokenEntry
|
|
1827
|
+
*/
|
|
1828
|
+
|
|
1829
|
+
/**
|
|
1830
|
+
* The entries of the top-level `tokens` map in a theme's own source, read
|
|
1831
|
+
* from its syntax tree, each with the line that sets it.
|
|
1832
|
+
* @param {string} cwd
|
|
1833
|
+
* @param {string} slug
|
|
1834
|
+
* @returns {OwnTokenEntry[]}
|
|
1835
|
+
*/
|
|
1836
|
+
export function ownTokenEntries(cwd, slug) {
|
|
1837
|
+
/** @type {OwnTokenEntry[]} */
|
|
1838
|
+
const detail = [];
|
|
1839
|
+
ownPageTokens(cwd, slug, detail);
|
|
1840
|
+
return detail;
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1136
1843
|
/**
|
|
1137
1844
|
* The page-wide tokens a theme's own source sets: the keys of the top-level
|
|
1138
1845
|
* `tokens` map in its `defineTheme({...})`, read from the syntax tree, each
|
|
@@ -1142,7 +1849,11 @@ export function valueProblem(s) {
|
|
|
1142
1849
|
* @param {string} slug
|
|
1143
1850
|
* @returns {Map<string, string | null>}
|
|
1144
1851
|
*/
|
|
1145
|
-
function ownPageTokens(
|
|
1852
|
+
function ownPageTokens(
|
|
1853
|
+
cwd,
|
|
1854
|
+
slug,
|
|
1855
|
+
detail = /** @type {OwnTokenEntry[] | null} */ (null),
|
|
1856
|
+
) {
|
|
1146
1857
|
/** @type {Map<string, string | null>} */
|
|
1147
1858
|
const out = new Map();
|
|
1148
1859
|
/** @type {string} */
|
|
@@ -1243,32 +1954,327 @@ function ownPageTokens(cwd, slug) {
|
|
|
1243
1954
|
if (!chosen) return out;
|
|
1244
1955
|
const def = unwrap(chosen.call.arguments[0]);
|
|
1245
1956
|
if (def?.type !== 'ObjectExpression') return out;
|
|
1246
|
-
|
|
1957
|
+
// JavaScript keeps the last of two `tokens` keys: that one is read, and
|
|
1958
|
+
// both are named.
|
|
1959
|
+
const tokensProps = def.properties.filter(
|
|
1247
1960
|
(/** @type {any} */ p) =>
|
|
1248
1961
|
p.type === 'ObjectProperty' && !p.computed && keyOf(p.key) === 'tokens',
|
|
1249
1962
|
);
|
|
1250
|
-
if (
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1963
|
+
if (detail && tokensProps.length > 1)
|
|
1964
|
+
detail.push({
|
|
1965
|
+
token: null,
|
|
1966
|
+
key: 'tokens',
|
|
1967
|
+
kind: 'duplicate',
|
|
1968
|
+
line: tokensProps[0].loc?.start?.line ?? null,
|
|
1969
|
+
lines: tokensProps.map((/** @type {any} */ p) => p.loc?.start?.line),
|
|
1970
|
+
});
|
|
1971
|
+
// Tokens a scope sets (a part's blocks in `components`, `onDark`,
|
|
1972
|
+
// `onLight`, an adaptation rule), each with its scope path and line.
|
|
1973
|
+
if (detail)
|
|
1974
|
+
for (const p of def.properties) {
|
|
1975
|
+
const scope =
|
|
1976
|
+
p.type === 'ObjectProperty' && !p.computed ? keyOf(p.key) : null;
|
|
1977
|
+
if (
|
|
1978
|
+
!scope ||
|
|
1979
|
+
!['components', 'onDark', 'onLight', 'adaptations'].includes(scope)
|
|
1980
|
+
)
|
|
1981
|
+
continue;
|
|
1982
|
+
detail.push({
|
|
1983
|
+
token: null,
|
|
1984
|
+
key: scope,
|
|
1985
|
+
kind: 'scope',
|
|
1986
|
+
line: p.loc?.start?.line ?? null,
|
|
1987
|
+
});
|
|
1988
|
+
/** @param {any} node @param {string[]} at @param {number} depth */
|
|
1989
|
+
const walk = (node, at, depth) => {
|
|
1990
|
+
const obj = unwrap(node);
|
|
1991
|
+
if (depth > 12) return;
|
|
1992
|
+
if (obj?.type === 'ArrayExpression') {
|
|
1993
|
+
obj.elements.forEach(
|
|
1994
|
+
(/** @type {any} */ el, /** @type {number} */ i) =>
|
|
1995
|
+
walk(el, [...at, String(i)], depth + 1),
|
|
1996
|
+
);
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
if (obj?.type !== 'ObjectExpression') return;
|
|
2000
|
+
for (const q of obj.properties) {
|
|
2001
|
+
if (q.type !== 'ObjectProperty') continue;
|
|
2002
|
+
const k = q.computed ? textOf(unwrap(q.key)) : keyOf(q.key);
|
|
2003
|
+
if (!k) continue;
|
|
2004
|
+
const leaf = unwrap(q.value);
|
|
2005
|
+
// A token, or a part's property (its value no block), by its
|
|
2006
|
+
// scope path and line.
|
|
2007
|
+
if (
|
|
2008
|
+
k.startsWith('--') ||
|
|
2009
|
+
(leaf?.type !== 'ObjectExpression' &&
|
|
2010
|
+
leaf?.type !== 'ArrayExpression')
|
|
2011
|
+
)
|
|
2012
|
+
detail.push({
|
|
2013
|
+
token: k,
|
|
2014
|
+
key: k,
|
|
2015
|
+
kind: 'scoped',
|
|
2016
|
+
scope: at.join('.'),
|
|
2017
|
+
line: q.loc?.start?.line ?? null,
|
|
2018
|
+
});
|
|
2019
|
+
else walk(q.value, [...at, k], depth + 1);
|
|
2020
|
+
}
|
|
2021
|
+
};
|
|
2022
|
+
walk(p.value, [scope], 0);
|
|
2023
|
+
}
|
|
2024
|
+
// The typography fields, as written, for their own checks.
|
|
2025
|
+
const typo = def.properties.find(
|
|
2026
|
+
(/** @type {any} */ p) =>
|
|
2027
|
+
p.type === 'ObjectProperty' &&
|
|
2028
|
+
!p.computed &&
|
|
2029
|
+
keyOf(p.key) === 'typography',
|
|
2030
|
+
);
|
|
2031
|
+
if (detail && typo) {
|
|
2032
|
+
/** @param {any} node @param {string[]} at */
|
|
2033
|
+
const walk = (node, at) => {
|
|
2034
|
+
const obj = unwrap(node);
|
|
2035
|
+
if (obj?.type !== 'ObjectExpression') return;
|
|
2036
|
+
for (const p of obj.properties) {
|
|
2037
|
+
if (p.type !== 'ObjectProperty' || p.computed) continue;
|
|
2038
|
+
const k = keyOf(p.key);
|
|
2039
|
+
if (!k) continue;
|
|
2040
|
+
const v = unwrap(p.value);
|
|
2041
|
+
if (v?.type === 'ObjectExpression') walk(v, [...at, k]);
|
|
2042
|
+
else
|
|
2043
|
+
detail.push({
|
|
2044
|
+
token: null,
|
|
2045
|
+
key: [...at, k].join('.'),
|
|
2046
|
+
kind: 'typography',
|
|
2047
|
+
line: p.loc?.start?.line ?? null,
|
|
2048
|
+
expression:
|
|
2049
|
+
textOf(v) == null &&
|
|
2050
|
+
v?.type !== 'NumericLiteral' &&
|
|
2051
|
+
!(
|
|
2052
|
+
v?.type === 'UnaryExpression' &&
|
|
2053
|
+
v.argument?.type === 'NumericLiteral'
|
|
2054
|
+
),
|
|
2055
|
+
value:
|
|
2056
|
+
textOf(v) ??
|
|
2057
|
+
(v?.type === 'NumericLiteral'
|
|
2058
|
+
? v.value
|
|
2059
|
+
: v?.type === 'UnaryExpression' &&
|
|
2060
|
+
v.operator === '-' &&
|
|
2061
|
+
v.argument?.type === 'NumericLiteral'
|
|
2062
|
+
? -v.argument.value
|
|
2063
|
+
: undefined),
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
walk(typo.value, []);
|
|
2068
|
+
}
|
|
2069
|
+
const tokensProp = tokensProps[tokensProps.length - 1];
|
|
2070
|
+
if (!tokensProp) return out;
|
|
2071
|
+
let unresolved = false;
|
|
2072
|
+
const dir = path.dirname(themeFiles(cwd, slug).ts);
|
|
2073
|
+
/**
|
|
2074
|
+
* The object a name imported from a file beside the theme gives (`import
|
|
2075
|
+
* {mine} from './mine'`, a default import of a .json file), read from
|
|
2076
|
+
* that file, with its own code and path; null for any other name.
|
|
2077
|
+
* @param {string} name
|
|
2078
|
+
* @returns {{obj: any, code: string, file: string, unwrap: (n: any) => any} | null}
|
|
2079
|
+
*/
|
|
2080
|
+
const imported = name => {
|
|
2081
|
+
for (const st of ast.program.body) {
|
|
2082
|
+
if (st.type !== 'ImportDeclaration') continue;
|
|
2083
|
+
const spec = st.specifiers.find(
|
|
2084
|
+
(/** @type {any} */ x) => x.local?.name === name,
|
|
2085
|
+
);
|
|
2086
|
+
const from = String(st.source?.value ?? '');
|
|
2087
|
+
if (!spec || !from.startsWith('.')) continue;
|
|
2088
|
+
const base = path.resolve(dir, from);
|
|
2089
|
+
const file = [
|
|
2090
|
+
'',
|
|
2091
|
+
'.ts',
|
|
2092
|
+
'.mts',
|
|
2093
|
+
'.tsx',
|
|
2094
|
+
'.js',
|
|
2095
|
+
'.mjs',
|
|
2096
|
+
'.json',
|
|
2097
|
+
'/index.ts',
|
|
2098
|
+
]
|
|
2099
|
+
.map(x => base + x)
|
|
2100
|
+
.find(f => {
|
|
2101
|
+
try {
|
|
2102
|
+
return fs.statSync(f).isFile();
|
|
2103
|
+
} catch {
|
|
2104
|
+
return false;
|
|
2105
|
+
}
|
|
2106
|
+
});
|
|
2107
|
+
if (!file) return null;
|
|
2108
|
+
try {
|
|
2109
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
2110
|
+
const json = file.endsWith('.json');
|
|
2111
|
+
const source = json ? `export default ${text}` : text;
|
|
2112
|
+
const other = /** @type {any} */ (
|
|
2113
|
+
parseModule(source, {sourceType: 'module', plugins: ['typescript']})
|
|
2114
|
+
);
|
|
2115
|
+
/** @type {Map<string, any>} */
|
|
2116
|
+
const local = new Map();
|
|
2117
|
+
/** @type {any} */
|
|
2118
|
+
let found = null;
|
|
2119
|
+
const want =
|
|
2120
|
+
spec.type === 'ImportDefaultSpecifier'
|
|
2121
|
+
? 'default'
|
|
2122
|
+
: (spec.imported?.name ?? spec.imported?.value);
|
|
2123
|
+
for (const top of other.program.body) {
|
|
2124
|
+
const d =
|
|
2125
|
+
top.type === 'ExportNamedDeclaration' ? top.declaration : top;
|
|
2126
|
+
for (const v of d?.declarations ?? [])
|
|
2127
|
+
if (v.id?.type === 'Identifier') {
|
|
2128
|
+
local.set(v.id.name, v.init);
|
|
2129
|
+
if (top.type === 'ExportNamedDeclaration' && v.id.name === want)
|
|
2130
|
+
found = v.init;
|
|
2131
|
+
}
|
|
2132
|
+
if (top.type === 'ExportDefaultDeclaration' && want === 'default')
|
|
2133
|
+
found = top.declaration;
|
|
2134
|
+
}
|
|
2135
|
+
/** @param {any} n @param {number} [k] @returns {any} */
|
|
2136
|
+
const unwrapIn = (n, k = 0) => {
|
|
2137
|
+
if (!n || k > 20) return null;
|
|
2138
|
+
if (
|
|
2139
|
+
[
|
|
2140
|
+
'TSAsExpression',
|
|
2141
|
+
'TSSatisfiesExpression',
|
|
2142
|
+
'TSNonNullExpression',
|
|
2143
|
+
'ParenthesizedExpression',
|
|
2144
|
+
].includes(n.type)
|
|
2145
|
+
)
|
|
2146
|
+
return unwrapIn(n.expression, k + 1);
|
|
2147
|
+
if (n.type === 'Identifier' && local.has(n.name))
|
|
2148
|
+
return unwrapIn(local.get(n.name), k + 1);
|
|
2149
|
+
return n;
|
|
2150
|
+
};
|
|
2151
|
+
return found
|
|
2152
|
+
? {obj: unwrapIn(found), code: source, file, unwrap: unwrapIn}
|
|
2153
|
+
: null;
|
|
2154
|
+
} catch {
|
|
2155
|
+
return null;
|
|
2156
|
+
}
|
|
2157
|
+
}
|
|
2158
|
+
return null;
|
|
2159
|
+
};
|
|
2160
|
+
/**
|
|
2161
|
+
* @param {any} node
|
|
2162
|
+
* @param {number} depth
|
|
2163
|
+
* @param {{code: string, file: string | null, unwrap: (n: any) => any}} [at]
|
|
2164
|
+
* the file the node is in (null for the theme file)
|
|
2165
|
+
*/
|
|
2166
|
+
const collect = (node, depth, at = {code, file: null, unwrap}) => {
|
|
2167
|
+
const obj = at.unwrap(node);
|
|
2168
|
+
// A name imported from a file beside the theme: read in that file, so
|
|
2169
|
+
// its values and lines are its own; the build still judges the result.
|
|
2170
|
+
if (obj?.type === 'Identifier' && at.file == null) {
|
|
2171
|
+
const from = imported(obj.name);
|
|
2172
|
+
if (from) {
|
|
2173
|
+
unresolved = true;
|
|
2174
|
+
collect(from.obj, depth + 1, from);
|
|
2175
|
+
return;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
1255
2178
|
if (obj?.type !== 'ObjectExpression' || depth > 20) {
|
|
1256
2179
|
unresolved = true;
|
|
1257
2180
|
return;
|
|
1258
2181
|
}
|
|
1259
|
-
// Spreads and keys merge in order: a later one wins.
|
|
2182
|
+
// Spreads and keys merge in order: a later one wins. A computed key
|
|
2183
|
+
// that is text (`['--radius-page']`) is read like a literal one.
|
|
1260
2184
|
for (const p of obj.properties) {
|
|
1261
|
-
if (p.type === 'SpreadElement') collect(p.argument, depth + 1);
|
|
1262
|
-
else if (p.type === 'ObjectProperty'
|
|
1263
|
-
const k = keyOf(p.key);
|
|
1264
|
-
if (k?.startsWith('--')) out.set(k,
|
|
2185
|
+
if (p.type === 'SpreadElement') collect(p.argument, depth + 1, at);
|
|
2186
|
+
else if (p.type === 'ObjectProperty') {
|
|
2187
|
+
const k = p.computed ? textOf(at.unwrap(p.key)) : keyOf(p.key);
|
|
2188
|
+
if (k?.startsWith('--')) out.set(k, builtText(at.unwrap(p.value)));
|
|
2189
|
+
else if (p.computed) unresolved = true;
|
|
2190
|
+
if (detail && (k || p.computed))
|
|
2191
|
+
detail.push({
|
|
2192
|
+
token: k?.startsWith('--') ? k : null,
|
|
2193
|
+
key: at.code.slice(p.key.start, p.key.end),
|
|
2194
|
+
line: p.loc?.start?.line ?? null,
|
|
2195
|
+
...(at.file ? {file: at.file} : {}),
|
|
2196
|
+
// A key that is no token name (`color`): the build writes it
|
|
2197
|
+
// as a declaration on the theme's root.
|
|
2198
|
+
...(k && !k.startsWith('--') ? {notToken: true} : {}),
|
|
2199
|
+
...valueShape(at.unwrap(p.value), at.unwrap),
|
|
2200
|
+
});
|
|
1265
2201
|
} else unresolved = true;
|
|
1266
2202
|
}
|
|
1267
2203
|
};
|
|
2204
|
+
/**
|
|
2205
|
+
* A token's value as the build writes it, when the file gives it as
|
|
2206
|
+
* text: a [light, dark] pair of two texts is its light-dark(); else
|
|
2207
|
+
* null.
|
|
2208
|
+
* @param {any} node
|
|
2209
|
+
* @returns {string | null}
|
|
2210
|
+
*/
|
|
2211
|
+
const builtText = node => {
|
|
2212
|
+
const halves =
|
|
2213
|
+
node?.type === 'ArrayExpression' && node.elements.length === 2
|
|
2214
|
+
? node.elements.map((/** @type {any} */ e) => textOf(unwrap(e)))
|
|
2215
|
+
: null;
|
|
2216
|
+
return halves && halves.every((/** @type {any} */ h) => h != null)
|
|
2217
|
+
? `light-dark(${halves.join(', ')})`
|
|
2218
|
+
: textOf(node);
|
|
2219
|
+
};
|
|
2220
|
+
/**
|
|
2221
|
+
* What a token's value is, as written: text, a number, a [light, dark]
|
|
2222
|
+
* pair (each half's text, a number, or undefined), or something else.
|
|
2223
|
+
* @param {any} node
|
|
2224
|
+
* @param {(n: any) => any} [unwrapIn] how the node's file reads a name
|
|
2225
|
+
* @returns {Pick<OwnTokenEntry, 'kind' | 'value' | 'halves'>}
|
|
2226
|
+
*/
|
|
2227
|
+
const valueShape = (node, unwrapIn = unwrap) => {
|
|
2228
|
+
const t = textOf(node);
|
|
2229
|
+
if (t != null) return {kind: 'text', value: t};
|
|
2230
|
+
if (node?.type === 'NumericLiteral')
|
|
2231
|
+
return {kind: 'number', value: node.value};
|
|
2232
|
+
if (node?.type === 'ArrayExpression')
|
|
2233
|
+
return {
|
|
2234
|
+
kind: 'pair',
|
|
2235
|
+
halves: node.elements.map((/** @type {any} */ e) => {
|
|
2236
|
+
const h = unwrapIn(e);
|
|
2237
|
+
return (
|
|
2238
|
+
textOf(h) ?? (h?.type === 'NumericLiteral' ? h.value : undefined)
|
|
2239
|
+
);
|
|
2240
|
+
}),
|
|
2241
|
+
};
|
|
2242
|
+
if (node?.type === 'ObjectExpression')
|
|
2243
|
+
return {
|
|
2244
|
+
kind: 'object',
|
|
2245
|
+
value: Object.fromEntries(
|
|
2246
|
+
node.properties
|
|
2247
|
+
.filter((/** @type {any} */ p) => p.type === 'ObjectProperty')
|
|
2248
|
+
.map((/** @type {any} */ p) => [
|
|
2249
|
+
keyOf(p.key) ?? '?',
|
|
2250
|
+
textOf(unwrap(p.value)) ?? null,
|
|
2251
|
+
]),
|
|
2252
|
+
),
|
|
2253
|
+
};
|
|
2254
|
+
return {kind: 'other'};
|
|
2255
|
+
};
|
|
1268
2256
|
collect(tokensProp.value, 0);
|
|
2257
|
+
// Only the last entry for a token is what the build writes: an earlier
|
|
2258
|
+
// one a later key or spread overrides is marked, never judged.
|
|
2259
|
+
if (detail) {
|
|
2260
|
+
/** @type {Map<string, number>} */
|
|
2261
|
+
const last = new Map();
|
|
2262
|
+
detail.forEach((e, i) => {
|
|
2263
|
+
if (e.token) last.set(e.token, i);
|
|
2264
|
+
});
|
|
2265
|
+
detail.forEach((e, i) => {
|
|
2266
|
+
if (e.token && last.get(e.token) !== i) e.overridden = true;
|
|
2267
|
+
});
|
|
2268
|
+
}
|
|
1269
2269
|
// A value no read of the file can resolve (a call, an import) is read
|
|
1270
2270
|
// from the build (ownTokens), and checked again after the next one.
|
|
1271
|
-
|
|
2271
|
+
if (detail && unresolved)
|
|
2272
|
+
detail.push({
|
|
2273
|
+
token: null,
|
|
2274
|
+
key: 'tokens',
|
|
2275
|
+
kind: 'unresolved',
|
|
2276
|
+
line: tokensProp.loc?.start?.line ?? null,
|
|
2277
|
+
});
|
|
1272
2278
|
return out;
|
|
1273
2279
|
}
|
|
1274
2280
|
|
|
@@ -1774,7 +2780,7 @@ export function ownSourceTokenValues(cwd, slug) {
|
|
|
1774
2780
|
function scopeProblem(s) {
|
|
1775
2781
|
if (s.on != null && !/^[a-z][a-z0-9-]*$/.test(s.on))
|
|
1776
2782
|
return `${s.thing} is set on "${s.on}", which is not a part or group name`;
|
|
1777
|
-
if (s.state != null && !/^[a-z][a-
|
|
2783
|
+
if (s.state != null && !/^[a-z][a-zA-Z-]*$/.test(s.state))
|
|
1778
2784
|
return `${s.thing} has state "${s.state}", which is not a state name`;
|
|
1779
2785
|
if (s.when != null && !/^[\w-]+:[\w.-]+$/.test(s.when))
|
|
1780
2786
|
return `${s.thing} has --when "${s.when}", which is not prop:value`;
|
|
@@ -1787,7 +2793,10 @@ function scopeProblem(s) {
|
|
|
1787
2793
|
export const keyOf = s =>
|
|
1788
2794
|
[
|
|
1789
2795
|
s.thing,
|
|
1790
|
-
|
|
2796
|
+
// A headings value saved with no axis is its weight half, as the build
|
|
2797
|
+
// reads it, so a set or undo of the weight replaces it. Only headings
|
|
2798
|
+
// has halves: an axis on any other setting names nothing.
|
|
2799
|
+
s.thing === 'headings' ? (s.axis ?? 'weight') : '',
|
|
1791
2800
|
s.on ?? '',
|
|
1792
2801
|
s.when ?? '',
|
|
1793
2802
|
s.state ?? '',
|
|
@@ -1843,6 +2852,18 @@ export function lengthPx(v) {
|
|
|
1843
2852
|
/** @param {string} v */
|
|
1844
2853
|
export const cornerPx = v =>
|
|
1845
2854
|
CORNERS.find(([n]) => n === v)?.[1] ?? parseFloat(v);
|
|
2855
|
+
/**
|
|
2856
|
+
* The base theme's body text size in px, read back from its built base
|
|
2857
|
+
* size (14 when it writes none).
|
|
2858
|
+
* @param {{light: Record<string, string>}} base
|
|
2859
|
+
*/
|
|
2860
|
+
export function baseTextPx(base) {
|
|
2861
|
+
const v = base?.light?.['--font-size-base'];
|
|
2862
|
+
return /^[\d.]+rem$/.test(v ?? '')
|
|
2863
|
+
? Math.round(Number.parseFloat(String(v)) * 16)
|
|
2864
|
+
: 14;
|
|
2865
|
+
}
|
|
2866
|
+
|
|
1846
2867
|
/** @param {string} v */
|
|
1847
2868
|
export const textPx = v => TEXT_SIZE.find(([n]) => n === v)?.[1] ?? Number(v);
|
|
1848
2869
|
|
|
@@ -1925,8 +2946,7 @@ export function cssFamily(name, spaced = false) {
|
|
|
1925
2946
|
}
|
|
1926
2947
|
|
|
1927
2948
|
/**
|
|
1928
|
-
* What a part reads for a property when the theme does not set it
|
|
1929
|
-
* one-mode component setting keeps the other mode as it was.
|
|
2949
|
+
* What a part reads for a property when the theme does not set it.
|
|
1930
2950
|
* @param {Ctx} ctx
|
|
1931
2951
|
* @param {string} target
|
|
1932
2952
|
* @param {string} prop camelCase or kebab-case
|
|
@@ -1959,6 +2979,44 @@ function partDefault(ctx, target, prop, when, state) {
|
|
|
1959
2979
|
return r ? `var(${r.token})` : null;
|
|
1960
2980
|
}
|
|
1961
2981
|
|
|
2982
|
+
/**
|
|
2983
|
+
* The variants (`variant:<name>`) of a part that each read their own value
|
|
2984
|
+
* of a color property (the button's background, a badge's text), in a
|
|
2985
|
+
* state when one is named and changes it: one rule's other-mode half would
|
|
2986
|
+
* give every one of them one variant's color. Empty when the part reads one
|
|
2987
|
+
* value for all.
|
|
2988
|
+
* @param {Ctx} ctx
|
|
2989
|
+
* @param {string} target
|
|
2990
|
+
* @param {string} prop camelCase or kebab-case
|
|
2991
|
+
* @param {string} [state]
|
|
2992
|
+
* @returns {string[]}
|
|
2993
|
+
*/
|
|
2994
|
+
function variantsReading(ctx, target, prop, state) {
|
|
2995
|
+
const k = kebab(prop);
|
|
2996
|
+
const g = ctx.C.groupOfProp(k === 'background' ? 'background-color' : k);
|
|
2997
|
+
if (!g) return [];
|
|
2998
|
+
const reads = ctx.C.partReads(target, g).filter(x =>
|
|
2999
|
+
x.props.some(
|
|
3000
|
+
p => p === k || (k === 'background' && p === 'background-color'),
|
|
3001
|
+
),
|
|
3002
|
+
);
|
|
3003
|
+
const named = (/** @type {(w: string) => boolean} */ ok) => [
|
|
3004
|
+
...new Set(
|
|
3005
|
+
reads
|
|
3006
|
+
.flatMap(x => x.when)
|
|
3007
|
+
.filter(ok)
|
|
3008
|
+
.map(w => /(?:^|,)variant=([\w-]+)/.exec(w)?.[1])
|
|
3009
|
+
.filter(Boolean)
|
|
3010
|
+
.map(n => `variant:${n}`),
|
|
3011
|
+
),
|
|
3012
|
+
];
|
|
3013
|
+
const inState = state ? named(w => w.includes(`:${state}`)) : [];
|
|
3014
|
+
if (inState.length) return inState;
|
|
3015
|
+
return reads.some(x => x.when.includes(''))
|
|
3016
|
+
? []
|
|
3017
|
+
: named(w => /^variant=[\w-]+$/.test(w));
|
|
3018
|
+
}
|
|
3019
|
+
|
|
1962
3020
|
/** The line that records a generated module's own hash. */
|
|
1963
3021
|
const SEAL = /^\/\/ Module ([0-9a-f]{16})\.\n/m;
|
|
1964
3022
|
|
|
@@ -2101,6 +3159,138 @@ function keepShades(pal, kept) {
|
|
|
2101
3159
|
return {...pal, tokens, lift: kept['lifted parts'] ? pal.lift : null};
|
|
2102
3160
|
}
|
|
2103
3161
|
|
|
3162
|
+
/**
|
|
3163
|
+
* A size in px: px, rem (16px each), or a calc() of those added and
|
|
3164
|
+
* subtracted; null for anything else.
|
|
3165
|
+
* @param {string} v
|
|
3166
|
+
* @param {Record<string, string>} [vars] values for the tokens a var() may name
|
|
3167
|
+
* @returns {number | null}
|
|
3168
|
+
*/
|
|
3169
|
+
export function sizePx(v, vars = {}) {
|
|
3170
|
+
let t = String(v).trim();
|
|
3171
|
+
// A token the dial or the theme gives a value (var(--spacing-12)),
|
|
3172
|
+
// through that value; a clamp() at its preferred value held to its
|
|
3173
|
+
// bounds, or its upper bound when the preferred one is no length;
|
|
3174
|
+
// min() and max() of the lengths they hold.
|
|
3175
|
+
const fn = /^(var|clamp|min|max)\((.*)\)$/is.exec(t);
|
|
3176
|
+
if (fn) {
|
|
3177
|
+
const args = splitTop(fn[2], ',').map(x => x.trim());
|
|
3178
|
+
if (fn[1].toLowerCase() === 'var') {
|
|
3179
|
+
const own = vars[args[0]];
|
|
3180
|
+
if (own != null) return sizePx(own, vars);
|
|
3181
|
+
return args[1] != null ? sizePx(args.slice(1).join(','), vars) : null;
|
|
3182
|
+
}
|
|
3183
|
+
const n = args.map(x => sizePx(x, vars));
|
|
3184
|
+
if (fn[1].toLowerCase() === 'clamp') {
|
|
3185
|
+
const [lo, mid, hi] = n;
|
|
3186
|
+
if (lo == null || hi == null) return null;
|
|
3187
|
+
return mid == null ? hi : Math.min(hi, Math.max(lo, mid));
|
|
3188
|
+
}
|
|
3189
|
+
const known = /** @type {number[]} */ (n.filter(x => x != null));
|
|
3190
|
+
if (!known.length) return null;
|
|
3191
|
+
return fn[1].toLowerCase() === 'min'
|
|
3192
|
+
? Math.min(...known)
|
|
3193
|
+
: Math.max(...known);
|
|
3194
|
+
}
|
|
3195
|
+
const calc = /^calc\((.*)\)$/is.exec(t);
|
|
3196
|
+
if (calc) t = calc[1].trim();
|
|
3197
|
+
const terms =
|
|
3198
|
+
t.match(/[+-]?\s*[\d.]+(?:px|rem|em|pt|pc|in|cm|mm|q)\b|\S+/gi) ?? [];
|
|
3199
|
+
let sum = 0;
|
|
3200
|
+
let sign = 1;
|
|
3201
|
+
for (const raw of terms) {
|
|
3202
|
+
const x = raw.replace(/\s+/g, '');
|
|
3203
|
+
if (x === '+' || x === '-') {
|
|
3204
|
+
if (!calc) return null;
|
|
3205
|
+
sign = x === '-' ? -1 : 1;
|
|
3206
|
+
continue;
|
|
3207
|
+
}
|
|
3208
|
+
const m = /^([+-]?)([\d.]+)(px|rem|em|pt|pc|in|cm|mm|q)$/i.exec(x);
|
|
3209
|
+
if (!m || (!calc && terms.length > 1)) return null;
|
|
3210
|
+
// Absolute units by their fixed ratio to px; em and rem against the
|
|
3211
|
+
// 16px a control's text starts from.
|
|
3212
|
+
const n = parseFloat(m[2]) * (PX_PER[m[3].toLowerCase()] ?? 1);
|
|
3213
|
+
if (!Number.isFinite(n)) return null;
|
|
3214
|
+
sum += (m[1] === '-' ? -1 : 1) * sign * n;
|
|
3215
|
+
sign = 1;
|
|
3216
|
+
}
|
|
3217
|
+
return terms.length ? sum : null;
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
/** Pixels per unit of each length unit a kept size may be written in. */
|
|
3221
|
+
const PX_PER = /** @type {Record<string, number>} */ ({
|
|
3222
|
+
px: 1,
|
|
3223
|
+
rem: 16,
|
|
3224
|
+
em: 16,
|
|
3225
|
+
pt: 96 / 72,
|
|
3226
|
+
pc: 16,
|
|
3227
|
+
in: 96,
|
|
3228
|
+
cm: 96 / 2.54,
|
|
3229
|
+
mm: 96 / 25.4,
|
|
3230
|
+
q: 96 / 101.6,
|
|
3231
|
+
});
|
|
3232
|
+
|
|
3233
|
+
/**
|
|
3234
|
+
* Spacing steps scaled with the spacing dial, kept in order around the
|
|
3235
|
+
* steps the theme keeps exact: each the plain scaled value unless it would
|
|
3236
|
+
* meet or pass a kept one; those are spread evenly between the kept one and
|
|
3237
|
+
* the nearest step that keeps its place. A step with no room below a kept
|
|
3238
|
+
* one takes the value of the one below it.
|
|
3239
|
+
* @param {Array<[string, number]>} steps token name and the base value, in px
|
|
3240
|
+
* @param {number} f the rung's factor
|
|
3241
|
+
* @param {Record<string, number>} kept exact steps the theme keeps, in px
|
|
3242
|
+
* @param {string[]} [noRoom] filled with the steps left no room
|
|
3243
|
+
* @returns {Array<[string, number]>} every step's new value in px, kept ones as kept
|
|
3244
|
+
*/
|
|
3245
|
+
export function spacingSteps(steps, f, kept, noRoom = []) {
|
|
3246
|
+
const px = [...steps].sort((a, b) => a[1] - b[1]);
|
|
3247
|
+
const n = px.map(([k, v]) => (k in kept ? kept[k] : v * f));
|
|
3248
|
+
const isKept = (/** @type {number} */ i) => px[i][0] in kept;
|
|
3249
|
+
px.forEach(([, base], k) => {
|
|
3250
|
+
if (!isKept(k)) return;
|
|
3251
|
+
// The steps below that would meet or pass it are spread evenly between
|
|
3252
|
+
// the last one that does not and it.
|
|
3253
|
+
let j0 = k;
|
|
3254
|
+
while (
|
|
3255
|
+
j0 > 0 &&
|
|
3256
|
+
!isKept(j0 - 1) &&
|
|
3257
|
+
n[j0 - 1] >= n[k] &&
|
|
3258
|
+
px[j0 - 1][1] < base
|
|
3259
|
+
)
|
|
3260
|
+
j0--;
|
|
3261
|
+
if (j0 < k) {
|
|
3262
|
+
const lo = j0 > 0 ? n[j0 - 1] : 0;
|
|
3263
|
+
if (lo >= n[k])
|
|
3264
|
+
for (let j = j0; j < k; j++) {
|
|
3265
|
+
n[j] = lo;
|
|
3266
|
+
noRoom.push(px[j][0]);
|
|
3267
|
+
}
|
|
3268
|
+
else
|
|
3269
|
+
for (let j = j0; j < k; j++)
|
|
3270
|
+
n[j] = lo + ((n[k] - lo) * (j - j0 + 1)) / (k - j0 + 1);
|
|
3271
|
+
}
|
|
3272
|
+
// The steps above that would meet or fall under it, likewise up to the
|
|
3273
|
+
// next one that does not (or above it by their own scaled steps).
|
|
3274
|
+
let j1 = k;
|
|
3275
|
+
while (
|
|
3276
|
+
j1 < n.length - 1 &&
|
|
3277
|
+
!isKept(j1 + 1) &&
|
|
3278
|
+
n[j1 + 1] <= n[k] &&
|
|
3279
|
+
px[j1 + 1][1] > base
|
|
3280
|
+
)
|
|
3281
|
+
j1++;
|
|
3282
|
+
if (j1 > k) {
|
|
3283
|
+
const hi = j1 < n.length - 1 ? n[j1 + 1] : null;
|
|
3284
|
+
for (let j = k + 1; j <= j1; j++)
|
|
3285
|
+
n[j] =
|
|
3286
|
+
hi == null
|
|
3287
|
+
? n[k] + (px[j][1] - base) * f
|
|
3288
|
+
: n[k] + ((hi - n[k]) * (j - k)) / (j1 - k + 1);
|
|
3289
|
+
}
|
|
3290
|
+
});
|
|
3291
|
+
return px.map(([k], i) => [k, n[i]]);
|
|
3292
|
+
}
|
|
3293
|
+
|
|
2104
3294
|
/**
|
|
2105
3295
|
* Control heights scaled with the spacing dial (a denser theme has shorter
|
|
2106
3296
|
* controls), each kept distinct and in order: smallest to largest, a
|
|
@@ -2110,28 +3300,44 @@ function keepShades(pal, kept) {
|
|
|
2110
3300
|
* scaled above it.
|
|
2111
3301
|
* @param {Array<[string, string]>} sizes token name and the theme's value
|
|
2112
3302
|
* @param {number} f the spacing rung's factor
|
|
3303
|
+
* @param {Record<string, number>} [kept] exact sizes the theme keeps, in px,
|
|
3304
|
+
* which the others stay in order around
|
|
3305
|
+
* @param {string[]} [noRoom] filled with the sizes left no room between
|
|
3306
|
+
* two kept ones (each then equals the one below it)
|
|
2113
3307
|
* @returns {Array<[string, string]>} the tokens it writes, in px
|
|
2114
3308
|
*/
|
|
2115
|
-
export function controlSizes(sizes, f) {
|
|
3309
|
+
export function controlSizes(sizes, f, kept = {}, noRoom = []) {
|
|
2116
3310
|
const px = sizes
|
|
2117
3311
|
.filter(([, v]) => /^[\d.]+px$/.test(String(v).trim()))
|
|
2118
3312
|
.map(([k, v]) => /** @type {[string, number]} */ ([k, parseFloat(v)]))
|
|
2119
3313
|
.sort((a, b) => a[1] - b[1]);
|
|
2120
|
-
/** @type {
|
|
2121
|
-
const
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
3314
|
+
/** @type {number[]} */
|
|
3315
|
+
const n = px.map(([k, v]) =>
|
|
3316
|
+
k in kept ? kept[k] : Math.max(Math.min(24, v), Math.round(v * f)),
|
|
3317
|
+
);
|
|
3318
|
+
/** The step between two sizes, scaled. @param {number} i */
|
|
3319
|
+
const step = i => Math.max(1, Math.round((px[i][1] - px[i - 1][1]) * f));
|
|
3320
|
+
// Only a size that would meet (or pass) the one below moves, up; then one
|
|
3321
|
+
// that would meet a kept size above it moves down (a kept size stays).
|
|
3322
|
+
for (let i = 1; i < n.length; i++)
|
|
3323
|
+
if (!(px[i][0] in kept) && n[i] <= n[i - 1]) n[i] = n[i - 1] + step(i);
|
|
3324
|
+
for (let i = n.length - 2; i >= 0; i--)
|
|
3325
|
+
if (!(px[i][0] in kept) && n[i] >= n[i + 1]) n[i] = n[i + 1] - step(i + 1);
|
|
3326
|
+
// A size between two that cannot both move (kept, or one kept and the
|
|
3327
|
+
// other already at its place) sits halfway between them; with no room
|
|
3328
|
+
// between them it takes the smaller one's size, and the result says so.
|
|
3329
|
+
for (let i = 1; i < n.length - 1; i++)
|
|
3330
|
+
if (!(px[i][0] in kept) && (n[i] <= n[i - 1] || n[i] >= n[i + 1])) {
|
|
3331
|
+
const mid = Math.round(((n[i - 1] + n[i + 1]) / 2) * 2) / 2;
|
|
3332
|
+
if (mid > n[i - 1] && mid < n[i + 1]) n[i] = mid;
|
|
3333
|
+
else {
|
|
3334
|
+
n[i] = n[i - 1];
|
|
3335
|
+
noRoom.push(px[i][0]);
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
3338
|
+
return px
|
|
3339
|
+
.map(([k], i) => /** @type {[string, string]} */ ([k, `${n[i]}px`]))
|
|
3340
|
+
.filter(([k]) => !(k in kept));
|
|
2135
3341
|
}
|
|
2136
3342
|
|
|
2137
3343
|
/**
|
|
@@ -2142,6 +3348,8 @@ export function controlSizes(sizes, f) {
|
|
|
2142
3348
|
* @returns {any}
|
|
2143
3349
|
*/
|
|
2144
3350
|
export function compileTheme(ctx, data, base) {
|
|
3351
|
+
ctx.sizeNotes = [];
|
|
3352
|
+
ctx.compileNotes = [];
|
|
2145
3353
|
// A setting the editor no longer accepts never reaches the build.
|
|
2146
3354
|
data = {...data, settings: data.settings.filter(s => !valueProblem(s))};
|
|
2147
3355
|
const TOKENS = ctx.project.core.tokenDefaults;
|
|
@@ -2153,7 +3361,7 @@ export function compileTheme(ctx, data, base) {
|
|
|
2153
3361
|
rem(base.light['--font-size-base']),
|
|
2154
3362
|
rem(base.light['--font-size-2xl']),
|
|
2155
3363
|
];
|
|
2156
|
-
const baseSize =
|
|
3364
|
+
const baseSize = baseTextPx(base);
|
|
2157
3365
|
const baseRatio =
|
|
2158
3366
|
b0 && b3 ? Math.round((b3 / b0) ** (1 / 3) * 100) / 100 : 1.2;
|
|
2159
3367
|
/** @type {any} */
|
|
@@ -2164,6 +3372,42 @@ export function compileTheme(ctx, data, base) {
|
|
|
2164
3372
|
: data.mode === 'dark-only'
|
|
2165
3373
|
? 'dark'
|
|
2166
3374
|
: null;
|
|
3375
|
+
/** The tokens the theme's own source sets, which win over the settings. */
|
|
3376
|
+
/** @type {Record<string, string> | null} */
|
|
3377
|
+
let ownTok = null;
|
|
3378
|
+
const ownTokenValues = () => {
|
|
3379
|
+
if (ownTok) return ownTok;
|
|
3380
|
+
try {
|
|
3381
|
+
ownTok = ownSourceTokenValues(ctx.project.cwd, data.name);
|
|
3382
|
+
} catch {
|
|
3383
|
+
ownTok = {};
|
|
3384
|
+
}
|
|
3385
|
+
return ownTok;
|
|
3386
|
+
};
|
|
3387
|
+
/**
|
|
3388
|
+
* The exact tokens kept where a dial writes (whole theme with no part):
|
|
3389
|
+
* the whole-theme settings', then the own source's, by name.
|
|
3390
|
+
* @param {string | null} t a part, for its own settings too
|
|
3391
|
+
* @returns {Map<string, string>}
|
|
3392
|
+
*/
|
|
3393
|
+
const keptTokens = t => {
|
|
3394
|
+
/** @type {Map<string, string>} */
|
|
3395
|
+
const out = new Map();
|
|
3396
|
+
for (const x of data.settings)
|
|
3397
|
+
if (
|
|
3398
|
+
x.source === 'token' &&
|
|
3399
|
+
!x.when &&
|
|
3400
|
+
!x.state &&
|
|
3401
|
+
!(modeOnly && x.mode && x.mode !== modeOnly) &&
|
|
3402
|
+
(!x.on || (t != null && partsOf(ctx, x).includes(t)))
|
|
3403
|
+
)
|
|
3404
|
+
out.set(x.thing, String(x.value));
|
|
3405
|
+
for (const [k, v] of Object.entries(ownTokenValues())) out.set(k, v);
|
|
3406
|
+
return out;
|
|
3407
|
+
};
|
|
3408
|
+
/** @param {string | null} t */
|
|
3409
|
+
const keptSizes = t =>
|
|
3410
|
+
[...keptTokens(t)].filter(([k]) => /^--size-element-/.test(k));
|
|
2167
3411
|
/** @type {Record<string, {light?: string, dark?: string}>} */
|
|
2168
3412
|
const tok = {};
|
|
2169
3413
|
/** @param {string} name @param {string | [string, string]} v @param {string} [mode] */
|
|
@@ -2177,12 +3421,22 @@ export function compileTheme(ctx, data, base) {
|
|
|
2177
3421
|
if (!mode || mode === 'dark') t.dark = v[1];
|
|
2178
3422
|
return;
|
|
2179
3423
|
}
|
|
2180
|
-
|
|
3424
|
+
// A color set in a one-mode theme fills its mode's half only; the other
|
|
3425
|
+
// half keeps what it had (the base theme's), for the regions Core draws
|
|
3426
|
+
// in the other mode.
|
|
3427
|
+
const half = mode ?? modeOnly;
|
|
3428
|
+
// Only a color has a half per mode (light-dark() takes colors only): a
|
|
3429
|
+
// shadow or any other value in a one-mode theme is written whole.
|
|
3430
|
+
if (
|
|
3431
|
+
!MODAL.has(ctx.C.typeOf(name)) ||
|
|
3432
|
+
!half ||
|
|
3433
|
+
(!mode && !parseColor(String(v).trim()))
|
|
3434
|
+
) {
|
|
2181
3435
|
t.light = v;
|
|
2182
3436
|
t.dark = v;
|
|
2183
3437
|
return;
|
|
2184
3438
|
}
|
|
2185
|
-
t[/** @type {'light' | 'dark'} */ (
|
|
3439
|
+
t[/** @type {'light' | 'dark'} */ (half)] = v;
|
|
2186
3440
|
};
|
|
2187
3441
|
/** @param {Setting} s @param {string} target @returns {Record<string, any>} */
|
|
2188
3442
|
const block = (s, target) => {
|
|
@@ -2197,6 +3451,29 @@ export function compileTheme(ctx, data, base) {
|
|
|
2197
3451
|
}
|
|
2198
3452
|
return b;
|
|
2199
3453
|
};
|
|
3454
|
+
/**
|
|
3455
|
+
* A setting's rule block for one color mode: the style key's `@light` or
|
|
3456
|
+
* `@dark` block, with the state inside it for a state Core writes as a
|
|
3457
|
+
* pseudo-class.
|
|
3458
|
+
* @param {Setting} s @param {string} target @param {'light' | 'dark'} mode
|
|
3459
|
+
* @returns {Record<string, any>}
|
|
3460
|
+
*/
|
|
3461
|
+
const modeBlock = (s, target, mode) => {
|
|
3462
|
+
const t = (theme.components[target] ??= {});
|
|
3463
|
+
const tg = ctx.project.target.get(target);
|
|
3464
|
+
const state = s.state;
|
|
3465
|
+
const ownState = !!state && !!tg?.states.includes(state);
|
|
3466
|
+
const key =
|
|
3467
|
+
state && ownState
|
|
3468
|
+
? s.when == null
|
|
3469
|
+
? state
|
|
3470
|
+
: `${s.when}+${state}`
|
|
3471
|
+
: (s.when ?? 'base');
|
|
3472
|
+
const rule = (t[key] ??= {});
|
|
3473
|
+
const mb = (rule[`@${mode}`] ??= {});
|
|
3474
|
+
if (!state || ownState) return mb;
|
|
3475
|
+
return (mb[stateKey(state, wrapsOneControl(ctx, target))] ??= {});
|
|
3476
|
+
};
|
|
2200
3477
|
/**
|
|
2201
3478
|
* A padding on a part whose padding Core writes one variable per side
|
|
2202
3479
|
* (its derivedVarRegistry's container expansion), as each side's value,
|
|
@@ -2302,6 +3579,16 @@ export function compileTheme(ctx, data, base) {
|
|
|
2302
3579
|
]),
|
|
2303
3580
|
);
|
|
2304
3581
|
};
|
|
3582
|
+
/** Whether the base theme's own rule for the part writes a radius. */
|
|
3583
|
+
const baseWritesRadius = (
|
|
3584
|
+
/** @type {string} */ target,
|
|
3585
|
+
/** @type {string | undefined} */ when,
|
|
3586
|
+
) => {
|
|
3587
|
+
const rules = /** @type {any} */ (base).components?.[target] ?? {};
|
|
3588
|
+
return [rules.base, when ? rules[when] : undefined].some(
|
|
3589
|
+
r => r && typeof r === 'object' && 'borderRadius' in r,
|
|
3590
|
+
);
|
|
3591
|
+
};
|
|
2305
3592
|
/**
|
|
2306
3593
|
* A radius of more than one value on a part whose radius Core copies
|
|
2307
3594
|
* into a variable for the content inside it (a card's full-bleed
|
|
@@ -2471,9 +3758,11 @@ export function compileTheme(ctx, data, base) {
|
|
|
2471
3758
|
return n && n.unit === '' && n.value === 0 ? '0px' : v;
|
|
2472
3759
|
};
|
|
2473
3760
|
for (const {target, s, list} of padQueue.values()) {
|
|
2474
|
-
// The own source's padding on the same rule wins over the editor's
|
|
2475
|
-
//
|
|
2476
|
-
|
|
3761
|
+
// The own source's padding on the same rule wins over the editor's on
|
|
3762
|
+
// the sides it sets (the result says so); the editor's other sides
|
|
3763
|
+
// still apply.
|
|
3764
|
+
const own = ownBox(target, s);
|
|
3765
|
+
if (own.ltr.size === 4 && own.rtl.size === 4) continue;
|
|
2477
3766
|
const prefix = /** @type {string} */ (padVars(target));
|
|
2478
3767
|
const owner = prefix.slice('--astryx-'.length, -'-padding'.length);
|
|
2479
3768
|
const scoped = target !== owner || s.when != null || s.state != null;
|
|
@@ -2517,6 +3806,10 @@ export function compileTheme(ctx, data, base) {
|
|
|
2517
3806
|
}
|
|
2518
3807
|
}
|
|
2519
3808
|
}
|
|
3809
|
+
// The sides the own source pads in a direction are its own there.
|
|
3810
|
+
for (const dir of /** @type {const} */ (['ltr', 'rtl']))
|
|
3811
|
+
for (const x of Object.keys(side[dir]))
|
|
3812
|
+
if (own[dir].has(boxSide(x, dir))) delete side[dir][x];
|
|
2520
3813
|
/** @type {Record<string, string>} */
|
|
2521
3814
|
let plain = {};
|
|
2522
3815
|
/** @type {Record<string, string>} */
|
|
@@ -2586,6 +3879,78 @@ export function compileTheme(ctx, data, base) {
|
|
|
2586
3879
|
b[key] = {...(b[key] ?? {}), ...rule};
|
|
2587
3880
|
}
|
|
2588
3881
|
}
|
|
3882
|
+
// A variant the own source pads: the sides it sets are left without a
|
|
3883
|
+
// side variable there, so the variant's own padding (which Core writes
|
|
3884
|
+
// as the whole padding) wins over any side the plain rule writes.
|
|
3885
|
+
for (const owner of new Set(
|
|
3886
|
+
[...padQueue.values()]
|
|
3887
|
+
.filter(({target, s}) => !s.when && !s.state && padVars(target))
|
|
3888
|
+
.map(({target}) =>
|
|
3889
|
+
/** @type {string} */ (padVars(target)).slice(
|
|
3890
|
+
'--astryx-'.length,
|
|
3891
|
+
-'-padding'.length,
|
|
3892
|
+
),
|
|
3893
|
+
),
|
|
3894
|
+
)) {
|
|
3895
|
+
const values = ctx.C.variantValues(owner);
|
|
3896
|
+
for (const [prop, vals] of Object.entries(values ?? {}))
|
|
3897
|
+
for (const v of /** @type {string[]} */ (vals ?? [])) {
|
|
3898
|
+
const when = `${prop}:${v}`;
|
|
3899
|
+
const own = ownBox(owner, /** @type {any} */ ({when}));
|
|
3900
|
+
if (!own.ltr.size && !own.rtl.size) continue;
|
|
3901
|
+
const variant = /** @type {any} */ ({when});
|
|
3902
|
+
const logical = [
|
|
3903
|
+
'block-start',
|
|
3904
|
+
'block-end',
|
|
3905
|
+
'inline-start',
|
|
3906
|
+
'inline-end',
|
|
3907
|
+
];
|
|
3908
|
+
const at = (/** @type {'ltr' | 'rtl'} */ dir) =>
|
|
3909
|
+
logical.filter(x => own[dir].has(boxSide(x, dir)));
|
|
3910
|
+
const [l, r] = [at('ltr'), at('rtl')];
|
|
3911
|
+
const b = block(variant, owner);
|
|
3912
|
+
for (const x of l.filter(x => r.includes(x)))
|
|
3913
|
+
b[`--astryx-${owner}-padding-${x}`] ??= 'initial';
|
|
3914
|
+
// A pair variable the plain rule writes (the inline or block
|
|
3915
|
+
// padding) feeds both its sides, under the side variables: where
|
|
3916
|
+
// the own source pads both, it is left without a value too, so
|
|
3917
|
+
// the variant's own padding holds there.
|
|
3918
|
+
for (const pair of ['block', 'inline'])
|
|
3919
|
+
if (
|
|
3920
|
+
[`${pair}-start`, `${pair}-end`].every(
|
|
3921
|
+
x => l.includes(x) && r.includes(x),
|
|
3922
|
+
)
|
|
3923
|
+
)
|
|
3924
|
+
b[`--astryx-${owner}-padding-${pair}`] ??= 'initial';
|
|
3925
|
+
// Its own sides inherit into a plain one of the part inside it:
|
|
3926
|
+
// they are reset there like the editor's (below).
|
|
3927
|
+
const sel = narrowSelector(owner, variant);
|
|
3928
|
+
if (sel) {
|
|
3929
|
+
const bySel = narrow.get(owner) ?? new Map();
|
|
3930
|
+
const names = bySel.get(sel) ?? new Set();
|
|
3931
|
+
for (const x of new Set([...l, ...r]))
|
|
3932
|
+
names.add(`--astryx-${owner}-padding-${x}`);
|
|
3933
|
+
for (const pair of ['block', 'inline'])
|
|
3934
|
+
if ([...l, ...r].some(x => x.startsWith(pair)))
|
|
3935
|
+
names.add(`--astryx-${owner}-padding-${pair}`);
|
|
3936
|
+
bySel.set(sel, names);
|
|
3937
|
+
narrow.set(owner, bySel);
|
|
3938
|
+
}
|
|
3939
|
+
for (const [dir, xs] of /** @type {const} */ ([
|
|
3940
|
+
[':dir(ltr)', l.filter(x => !r.includes(x))],
|
|
3941
|
+
[':dir(rtl)', r.filter(x => !l.includes(x))],
|
|
3942
|
+
]))
|
|
3943
|
+
if (xs.length) {
|
|
3944
|
+
const [db, key] = dirBlock(variant, owner, dir);
|
|
3945
|
+
db[key] = {
|
|
3946
|
+
...(db[key] ?? {}),
|
|
3947
|
+
...Object.fromEntries(
|
|
3948
|
+
xs.map(x => [`--astryx-${owner}-padding-${x}`, 'initial']),
|
|
3949
|
+
),
|
|
3950
|
+
};
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
}
|
|
2589
3954
|
// A plain part inside a narrower one: each side the narrower selector
|
|
2590
3955
|
// writes is reset (left without a value, so Core's own fallback holds),
|
|
2591
3956
|
// on the part's own class at its own weight, before any base setting a
|
|
@@ -2668,25 +4033,66 @@ export function compileTheme(ctx, data, base) {
|
|
|
2668
4033
|
return out;
|
|
2669
4034
|
};
|
|
2670
4035
|
/**
|
|
2671
|
-
*
|
|
2672
|
-
*
|
|
4036
|
+
* The box side a padding side names in a direction: a physical side as it
|
|
4037
|
+
* is, a logical one as the direction reads it (inline-start is the left in
|
|
4038
|
+
* left-to-right text, the right in right-to-left).
|
|
4039
|
+
* @param {string} x top, right, bottom, left, or a logical side
|
|
4040
|
+
* @param {'ltr' | 'rtl'} dir
|
|
4041
|
+
*/
|
|
4042
|
+
const boxSide = (x, dir) =>
|
|
4043
|
+
({
|
|
4044
|
+
'block-start': 'top',
|
|
4045
|
+
'block-end': 'bottom',
|
|
4046
|
+
'inline-start': dir === 'ltr' ? 'left' : 'right',
|
|
4047
|
+
'inline-end': dir === 'ltr' ? 'right' : 'left',
|
|
4048
|
+
})[x] ?? x;
|
|
4049
|
+
/**
|
|
4050
|
+
* The padding sides a padding property names, as it names them (physical
|
|
4051
|
+
* stays physical, logical stays logical), with each one's value.
|
|
4052
|
+
* @param {string} prop kebab-case
|
|
4053
|
+
* @param {string} value
|
|
4054
|
+
* @returns {Record<string, string>}
|
|
4055
|
+
*/
|
|
4056
|
+
const paddingSides = (prop, value) => {
|
|
4057
|
+
const w = splitTop(String(value).trim(), ' ').map(y => y.trim());
|
|
4058
|
+
if (prop === 'padding') {
|
|
4059
|
+
const [top, right = top, bottom = top, left = right] = w;
|
|
4060
|
+
return {top, right, bottom, left};
|
|
4061
|
+
}
|
|
4062
|
+
if (prop === 'padding-block')
|
|
4063
|
+
return {'block-start': w[0], 'block-end': w[1] ?? w[0]};
|
|
4064
|
+
if (prop === 'padding-inline')
|
|
4065
|
+
return {'inline-start': w[0], 'inline-end': w[1] ?? w[0]};
|
|
4066
|
+
const side = prop.slice('padding-'.length);
|
|
4067
|
+
return /^(top|right|bottom|left|(block|inline)-(start|end))$/.test(side)
|
|
4068
|
+
? {[side]: w[0]}
|
|
4069
|
+
: {};
|
|
4070
|
+
};
|
|
4071
|
+
/**
|
|
4072
|
+
* The box sides the theme's own source pads on a part's rule for a scope
|
|
4073
|
+
* (its plain rule, a variant, a state), in each direction.
|
|
2673
4074
|
* @param {string} target @param {Setting} s
|
|
4075
|
+
* @returns {Record<'ltr' | 'rtl', Set<string>>}
|
|
2674
4076
|
*/
|
|
2675
|
-
const
|
|
4077
|
+
const ownBox = (target, s) => {
|
|
4078
|
+
const out = {ltr: new Set(), rtl: new Set()};
|
|
2676
4079
|
try {
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
4080
|
+
for (const k of ownPartTokens(
|
|
4081
|
+
ctx.project.cwd,
|
|
4082
|
+
data.name,
|
|
4083
|
+
target,
|
|
4084
|
+
s.when ?? 'base',
|
|
4085
|
+
s.state ? `:${s.state}` : null,
|
|
4086
|
+
).keys())
|
|
4087
|
+
for (const x of Object.keys(paddingSides(k, '0')))
|
|
4088
|
+
for (const dir of /** @type {const} */ (['ltr', 'rtl']))
|
|
4089
|
+
out[dir].add(boxSide(x, dir));
|
|
2686
4090
|
} catch {
|
|
2687
|
-
|
|
4091
|
+
// No own source.
|
|
2688
4092
|
}
|
|
4093
|
+
return out;
|
|
2689
4094
|
};
|
|
4095
|
+
|
|
2690
4096
|
/**
|
|
2691
4097
|
* The selector a narrower padding scope is written with (a clickable
|
|
2692
4098
|
* card's class, a variant's attribute, a state's pseudo-class), or null
|
|
@@ -2712,11 +4118,18 @@ export function compileTheme(ctx, data, base) {
|
|
|
2712
4118
|
const keys = Object.keys(b);
|
|
2713
4119
|
const plain = keys.filter(k => !k.startsWith(':'));
|
|
2714
4120
|
const dirs = keys.filter(k => /^:(where\()?dir\(/.test(k));
|
|
4121
|
+
// The reset a plain part inside a narrower one gets comes before the
|
|
4122
|
+
// part's own direction rules at the same weight, so a side the part
|
|
4123
|
+
// sets for a direction holds inside a variant too.
|
|
4124
|
+
const resets = keys.filter(k => /^:where\((?!:?dir\().* \*\)$/.test(k));
|
|
2715
4125
|
const rest = keys.filter(
|
|
2716
|
-
k =>
|
|
4126
|
+
k =>
|
|
4127
|
+
k.startsWith(':') &&
|
|
4128
|
+
!/^:(where\()?dir\(/.test(k) &&
|
|
4129
|
+
!resets.includes(k),
|
|
2717
4130
|
);
|
|
2718
4131
|
return Object.fromEntries(
|
|
2719
|
-
[...plain, ...dirs, ...rest].map(k => [
|
|
4132
|
+
[...plain, ...resets, ...dirs, ...rest].map(k => [
|
|
2720
4133
|
k,
|
|
2721
4134
|
k.startsWith(':') && b[k] && typeof b[k] === 'object'
|
|
2722
4135
|
? dirFirst(b[k])
|
|
@@ -2767,33 +4180,213 @@ export function compileTheme(ctx, data, base) {
|
|
|
2767
4180
|
const parent = block({...s, state: undefined}, target);
|
|
2768
4181
|
return [parent, `${stateKey(s.state, wrapsOneControl(ctx, target))}${dir}`];
|
|
2769
4182
|
};
|
|
4183
|
+
/**
|
|
4184
|
+
* One half of a light-dark() pair (1 light, 2 dark), or the value itself.
|
|
4185
|
+
* @param {string} x @param {1 | 2} side
|
|
4186
|
+
*/
|
|
4187
|
+
const halfOf = (x, side) => {
|
|
4188
|
+
const inner = /^light-dark\((.*)\)$/s.exec(x.trim())?.[1];
|
|
4189
|
+
const halves = inner == null ? [] : splitTop(inner, ',');
|
|
4190
|
+
return halves.length === 2 ? halves[side - 1].trim() : x.trim();
|
|
4191
|
+
};
|
|
4192
|
+
/**
|
|
4193
|
+
* The value a variant of a part shows for a color property where the
|
|
4194
|
+
* theme sets none of its own: the base theme's rule for that variant (in
|
|
4195
|
+
* the state, else at rest), else Core's, else transparent for a
|
|
4196
|
+
* background Core never paints; null for a text color Core never sets.
|
|
4197
|
+
* @param {string} target @param {string} css a color longhand, kebab-case
|
|
4198
|
+
* @param {string} name the variant @param {string} [state]
|
|
4199
|
+
* @param {boolean} [stateOnly] the state's own value only, never the resting one
|
|
4200
|
+
* @returns {string | null}
|
|
4201
|
+
*/
|
|
4202
|
+
const exactVariantValue = (target, css, name, state, stateOnly = false) => {
|
|
4203
|
+
const when = `variant:${name}`;
|
|
4204
|
+
const kebabs =
|
|
4205
|
+
css === 'background-color' ? ['background-color', 'background'] : [css];
|
|
4206
|
+
const props = kebabs.map(camel);
|
|
4207
|
+
const steps = state
|
|
4208
|
+
? stateOnly
|
|
4209
|
+
? [state]
|
|
4210
|
+
: [state, undefined]
|
|
4211
|
+
: [undefined];
|
|
4212
|
+
for (const st of steps)
|
|
4213
|
+
for (const p of props) {
|
|
4214
|
+
const b = baseComponent(base, ctx, target, p, {when, state: st});
|
|
4215
|
+
if (b != null) return b;
|
|
4216
|
+
}
|
|
4217
|
+
const keyOf = (/** @type {string} */ k) => {
|
|
4218
|
+
const [attrs, cond = ''] = k.split(' ');
|
|
4219
|
+
return attrs.split(',').includes(`variant=${name}`) ? cond : null;
|
|
4220
|
+
};
|
|
4221
|
+
for (const st of steps)
|
|
4222
|
+
for (const p of kebabs) {
|
|
4223
|
+
const at = ctx.C.MAP[target]?.at?.[p];
|
|
4224
|
+
for (const [k, val] of Object.entries(at ?? {})) {
|
|
4225
|
+
const cond = keyOf(k);
|
|
4226
|
+
if (cond == null) continue;
|
|
4227
|
+
if (st ? cond.includes(`:${st}`) : cond === '') return val;
|
|
4228
|
+
}
|
|
4229
|
+
}
|
|
4230
|
+
if (stateOnly) return null;
|
|
4231
|
+
return css === 'background-color'
|
|
4232
|
+
? 'transparent'
|
|
4233
|
+
: css === 'color'
|
|
4234
|
+
? null
|
|
4235
|
+
: 'currentcolor';
|
|
4236
|
+
};
|
|
4237
|
+
/** The family a color property belongs to: background is background-color. @param {string} p */
|
|
4238
|
+
const colorFamily = p => {
|
|
4239
|
+
const k = kebab(p);
|
|
4240
|
+
return k === 'background' ? 'background-color' : k;
|
|
4241
|
+
};
|
|
2770
4242
|
/** @param {Setting} s @param {string} target @param {string} prop @param {string} v */
|
|
2771
4243
|
const putComp = (s, target, prop, v) => {
|
|
2772
4244
|
const b = block(s, target);
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
4245
|
+
// A gradient background for one mode goes into the rule's block for
|
|
4246
|
+
// that mode as its image layers (with their positions, sizes and
|
|
4247
|
+
// repeats), and a layer that is a color alone as that mode's color: the
|
|
4248
|
+
// other mode keeps every image and color it had, Core's own included.
|
|
4249
|
+
if (
|
|
4250
|
+
s.mode &&
|
|
4251
|
+
/^background(-image)?$/.test(kebab(prop)) &&
|
|
4252
|
+
/gradient\(/i.test(v)
|
|
4253
|
+
) {
|
|
4254
|
+
const mb = modeBlock(s, target, s.mode);
|
|
4255
|
+
// The background shorthand's last layer may carry the color under
|
|
4256
|
+
// the images: that color is this mode's background color, and the
|
|
4257
|
+
// rest are the images.
|
|
4258
|
+
const {images, color} =
|
|
4259
|
+
kebab(prop) === 'background'
|
|
4260
|
+
? backgroundColorLayer(v)
|
|
4261
|
+
: {images: v, color: null};
|
|
4262
|
+
const parts = splitTop(images ?? '', ',')
|
|
4263
|
+
.map(x => x.trim())
|
|
4264
|
+
.filter(Boolean)
|
|
4265
|
+
.map(layerParts);
|
|
4266
|
+
for (const k of [
|
|
4267
|
+
'background',
|
|
4268
|
+
'backgroundImage',
|
|
4269
|
+
'backgroundPosition',
|
|
4270
|
+
'backgroundSize',
|
|
4271
|
+
'backgroundRepeat',
|
|
4272
|
+
])
|
|
4273
|
+
delete mb[k];
|
|
4274
|
+
mb.backgroundImage = parts.map(x => x.image).join(', ');
|
|
4275
|
+
if (parts.some(x => x.position || x.size || x.repeat)) {
|
|
4276
|
+
mb.backgroundPosition = parts
|
|
4277
|
+
.map(x => x.position || '0% 0%')
|
|
4278
|
+
.join(', ');
|
|
4279
|
+
mb.backgroundSize = parts.map(x => x.size || 'auto').join(', ');
|
|
4280
|
+
mb.backgroundRepeat = parts.map(x => x.repeat || 'repeat').join(', ');
|
|
4281
|
+
}
|
|
4282
|
+
if (!parts.length) delete mb.backgroundImage;
|
|
4283
|
+
if (color) mb.backgroundColor = color;
|
|
4284
|
+
return;
|
|
4285
|
+
}
|
|
4286
|
+
// A color set in a one-mode theme fills its mode's half only (as a
|
|
4287
|
+
// setting for that mode does), so a region Core draws in the other mode
|
|
4288
|
+
// reads the other half.
|
|
4289
|
+
const mode =
|
|
4290
|
+
s.mode ??
|
|
4291
|
+
(modeOnly && parseColor(String(v).trim()) ? modeOnly : undefined);
|
|
4292
|
+
if (mode && !s.when && variantsReading(ctx, target, prop, s.state).length) {
|
|
4293
|
+
// A color for one mode on a part whose variants each have their own:
|
|
4294
|
+
// each variant's rule (in the state, for a state setting) gets the
|
|
4295
|
+
// color in its block for that mode, so no variant takes another's and
|
|
4296
|
+
// the other mode stays as it was. The part's own rule is left alone.
|
|
4297
|
+
// The property itself (a border color stays a border color); the
|
|
4298
|
+
// background shorthand is written as its color.
|
|
4299
|
+
const css = colorFamily(prop);
|
|
4300
|
+
// The part's own background being an image (a gradient) covers any
|
|
4301
|
+
// color under it: the color is written, and the result says it does
|
|
4302
|
+
// not show.
|
|
4303
|
+
// A color given beside its own image in one background (`linear-
|
|
4304
|
+
// gradient(…), ivory`) is that image's bottom layer, never covered by
|
|
4305
|
+
// it.
|
|
4306
|
+
const plainImage =
|
|
4307
|
+
css === 'background-color' && !ownImageLayer(s)
|
|
4308
|
+
? ['background', 'backgroundImage']
|
|
4309
|
+
.map(k => b[k])
|
|
4310
|
+
.find(y => typeof y === 'string' && /gradient\(|url\(/i.test(y))
|
|
4311
|
+
: undefined;
|
|
4312
|
+
if (plainImage)
|
|
4313
|
+
(ctx.compileNotes ??= []).push(
|
|
4314
|
+
`${target}'s own background is an image (${plainImage}), which covers the ${mode} color: it does not show until that background is a color`,
|
|
4315
|
+
);
|
|
4316
|
+
const names = new Set([
|
|
4317
|
+
...(ctx.C.variantValues(target)?.variant ?? []),
|
|
4318
|
+
...(ctx.C.MAP[target]?.attrs?.variant ?? []),
|
|
4319
|
+
]);
|
|
4320
|
+
for (const name of names) {
|
|
4321
|
+
const x = exactVariantValue(target, css, name, s.state);
|
|
4322
|
+
const variant = {...s, when: `variant:${name}`};
|
|
4323
|
+
const vb = block({...variant, mode: undefined}, target);
|
|
4324
|
+
const family = [
|
|
4325
|
+
css,
|
|
4326
|
+
...(css === 'background-color' ? ['background'] : []),
|
|
4327
|
+
];
|
|
4328
|
+
// A variant whose own background is an image (a gradient) keeps
|
|
4329
|
+
// it: one mode's color is not written there, and the result says so.
|
|
4330
|
+
const image = family
|
|
4331
|
+
.map(k => vb[camel(k)])
|
|
4332
|
+
.find(y => typeof y === 'string' && /gradient\(|url\(/i.test(y));
|
|
4333
|
+
if (image) {
|
|
4334
|
+
(ctx.compileNotes ??= []).push(
|
|
4335
|
+
`${target} variant:${name} keeps its own background image (${image}), so the ${mode} color is not written there`,
|
|
4336
|
+
);
|
|
4337
|
+
continue;
|
|
4338
|
+
}
|
|
4339
|
+
const mb = modeBlock(variant, target, mode);
|
|
4340
|
+
for (const k of family) delete mb[camel(k)];
|
|
4341
|
+
mb[camel(css)] = v;
|
|
4342
|
+
// A resting color on the variant's rule in this mode would also hide
|
|
4343
|
+
// the hover the variant shows unset in this mode: that hover is
|
|
4344
|
+
// written back there as it was, unless a hover setting of the
|
|
4345
|
+
// theme's own gives it one.
|
|
4346
|
+
if (
|
|
4347
|
+
!s.state &&
|
|
4348
|
+
!data.settings.some(
|
|
4349
|
+
y =>
|
|
4350
|
+
y.on === s.on &&
|
|
4351
|
+
y.state === 'hover' &&
|
|
4352
|
+
colorFamily(y.thing) === colorFamily(s.thing),
|
|
4353
|
+
)
|
|
4354
|
+
) {
|
|
4355
|
+
const hover = exactVariantValue(target, css, name, 'hover', true);
|
|
4356
|
+
if (hover != null && hover !== x)
|
|
4357
|
+
modeBlock({...variant, state: 'hover'}, target, mode)[
|
|
4358
|
+
camel(css)
|
|
4359
|
+
] ??= halfOf(hover, mode === 'light' ? 1 : 2);
|
|
4360
|
+
}
|
|
4361
|
+
}
|
|
4362
|
+
return;
|
|
4363
|
+
}
|
|
4364
|
+
// A color for one mode under the part's own gradient does not show:
|
|
4365
|
+
// the result says so.
|
|
4366
|
+
if (
|
|
4367
|
+
mode &&
|
|
4368
|
+
colorFamily(prop) === 'background-color' &&
|
|
4369
|
+
!ownImageLayer(s) &&
|
|
4370
|
+
['background', 'backgroundImage'].some(
|
|
4371
|
+
k => typeof b[k] === 'string' && /gradient\(|url\(/i.test(b[k]),
|
|
4372
|
+
)
|
|
4373
|
+
)
|
|
4374
|
+
(ctx.compileNotes ??= []).push(
|
|
4375
|
+
`${target}${s.when ? ` ${s.when}` : ''}${s.state ? ` :${s.state}` : ''} has a background image (a gradient) over its color, so the ${mode} color does not show until that background is a color`,
|
|
4376
|
+
);
|
|
4377
|
+
// A color for one mode goes into the rule's block for that mode, which
|
|
4378
|
+
// applies only where the element's color mode is that mode: in the other
|
|
4379
|
+
// mode the rule, and every rule the base theme and Core give the part in
|
|
4380
|
+
// context, apply as they would without it.
|
|
4381
|
+
// The background shorthand and its color are one property there: the
|
|
4382
|
+
// later setting replaces the earlier.
|
|
4383
|
+
if (mode) {
|
|
4384
|
+
const mb = modeBlock(s, target, mode);
|
|
4385
|
+
if (colorFamily(prop) === 'background-color')
|
|
4386
|
+
for (const k of ['background', 'backgroundColor'])
|
|
4387
|
+
if (typeof mb[k] === 'string' && !/gradient\(|url\(/i.test(mb[k]))
|
|
4388
|
+
delete mb[k];
|
|
4389
|
+
mb[prop] = v;
|
|
2797
4390
|
} else b[prop] = v;
|
|
2798
4391
|
};
|
|
2799
4392
|
// Palette stops go first, so an exact token setting always wins over them.
|
|
@@ -2871,14 +4464,30 @@ export function compileTheme(ctx, data, base) {
|
|
|
2871
4464
|
);
|
|
2872
4465
|
// Dials first, then exact tokens and part properties: an exact setting
|
|
2873
4466
|
// always wins over a dial, whatever the order they were made in.
|
|
2874
|
-
// A font for one role
|
|
2875
|
-
//
|
|
2876
|
-
const
|
|
4467
|
+
// A dial on a part (a font for one role, corners on cards) comes after
|
|
4468
|
+
// the whole-theme dials, so the part keeps its own whatever the order.
|
|
4469
|
+
const onPart = (/** @type {Setting} */ x) => !!x.on;
|
|
2877
4470
|
const ordered = [
|
|
2878
|
-
...data.settings.filter(x => x.source === 'dial' && !
|
|
2879
|
-
...data.settings.filter(x => x.source === 'dial' &&
|
|
4471
|
+
...data.settings.filter(x => x.source === 'dial' && !onPart(x)),
|
|
4472
|
+
...data.settings.filter(x => x.source === 'dial' && onPart(x)),
|
|
2880
4473
|
...data.settings.filter(x => x.source !== 'dial'),
|
|
2881
4474
|
];
|
|
4475
|
+
// The shell regions a setting gives a background of their own keep it
|
|
4476
|
+
// when the page background paints the rest of the shell.
|
|
4477
|
+
// A background for one mode is that region's in its mode block only:
|
|
4478
|
+
// the page background still paints the region in the other mode.
|
|
4479
|
+
const regionsWithOwnBackground = new Set(
|
|
4480
|
+
data.settings
|
|
4481
|
+
.filter(
|
|
4482
|
+
x =>
|
|
4483
|
+
x.source === 'property' &&
|
|
4484
|
+
!x.mode &&
|
|
4485
|
+
/^background(-color|-image)?$/.test(kebab(x.thing)),
|
|
4486
|
+
)
|
|
4487
|
+
.flatMap(x => partsOf(ctx, x)),
|
|
4488
|
+
);
|
|
4489
|
+
/** @type {Record<string, any>[]} */
|
|
4490
|
+
const fixedToViewport = [];
|
|
2882
4491
|
for (const s of ordered) {
|
|
2883
4492
|
// A setting for the mode a one-mode theme does not have stays saved but
|
|
2884
4493
|
// does not apply (maskedSettings names it).
|
|
@@ -2893,27 +4502,56 @@ export function compileTheme(ctx, data, base) {
|
|
|
2893
4502
|
/** @param {string} token */
|
|
2894
4503
|
const derived = token => `${radiusAt(base, s.value, token)}px`;
|
|
2895
4504
|
if (parts.length)
|
|
2896
|
-
for (const t of parts) {
|
|
4505
|
+
for (const t of parts.flatMap(p => cornerParts(ctx, p))) {
|
|
2897
4506
|
// A rung on a part gives what the whole-theme rung gives the
|
|
2898
4507
|
// radius token the part reads; an exact size is as given.
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
4508
|
+
{
|
|
4509
|
+
const value =
|
|
4510
|
+
rung || pill
|
|
4511
|
+
? derived(radiusTokenOf(ctx, t, s.when))
|
|
4512
|
+
: `${px}px`;
|
|
4513
|
+
// (A base rule that writes the part's own radius would win
|
|
4514
|
+
// over the token: the radius is written there as before.)
|
|
4515
|
+
const token = baseWritesRadius(t, s.when)
|
|
4516
|
+
? null
|
|
4517
|
+
: positionalCornerToken(ctx, t);
|
|
4518
|
+
putComp(
|
|
4519
|
+
{...s, mode: undefined},
|
|
4520
|
+
t,
|
|
4521
|
+
token ?? 'borderRadius',
|
|
4522
|
+
value,
|
|
4523
|
+
);
|
|
4524
|
+
}
|
|
2905
4525
|
}
|
|
2906
|
-
else
|
|
2907
|
-
for (const token of
|
|
2908
|
-
'--radius-element',
|
|
2909
|
-
'--radius-inner',
|
|
2910
|
-
'--radius-container',
|
|
2911
|
-
'--radius-page',
|
|
2912
|
-
])
|
|
4526
|
+
else {
|
|
4527
|
+
for (const token of DIAL_RADII)
|
|
2913
4528
|
// The radii the build already shows for this corners value (a
|
|
2914
4529
|
// build by an earlier editor, or a step back) are kept, as the
|
|
2915
4530
|
// colors are: they change only when corners or the base does.
|
|
2916
4531
|
putTok(token, ctx.keepRadii?.[token] ?? derived(token));
|
|
4532
|
+
// A part rule of the base theme that gives a radius other than the
|
|
4533
|
+
// dial token the part reads (a length, or a token the dial never
|
|
4534
|
+
// moves, such as --radius-full) would keep it over the dial: the
|
|
4535
|
+
// rule is given that dial token, so the part follows the dial. A
|
|
4536
|
+
// part Core rounds with a token the dial never moves (a badge's
|
|
4537
|
+
// --radius-full) keeps its base radius.
|
|
4538
|
+
for (const [t, when, radius] of baseRadii(
|
|
4539
|
+
/** @type {{components?: Record<string, any>}} */ (base),
|
|
4540
|
+
)) {
|
|
4541
|
+
const token = radiusTokenOf(ctx, t, when);
|
|
4542
|
+
if (
|
|
4543
|
+
ctx.project.target.has(t) &&
|
|
4544
|
+
DIAL_RADII.includes(token) &&
|
|
4545
|
+
radius !== `var(${token})`
|
|
4546
|
+
)
|
|
4547
|
+
putComp(
|
|
4548
|
+
{...s, mode: undefined, when},
|
|
4549
|
+
t,
|
|
4550
|
+
'borderRadius',
|
|
4551
|
+
`var(${token})`,
|
|
4552
|
+
);
|
|
4553
|
+
}
|
|
4554
|
+
}
|
|
2917
4555
|
} else if (PALETTE_DIALS.includes(s.thing) || s.thing === 'vibrancy') {
|
|
2918
4556
|
// The palette mapping above sets these tokens.
|
|
2919
4557
|
} else if (s.thing === 'font') {
|
|
@@ -3001,40 +4639,263 @@ export function compileTheme(ctx, data, base) {
|
|
|
3001
4639
|
// The rungs scale the spacing the base theme starts from.
|
|
3002
4640
|
/** @param {string} k @param {string} nv */
|
|
3003
4641
|
const put = (k, nv) => {
|
|
3004
|
-
if (parts.length)
|
|
3005
|
-
|
|
4642
|
+
if (parts.length) {
|
|
4643
|
+
// An exact whole-theme or own-source token holds inside the
|
|
4644
|
+
// part too.
|
|
4645
|
+
if (!keptTokens(null).has(k))
|
|
4646
|
+
for (const t of parts) block(s, t)[k] = nv;
|
|
4647
|
+
} else putTok(k, nv);
|
|
3006
4648
|
};
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
)
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
4649
|
+
// The spacing steps, in order around the exact steps the theme
|
|
4650
|
+
// keeps (a whole-theme one or the own source's: they win over the
|
|
4651
|
+
// dial).
|
|
4652
|
+
/** @type {Record<string, string>} */
|
|
4653
|
+
const dialVars = {};
|
|
4654
|
+
{
|
|
4655
|
+
/** @type {Array<[string, number]>} */
|
|
4656
|
+
const steps = [];
|
|
4657
|
+
for (const [k, core] of Object.entries(
|
|
4658
|
+
/** @type {Record<string, string>} */ (TOKENS),
|
|
4659
|
+
)) {
|
|
4660
|
+
const v = base.light[k] ?? core;
|
|
4661
|
+
if (/^--spacing-/.test(k) && /rem$/.test(v))
|
|
4662
|
+
steps.push([k, parseFloat(v) * 16]);
|
|
4663
|
+
}
|
|
4664
|
+
/** @type {Record<string, number>} */
|
|
4665
|
+
const keptSteps = {};
|
|
4666
|
+
for (const [k, v] of keptTokens(null))
|
|
4667
|
+
if (/^--spacing-/.test(k)) {
|
|
4668
|
+
const n = sizePx(v);
|
|
4669
|
+
if (n != null) keptSteps[k] = n;
|
|
4670
|
+
}
|
|
4671
|
+
/** @type {string[]} */
|
|
4672
|
+
const noRoom = [];
|
|
4673
|
+
for (const [k, n] of spacingSteps(steps, f, keptSteps, noRoom)) {
|
|
4674
|
+
const nv = `${Number((n / 16).toFixed(3))}rem`;
|
|
4675
|
+
dialVars[k] = k in keptSteps ? String(keptTokens(null).get(k)) : nv;
|
|
4676
|
+
if (!(k in keptSteps)) put(k, nv);
|
|
4677
|
+
}
|
|
4678
|
+
for (const k of noRoom)
|
|
4679
|
+
(ctx.sizeNotes ??= []).push(
|
|
4680
|
+
`${k} has no room between the exact spacing steps kept around it, so it takes the value of the one below it`,
|
|
4681
|
+
);
|
|
4682
|
+
}
|
|
4683
|
+
// An exact size the theme keeps (it wins over the dial) is a fixed
|
|
4684
|
+
// point the others stay in order around: a whole-theme one, one of
|
|
4685
|
+
// the theme's own source, and on a part the part's own, in any
|
|
4686
|
+
// unit that comes to a length (a spacing token through the value
|
|
4687
|
+
// this dial gives it). A whole-theme dial also keeps each part's
|
|
4688
|
+
// own exact sizes in order on that part.
|
|
4689
|
+
const sizeParts = parts.length
|
|
4690
|
+
? parts
|
|
4691
|
+
: [
|
|
4692
|
+
null,
|
|
4693
|
+
...new Set(
|
|
4694
|
+
data.settings
|
|
4695
|
+
.filter(
|
|
4696
|
+
x =>
|
|
4697
|
+
x.source === 'token' &&
|
|
4698
|
+
/^--size-element-/.test(x.thing) &&
|
|
4699
|
+
x.on &&
|
|
4700
|
+
!x.when &&
|
|
4701
|
+
!x.state,
|
|
4702
|
+
)
|
|
4703
|
+
.flatMap(x => partsOf(ctx, x)),
|
|
4704
|
+
),
|
|
4705
|
+
];
|
|
4706
|
+
for (const t of sizeParts) {
|
|
4707
|
+
/** @type {Record<string, number>} */
|
|
4708
|
+
const kept = {};
|
|
4709
|
+
for (const [k, v] of keptSizes(t)) {
|
|
4710
|
+
const n = sizePx(v, dialVars);
|
|
4711
|
+
if (n != null) kept[k] = n;
|
|
4712
|
+
}
|
|
4713
|
+
/** @type {string[]} */
|
|
4714
|
+
const noRoom = [];
|
|
4715
|
+
for (const [k, nv] of controlSizes(
|
|
4716
|
+
Object.keys(/** @type {Record<string, string>} */ (TOKENS))
|
|
4717
|
+
.filter(k => /^--size-element-/.test(k))
|
|
4718
|
+
.map(k => [k, base.light[k] ?? TOKENS[k]]),
|
|
4719
|
+
f,
|
|
4720
|
+
kept,
|
|
4721
|
+
noRoom,
|
|
4722
|
+
))
|
|
4723
|
+
if (t) block(s, t)[k] = nv;
|
|
4724
|
+
else putTok(k, nv);
|
|
4725
|
+
for (const k of noRoom)
|
|
4726
|
+
(ctx.sizeNotes ??= []).push(
|
|
4727
|
+
`${k}${t ? ` on ${t}` : ''} has no room between the exact sizes kept around it, so it takes the size of the one below it`,
|
|
4728
|
+
);
|
|
4729
|
+
}
|
|
4730
|
+
// A variant's own exact size is kept in order on that variant too,
|
|
4731
|
+
// with the part's and the theme's kept sizes around it.
|
|
4732
|
+
/** @type {Map<string, {t: string, when: string}>} */
|
|
4733
|
+
const variantScopes = new Map();
|
|
4734
|
+
for (const x of data.settings)
|
|
4735
|
+
if (
|
|
4736
|
+
x.source === 'token' &&
|
|
4737
|
+
/^--size-element-/.test(x.thing) &&
|
|
4738
|
+
x.on &&
|
|
4739
|
+
x.when &&
|
|
4740
|
+
!x.state
|
|
4741
|
+
)
|
|
4742
|
+
for (const t of partsOf(ctx, x))
|
|
4743
|
+
if (!parts.length || parts.includes(t))
|
|
4744
|
+
variantScopes.set(`${t} ${x.when}`, {t, when: x.when});
|
|
4745
|
+
for (const {t, when} of variantScopes.values()) {
|
|
4746
|
+
/** @type {Record<string, number>} */
|
|
4747
|
+
const kept = {};
|
|
4748
|
+
for (const [k, v] of keptSizes(t)) {
|
|
4749
|
+
const n = sizePx(v, dialVars);
|
|
4750
|
+
if (n != null) kept[k] = n;
|
|
4751
|
+
}
|
|
4752
|
+
for (const x of data.settings)
|
|
4753
|
+
if (
|
|
4754
|
+
x.source === 'token' &&
|
|
4755
|
+
/^--size-element-/.test(x.thing) &&
|
|
4756
|
+
x.when === when &&
|
|
4757
|
+
!x.state &&
|
|
4758
|
+
partsOf(ctx, x).includes(t)
|
|
4759
|
+
) {
|
|
4760
|
+
const n = sizePx(String(x.value), dialVars);
|
|
4761
|
+
if (n != null) kept[x.thing] = n;
|
|
4762
|
+
}
|
|
4763
|
+
/** @type {string[]} */
|
|
4764
|
+
const noRoom = [];
|
|
4765
|
+
for (const [k, nv] of controlSizes(
|
|
4766
|
+
Object.keys(/** @type {Record<string, string>} */ (TOKENS))
|
|
4767
|
+
.filter(k => /^--size-element-/.test(k))
|
|
4768
|
+
.map(k => [k, base.light[k] ?? TOKENS[k]]),
|
|
4769
|
+
f,
|
|
4770
|
+
kept,
|
|
4771
|
+
noRoom,
|
|
4772
|
+
))
|
|
4773
|
+
block({...s, when}, t)[k] = nv;
|
|
4774
|
+
for (const k of noRoom)
|
|
4775
|
+
(ctx.sizeNotes ??= []).push(
|
|
4776
|
+
`${k} on ${t} when ${when} has no room between the exact sizes kept around it, so it takes the size of the one below it`,
|
|
4777
|
+
);
|
|
3013
4778
|
}
|
|
3014
|
-
for (const [k, nv] of controlSizes(
|
|
3015
|
-
Object.keys(/** @type {Record<string, string>} */ (TOKENS))
|
|
3016
|
-
.filter(k => /^--size-element-/.test(k))
|
|
3017
|
-
.map(k => [k, base.light[k] ?? TOKENS[k]]),
|
|
3018
|
-
f,
|
|
3019
|
-
))
|
|
3020
|
-
put(k, nv);
|
|
3021
4779
|
}
|
|
3022
4780
|
} else if (s.source === 'token') {
|
|
3023
4781
|
if (parts.length) for (const t of parts) putComp(s, t, s.thing, s.value);
|
|
3024
4782
|
else putTok(s.thing, s.value, s.mode);
|
|
3025
4783
|
} else if (s.source === 'property') {
|
|
3026
|
-
|
|
4784
|
+
// A background on the whole app shell is the page background. The
|
|
4785
|
+
// shell's header and side nav paint the body or surface color over it
|
|
4786
|
+
// in every variant but section, and in an auto-height shell they are
|
|
4787
|
+
// sticky, so content must not show through them when it scrolls: they
|
|
4788
|
+
// take the same background. The main content area is made
|
|
4789
|
+
// see-through. A gradient is fixed to the viewport on the shell and on
|
|
4790
|
+
// those regions, so the regions line up with the page behind them. A
|
|
4791
|
+
// background a setting gives one of those regions itself is kept.
|
|
4792
|
+
/** @type {string[]} */
|
|
4793
|
+
const pageParts = [];
|
|
4794
|
+
if (
|
|
4795
|
+
parts.includes('app-shell') &&
|
|
4796
|
+
/^background(-color|-image)?$/.test(kebab(s.thing)) &&
|
|
4797
|
+
!s.when &&
|
|
4798
|
+
!s.state
|
|
4799
|
+
) {
|
|
4800
|
+
/** @param {Record<string, any>} r */
|
|
4801
|
+
const ownBackground = r =>
|
|
4802
|
+
['background', 'backgroundColor', 'backgroundImage'].some(
|
|
4803
|
+
k => k in r,
|
|
4804
|
+
);
|
|
4805
|
+
for (const region of ['app-shell-header', 'app-shell-sidenav'])
|
|
4806
|
+
if (
|
|
4807
|
+
ctx.project.target.has(region) &&
|
|
4808
|
+
!regionsWithOwnBackground.has(region)
|
|
4809
|
+
)
|
|
4810
|
+
pageParts.push(region);
|
|
4811
|
+
if (ctx.project.target.has('layout-content')) {
|
|
4812
|
+
const lc = ((theme.components['layout-content'] ??= {}).base ??= {});
|
|
4813
|
+
const main = (lc[':where(.astryx-app-shell [role="main"])'] ??= {});
|
|
4814
|
+
if (!ownBackground(main)) main.backgroundColor = 'transparent';
|
|
4815
|
+
}
|
|
4816
|
+
// Written after every setting (below), so no background shorthand
|
|
4817
|
+
// in the same rule resets it.
|
|
4818
|
+
if (/gradient\(/.test(String(s.value)))
|
|
4819
|
+
for (const t of ['app-shell', ...pageParts])
|
|
4820
|
+
fixedToViewport.push(block({...s, mode: undefined}, t));
|
|
4821
|
+
(ctx.compileNotes ??= []).push(
|
|
4822
|
+
"the page background on app-shell also paints the shell's header and side nav (they stay opaque when content scrolls under them) and makes the main content area see-through; what is drawn inside keeps its colors, and in the default elevated layout the content panel keeps its own surface",
|
|
4823
|
+
);
|
|
4824
|
+
}
|
|
4825
|
+
for (const t of [...parts, ...pageParts]) {
|
|
4826
|
+
// A heading takes its color from its color prop, which reads the
|
|
4827
|
+
// text color tokens after part styles: the color goes into those
|
|
4828
|
+
// tokens on the heading (a named color prop's own token for
|
|
4829
|
+
// --when color:<name>), so it shows there and nowhere else.
|
|
4830
|
+
if (t === 'heading' && kebab(s.thing) === 'color') {
|
|
4831
|
+
const named = /^color:([\w-]+)$/.exec(s.when ?? '')?.[1];
|
|
4832
|
+
const scope = named ? {...s, when: undefined} : s;
|
|
4833
|
+
for (const tok of named
|
|
4834
|
+
? [`--color-text-${named}`]
|
|
4835
|
+
: ['--color-text-primary', '--color-text-secondary'])
|
|
4836
|
+
// A value naming one of these tokens (the secondary text color)
|
|
4837
|
+
// leaves that token as it is: it would read itself.
|
|
4838
|
+
if (!new RegExp(`var\\(\\s*${tok}\\s*[,)]`).test(String(s.value)))
|
|
4839
|
+
putComp(scope, t, tok, s.value);
|
|
4840
|
+
continue;
|
|
4841
|
+
}
|
|
4842
|
+
// A part whose text is drawn by elements inside it that read a text
|
|
4843
|
+
// color token themselves (a top nav heading's title) takes the color
|
|
4844
|
+
// in those tokens too, on the part alone, so that text shows it.
|
|
4845
|
+
if (kebab(s.thing) === 'color')
|
|
4846
|
+
for (const tok of innerTextTokens(ctx.C, t))
|
|
4847
|
+
if (!new RegExp(`var\\(\\s*${tok}\\s*[,)]`).test(String(s.value)))
|
|
4848
|
+
putComp(s, t, tok, s.value);
|
|
3027
4849
|
// A padding on a part whose box reads Core's padding variables is
|
|
3028
4850
|
// resolved with the rest of that part's padding, below.
|
|
3029
4851
|
if (padVars(t) && /^padding(-|$)/.test(kebab(s.thing))) {
|
|
3030
4852
|
queuePadding(s, t);
|
|
3031
4853
|
continue;
|
|
3032
4854
|
}
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
4855
|
+
// A padding on a part without padding variables, where the own source
|
|
4856
|
+
// pads some sides of the same rule: the editor writes the other
|
|
4857
|
+
// sides only, each as it names it (physical stays physical), so the
|
|
4858
|
+
// own sides win; a side the own source holds in one direction only
|
|
4859
|
+
// is written for the other direction.
|
|
4860
|
+
if (/^padding(-|$)/.test(kebab(s.thing)) && !s.mode && !padVars(t)) {
|
|
4861
|
+
const own = ownBox(t, s);
|
|
4862
|
+
if (own.ltr.size || own.rtl.size) {
|
|
4863
|
+
for (const [x, v] of Object.entries(
|
|
4864
|
+
paddingSides(kebab(s.thing), String(s.value)),
|
|
4865
|
+
)) {
|
|
4866
|
+
const prop = camel(`padding-${x}`);
|
|
4867
|
+
const [inL, inR] = [
|
|
4868
|
+
own.ltr.has(boxSide(x, 'ltr')),
|
|
4869
|
+
own.rtl.has(boxSide(x, 'rtl')),
|
|
4870
|
+
];
|
|
4871
|
+
if (inL && inR) continue;
|
|
4872
|
+
if (!inL && !inR) {
|
|
4873
|
+
putComp(s, t, prop, v);
|
|
4874
|
+
continue;
|
|
4875
|
+
}
|
|
4876
|
+
const [db, key] = dirBlock(s, t, inL ? ':dir(rtl)' : ':dir(ltr)');
|
|
4877
|
+
db[key] = {...(db[key] ?? {}), [prop]: v};
|
|
4878
|
+
}
|
|
4879
|
+
continue;
|
|
4880
|
+
}
|
|
4881
|
+
}
|
|
4882
|
+
// One radius for a part whose corners Core draws by position: its
|
|
4883
|
+
// corner token on the part.
|
|
4884
|
+
const cornerToken =
|
|
4885
|
+
kebab(s.thing) === 'border-radius' &&
|
|
4886
|
+
!s.mode &&
|
|
4887
|
+
splitTop(String(s.value).trim(), ' ').length === 1 &&
|
|
4888
|
+
!String(s.value).includes('/')
|
|
4889
|
+
? baseWritesRadius(t, s.when)
|
|
4890
|
+
? null
|
|
4891
|
+
: positionalCornerToken(ctx, t)
|
|
4892
|
+
: null;
|
|
4893
|
+
const sides = cornerToken
|
|
4894
|
+
? {[cornerToken]: String(s.value)}
|
|
4895
|
+
: (wrappedPadding(t, s) ??
|
|
4896
|
+
containerSides(t, s) ??
|
|
4897
|
+
cornerRadii(t, s) ??
|
|
4898
|
+
boxSides(t, s));
|
|
3038
4899
|
if (sides)
|
|
3039
4900
|
for (const [k, v] of Object.entries(sides)) {
|
|
3040
4901
|
// A per-direction block joins what that direction already has,
|
|
@@ -3109,6 +4970,10 @@ export function compileTheme(ctx, data, base) {
|
|
|
3109
4970
|
}
|
|
3110
4971
|
}
|
|
3111
4972
|
emitPadding();
|
|
4973
|
+
for (const r of fixedToViewport) {
|
|
4974
|
+
delete r.backgroundAttachment;
|
|
4975
|
+
r.backgroundAttachment = 'fixed';
|
|
4976
|
+
}
|
|
3112
4977
|
orderBlocks();
|
|
3113
4978
|
// Core gives headings the body font when a theme sets only the body's:
|
|
3114
4979
|
// a font for the body alone keeps the base theme's heading font.
|
|
@@ -3162,7 +5027,16 @@ export function compileTheme(ctx, data, base) {
|
|
|
3162
5027
|
for (const [k, {light, dark}] of Object.entries(tok)) {
|
|
3163
5028
|
const l = light ?? base.light[k] ?? TOKENS[k];
|
|
3164
5029
|
const d = dark ?? base.dark[k] ?? TOKENS[k];
|
|
3165
|
-
|
|
5030
|
+
// A pair only for a color: anything else (a shadow) is one value, the
|
|
5031
|
+
// one the theme's mode shows.
|
|
5032
|
+
theme.tokens[k] =
|
|
5033
|
+
l === d
|
|
5034
|
+
? l
|
|
5035
|
+
: ctx.C.typeOf(k) === 'color' || isColorToken(k)
|
|
5036
|
+
? [l, d]
|
|
5037
|
+
: modeOnly === 'dark'
|
|
5038
|
+
? d
|
|
5039
|
+
: l;
|
|
3166
5040
|
}
|
|
3167
5041
|
// Base themes set the type tokens again for phones (a coarse pointer
|
|
3168
5042
|
// below the lg width); an exact font family or weight, and the headings
|
|
@@ -3198,11 +5072,200 @@ export function compileTheme(ctx, data, base) {
|
|
|
3198
5072
|
},
|
|
3199
5073
|
],
|
|
3200
5074
|
};
|
|
5075
|
+
// A longhand set after a shorthand that covers it is the one in effect,
|
|
5076
|
+
// as the result says. The build merges these blocks over the base
|
|
5077
|
+
// theme's, and a key the base holds keeps the base's (earlier) place, so
|
|
5078
|
+
// such a longhand would come before the shorthand and lose: it is written
|
|
5079
|
+
// as its own sides instead, which the base does not hold.
|
|
5080
|
+
const baseBlocks =
|
|
5081
|
+
/** @type {{components?: Record<string, any>}} */ (base).components ?? {};
|
|
5082
|
+
for (const [target, keys] of Object.entries(theme.components ?? {}))
|
|
5083
|
+
for (const [key, b] of Object.entries(
|
|
5084
|
+
/** @type {Record<string, any>} */ (keys),
|
|
5085
|
+
))
|
|
5086
|
+
laterLonghandsAsSides(b, baseBlocks[target]?.[key]);
|
|
3201
5087
|
if (!Object.keys(theme.tokens).length) delete theme.tokens;
|
|
3202
5088
|
if (!Object.keys(theme.components).length) delete theme.components;
|
|
3203
5089
|
return theme;
|
|
3204
5090
|
}
|
|
3205
5091
|
|
|
5092
|
+
/**
|
|
5093
|
+
* Whether shorthand `a` sets `b`, a logical side included: padding sets
|
|
5094
|
+
* padding-inline, border-color sets border-inline-start-color.
|
|
5095
|
+
* @param {string} a @param {string} b
|
|
5096
|
+
*/
|
|
5097
|
+
function coversSide(a, b) {
|
|
5098
|
+
if (coversProperty(a, b)) return true;
|
|
5099
|
+
const root = b.replace(/-(inline|block)(-start|-end)?(?=-|$)/, '');
|
|
5100
|
+
return root !== b && (a === root || coversProperty(a, root));
|
|
5101
|
+
}
|
|
5102
|
+
|
|
5103
|
+
/**
|
|
5104
|
+
* The part rules of a base theme that give a border radius: each as its
|
|
5105
|
+
* part, the rule's --when (undefined for the plain rule) and the radius.
|
|
5106
|
+
* @param {{components?: Record<string, any>}} base
|
|
5107
|
+
* @returns {Array<[string, string | undefined, string]>}
|
|
5108
|
+
*/
|
|
5109
|
+
function baseRadii(base) {
|
|
5110
|
+
/** @type {Array<[string, string | undefined, string]>} */
|
|
5111
|
+
const out = [];
|
|
5112
|
+
for (const [t, rules] of Object.entries(base.components ?? {}))
|
|
5113
|
+
for (const [key, rule] of Object.entries(
|
|
5114
|
+
/** @type {Record<string, any>} */ (rules ?? {}),
|
|
5115
|
+
)) {
|
|
5116
|
+
if (key !== 'base' && !/^[\w-]+:[\w-]+$/.test(key)) continue;
|
|
5117
|
+
const r = rule?.borderRadius;
|
|
5118
|
+
if (typeof r === 'string')
|
|
5119
|
+
out.push([t, key === 'base' ? undefined : key, r.trim()]);
|
|
5120
|
+
}
|
|
5121
|
+
return out;
|
|
5122
|
+
}
|
|
5123
|
+
|
|
5124
|
+
/**
|
|
5125
|
+
* Whether a setting's own value is a background with an image layer, so any
|
|
5126
|
+
* color it writes is that background's bottom layer.
|
|
5127
|
+
* @param {Setting} s
|
|
5128
|
+
*/
|
|
5129
|
+
function ownImageLayer(s) {
|
|
5130
|
+
return (
|
|
5131
|
+
kebab(s.thing) === 'background' && /gradient\(|url\(/i.test(String(s.value))
|
|
5132
|
+
);
|
|
5133
|
+
}
|
|
5134
|
+
|
|
5135
|
+
/**
|
|
5136
|
+
* A background shorthand split into its image layers and the color its
|
|
5137
|
+
* last layer may carry (the color under every image): a color word beside
|
|
5138
|
+
* the last image, or a last layer that is a color alone.
|
|
5139
|
+
* @param {string} value
|
|
5140
|
+
* @returns {{images: string, color: string | null}}
|
|
5141
|
+
*/
|
|
5142
|
+
export function backgroundColorLayer(value) {
|
|
5143
|
+
const layers = splitTop(value, ',')
|
|
5144
|
+
.map(x => x.trim())
|
|
5145
|
+
.filter(Boolean);
|
|
5146
|
+
const last = layers.at(-1) ?? '';
|
|
5147
|
+
const words = splitTop(last, ' ').map(x => x.trim());
|
|
5148
|
+
const at = words.findIndex(
|
|
5149
|
+
w => !/gradient\(|url\(/i.test(w) && isColorValue(w),
|
|
5150
|
+
);
|
|
5151
|
+
if (at < 0) return {images: value, color: null};
|
|
5152
|
+
const rest = words.filter((_, i) => i !== at).join(' ');
|
|
5153
|
+
return {
|
|
5154
|
+
images: [...layers.slice(0, -1), ...(rest ? [rest] : [])].join(', '),
|
|
5155
|
+
color: words[at],
|
|
5156
|
+
};
|
|
5157
|
+
}
|
|
5158
|
+
|
|
5159
|
+
/**
|
|
5160
|
+
* A background layer's image (its gradient) and what follows it, as the
|
|
5161
|
+
* background longhands take them: position, size (after a slash) and
|
|
5162
|
+
* repeat.
|
|
5163
|
+
* @param {string} layer
|
|
5164
|
+
* @returns {{image: string, position: string, size: string, repeat: string}}
|
|
5165
|
+
*/
|
|
5166
|
+
function layerParts(layer) {
|
|
5167
|
+
const fn = /^((?:repeating-)?(?:linear|radial|conic)-gradient)\(/i.exec(
|
|
5168
|
+
layer,
|
|
5169
|
+
);
|
|
5170
|
+
if (!fn) return {image: layer, position: '', size: '', repeat: ''};
|
|
5171
|
+
let depth = 0;
|
|
5172
|
+
let end = layer.length - 1;
|
|
5173
|
+
for (let i = fn[0].length - 1; i < layer.length; i++) {
|
|
5174
|
+
if (layer[i] === '(') depth++;
|
|
5175
|
+
if (layer[i] === ')' && --depth === 0) {
|
|
5176
|
+
end = i;
|
|
5177
|
+
break;
|
|
5178
|
+
}
|
|
5179
|
+
}
|
|
5180
|
+
const rest = splitTop(layer.slice(end + 1).trim(), ' ');
|
|
5181
|
+
const REPEAT = /^(repeat|no-repeat|repeat-x|repeat-y|space|round)$/i;
|
|
5182
|
+
const repeat = rest.filter(w => REPEAT.test(w)).join(' ');
|
|
5183
|
+
const words = rest.filter(w => !REPEAT.test(w)).join(' ');
|
|
5184
|
+
const [position = '', size = ''] = words.split('/').map(x => x.trim());
|
|
5185
|
+
return {image: layer.slice(0, end + 1), position, size, repeat};
|
|
5186
|
+
}
|
|
5187
|
+
|
|
5188
|
+
/** A side word in a longhand's name, in the order a box value lists sides. */
|
|
5189
|
+
const BOX_SIDES = ['top', 'right', 'bottom', 'left'];
|
|
5190
|
+
|
|
5191
|
+
/**
|
|
5192
|
+
* A block's longhands that follow a shorthand covering them, where the base
|
|
5193
|
+
* theme's block holds the same key, rewritten as that longhand's own sides
|
|
5194
|
+
* (padding-inline as padding-inline-start and -end, border-color as the
|
|
5195
|
+
* four side colors), in place and at the same position; a state's block
|
|
5196
|
+
* nested in it too. A longhand that is no side pair or box is kept.
|
|
5197
|
+
* @param {Record<string, any>} b
|
|
5198
|
+
* @param {Record<string, any> | undefined} baseBlock
|
|
5199
|
+
*/
|
|
5200
|
+
function laterLonghandsAsSides(b, baseBlock) {
|
|
5201
|
+
if (
|
|
5202
|
+
!b ||
|
|
5203
|
+
typeof b !== 'object' ||
|
|
5204
|
+
!baseBlock ||
|
|
5205
|
+
typeof baseBlock !== 'object'
|
|
5206
|
+
)
|
|
5207
|
+
return;
|
|
5208
|
+
for (const [k, v] of Object.entries(b))
|
|
5209
|
+
if (k.startsWith(':') && v && typeof v === 'object')
|
|
5210
|
+
laterLonghandsAsSides(v, baseBlock[k]);
|
|
5211
|
+
const keys = Object.keys(b);
|
|
5212
|
+
/** @type {Record<string, any>} */
|
|
5213
|
+
const out = {};
|
|
5214
|
+
let changed = false;
|
|
5215
|
+
keys.forEach((k, i) => {
|
|
5216
|
+
const v = b[k];
|
|
5217
|
+
const css = kebab(k);
|
|
5218
|
+
const sides =
|
|
5219
|
+
typeof v === 'string' &&
|
|
5220
|
+
!k.startsWith('-') &&
|
|
5221
|
+
typeof baseBlock[k] === 'string' &&
|
|
5222
|
+
keys
|
|
5223
|
+
.slice(0, i)
|
|
5224
|
+
.some(e => !e.startsWith(':') && coversSide(kebab(e), css))
|
|
5225
|
+
? sidesOf(css, v)
|
|
5226
|
+
: null;
|
|
5227
|
+
if (!sides) {
|
|
5228
|
+
out[k] = v;
|
|
5229
|
+
return;
|
|
5230
|
+
}
|
|
5231
|
+
changed = true;
|
|
5232
|
+
for (const [p, x] of Object.entries(sides)) {
|
|
5233
|
+
delete out[camel(p)];
|
|
5234
|
+
out[camel(p)] = x;
|
|
5235
|
+
}
|
|
5236
|
+
});
|
|
5237
|
+
if (!changed) return;
|
|
5238
|
+
for (const k of Object.keys(b)) delete b[k];
|
|
5239
|
+
Object.assign(b, out);
|
|
5240
|
+
}
|
|
5241
|
+
|
|
5242
|
+
/**
|
|
5243
|
+
* A two-sided (start/end) or four-sided (top/right/bottom/left) longhand's
|
|
5244
|
+
* value, as each side's property and value by the shorthand rules; null for
|
|
5245
|
+
* any other property.
|
|
5246
|
+
* @param {string} prop kebab-case
|
|
5247
|
+
* @param {string} v
|
|
5248
|
+
* @returns {Record<string, string> | null}
|
|
5249
|
+
*/
|
|
5250
|
+
function sidesOf(prop, v) {
|
|
5251
|
+
const direct = [...longhandsOf(prop)].filter(p => longhandsOf(p).size === 0);
|
|
5252
|
+
const w = splitTop(v.trim(), ' ').map(x => x.trim());
|
|
5253
|
+
if (!w.length || /!important/i.test(v)) return null;
|
|
5254
|
+
const start = direct.find(p => /-start(-|$)/.test(p));
|
|
5255
|
+
const end = direct.find(p => /-end(-|$)/.test(p));
|
|
5256
|
+
if (direct.length === 2 && start && end && w.length <= 2)
|
|
5257
|
+
return {[start]: w[0], [end]: w[1] ?? w[0]};
|
|
5258
|
+
const named = BOX_SIDES.map(side =>
|
|
5259
|
+
direct.find(p => new RegExp(`-${side}(-|$)`).test(p)),
|
|
5260
|
+
);
|
|
5261
|
+
if (direct.length !== 4 || named.some(x => !x) || w.length > 4) return null;
|
|
5262
|
+
const [t, r = t, bo = t, l = r] = w;
|
|
5263
|
+
const vals = [t, r, bo, l];
|
|
5264
|
+
return Object.fromEntries(
|
|
5265
|
+
named.map((p, i) => [/** @type {string} */ (p), vals[i]]),
|
|
5266
|
+
);
|
|
5267
|
+
}
|
|
5268
|
+
|
|
3206
5269
|
/** Core's default type scale ratio. */
|
|
3207
5270
|
const STANDARD_RATIO = 1.2;
|
|
3208
5271
|
/** The font-size tokens below the base, with their step on the scale. */
|
|
@@ -3314,16 +5377,16 @@ export function maskedSettings(data, ctx = null) {
|
|
|
3314
5377
|
* {font-weight}`. Find those rules in the built stylesheet.
|
|
3315
5378
|
* @param {string} cssText
|
|
3316
5379
|
* @param {any} theme
|
|
3317
|
-
* @returns {Array<{target: string, key: string, prop: string, value: string}>}
|
|
5380
|
+
* @returns {Array<{target: string, key: string, prop: string, value: string, state?: string}>}
|
|
3318
5381
|
*/
|
|
3319
5382
|
export function findOverrides(cssText, theme) {
|
|
3320
|
-
/** @type {Array<{target: string, key: string, prop: string, value: string}>} */
|
|
5383
|
+
/** @type {Array<{target: string, key: string, prop: string, value: string, state?: string}>} */
|
|
3321
5384
|
const extra = [];
|
|
3322
5385
|
for (const [target, keys] of Object.entries(
|
|
3323
5386
|
/** @type {Record<string, any>} */ (theme.components ?? {}),
|
|
3324
5387
|
)) {
|
|
3325
5388
|
const props = Object.keys(keys.base ?? {}).filter(
|
|
3326
|
-
p => !p.startsWith('--') && !p.startsWith(':'),
|
|
5389
|
+
p => !p.startsWith('--') && !p.startsWith(':') && !p.startsWith('@'),
|
|
3327
5390
|
);
|
|
3328
5391
|
if (!props.length) continue;
|
|
3329
5392
|
const re = new RegExp(
|
|
@@ -3362,6 +5425,51 @@ export function findOverrides(cssText, theme) {
|
|
|
3362
5425
|
}
|
|
3363
5426
|
}
|
|
3364
5427
|
}
|
|
5428
|
+
// A state setting on the plain part (a whole-part hover) loses the same
|
|
5429
|
+
// way to a variant's own rule in that state: it is repeated there too.
|
|
5430
|
+
for (const [target, keys] of Object.entries(
|
|
5431
|
+
/** @type {Record<string, any>} */ (theme.components ?? {}),
|
|
5432
|
+
))
|
|
5433
|
+
for (const [state, decl] of Object.entries(keys.base ?? {})) {
|
|
5434
|
+
if (!/^:[a-z-]+$/.test(state) || !decl || typeof decl !== 'object')
|
|
5435
|
+
continue;
|
|
5436
|
+
const props = Object.keys(decl).filter(
|
|
5437
|
+
p => !p.startsWith('--') && !p.startsWith(':'),
|
|
5438
|
+
);
|
|
5439
|
+
if (!props.length) continue;
|
|
5440
|
+
const re = new RegExp(
|
|
5441
|
+
`\\.astryx-${target.replace(/-/g, '\\-')}\\[data-([a-z-]+)="([^"]+)"\\](?:${state}(?![\\w-])[^{}]*|\\s*)\\{([^}]*)\\}`,
|
|
5442
|
+
'g',
|
|
5443
|
+
);
|
|
5444
|
+
// The variant's own rule in that state, and its resting rule: that
|
|
5445
|
+
// one comes after the plain state rule at the same weight.
|
|
5446
|
+
const seen = new Set();
|
|
5447
|
+
for (const m of cssText.matchAll(re)) {
|
|
5448
|
+
const declared = [...m[3].matchAll(/(?:^|[;\s])([a-z-]+)\s*:/g)].map(
|
|
5449
|
+
d => d[1],
|
|
5450
|
+
);
|
|
5451
|
+
for (const prop of props) {
|
|
5452
|
+
const css = kebab(prop);
|
|
5453
|
+
if (css === m[1] || css.endsWith(`-${m[1]}`)) continue;
|
|
5454
|
+
if (
|
|
5455
|
+
!seen.has(`${m[1]}:${m[2]} ${prop}`) &&
|
|
5456
|
+
declared.some(
|
|
5457
|
+
d =>
|
|
5458
|
+
d === css || coversProperty(d, css) || coversProperty(css, d),
|
|
5459
|
+
)
|
|
5460
|
+
) {
|
|
5461
|
+
seen.add(`${m[1]}:${m[2]} ${prop}`);
|
|
5462
|
+
extra.push({
|
|
5463
|
+
target,
|
|
5464
|
+
key: `${m[1]}:${m[2]}`,
|
|
5465
|
+
state,
|
|
5466
|
+
prop,
|
|
5467
|
+
value: decl[prop],
|
|
5468
|
+
});
|
|
5469
|
+
}
|
|
5470
|
+
}
|
|
5471
|
+
}
|
|
5472
|
+
}
|
|
3365
5473
|
return extra;
|
|
3366
5474
|
}
|
|
3367
5475
|
|
|
@@ -3384,8 +5492,43 @@ const liftValue = (lift, only) =>
|
|
|
3384
5492
|
* @param {ThemeData} data
|
|
3385
5493
|
* @param {any} theme
|
|
3386
5494
|
* @param {import('./_palette.mjs').PaletteMapping | null} pal
|
|
5495
|
+
* @param {string} [cwd]
|
|
5496
|
+
* @param {Record<string, any> | null} [baseParts] the base theme's components, whose state objects each written one starts from
|
|
3387
5497
|
*/
|
|
3388
|
-
function settingsSource(slug, data, theme, pal, cwd = '') {
|
|
5498
|
+
function settingsSource(slug, data, theme, pal, cwd = '', baseParts = null) {
|
|
5499
|
+
// Core merges a theme's state object (`:hover` in a rule) over the base
|
|
5500
|
+
// theme's whole, one level deep: each one starts as the base theme's own
|
|
5501
|
+
// for that rule, so the properties no setting names keep the base's.
|
|
5502
|
+
if (baseParts) {
|
|
5503
|
+
theme = {...theme, components: {...(theme.components ?? {})}};
|
|
5504
|
+
for (const [target, keys] of Object.entries(theme.components)) {
|
|
5505
|
+
/** @type {Record<string, any>} */
|
|
5506
|
+
const next = {};
|
|
5507
|
+
for (const [key, rule] of Object.entries(
|
|
5508
|
+
/** @type {Record<string, any>} */ (keys),
|
|
5509
|
+
)) {
|
|
5510
|
+
if (!rule || typeof rule !== 'object') {
|
|
5511
|
+
next[key] = rule;
|
|
5512
|
+
continue;
|
|
5513
|
+
}
|
|
5514
|
+
/** @type {Record<string, any>} */
|
|
5515
|
+
const r = {...rule};
|
|
5516
|
+
for (const [k, v] of Object.entries(rule)) {
|
|
5517
|
+
const own = baseParts[target]?.[key]?.[k];
|
|
5518
|
+
if (
|
|
5519
|
+
k.startsWith(':') &&
|
|
5520
|
+
v &&
|
|
5521
|
+
typeof v === 'object' &&
|
|
5522
|
+
own &&
|
|
5523
|
+
typeof own === 'object'
|
|
5524
|
+
)
|
|
5525
|
+
r[k] = {...own, ...v};
|
|
5526
|
+
}
|
|
5527
|
+
next[key] = r;
|
|
5528
|
+
}
|
|
5529
|
+
theme.components[target] = next;
|
|
5530
|
+
}
|
|
5531
|
+
}
|
|
3389
5532
|
const like = data.like;
|
|
3390
5533
|
const REF = '@@palette:';
|
|
3391
5534
|
/** @param {string} family @param {'light' | 'dark'} m @param {import('./_palette.mjs').StopRef} r */
|
|
@@ -3393,7 +5536,7 @@ function settingsSource(slug, data, theme, pal, cwd = '') {
|
|
|
3393
5536
|
r.family === 'black' || r.family === 'white'
|
|
3394
5537
|
? `${REF}${r.family}`
|
|
3395
5538
|
: `${REF}${r.family ?? family}.${m}.${r.stop}${r.alpha ? `+${r.alpha}` : ''}`;
|
|
3396
|
-
const out = {...theme, name:
|
|
5539
|
+
const out = {...theme, name: settingsThemeName(slug)};
|
|
3397
5540
|
// A one-mode theme keeps both halves too (see compileTheme).
|
|
3398
5541
|
if (pal && theme.tokens) {
|
|
3399
5542
|
out.tokens = {...theme.tokens};
|
|
@@ -3637,32 +5780,217 @@ function themeModuleSource(slug) {
|
|
|
3637
5780
|
export const themeSource = slug => themeModuleSource(slug);
|
|
3638
5781
|
|
|
3639
5782
|
/**
|
|
3640
|
-
*
|
|
3641
|
-
*
|
|
3642
|
-
|
|
5783
|
+
* The name of the theme a settings module defines.
|
|
5784
|
+
* @param {string} slug
|
|
5785
|
+
*/
|
|
5786
|
+
export const settingsThemeName = slug => `${slug}-settings`;
|
|
5787
|
+
|
|
5788
|
+
/**
|
|
5789
|
+
* Why a theme's source cannot carry its settings, with the step that puts
|
|
5790
|
+
* it right; null when it carries them. The theme is the export a build of
|
|
5791
|
+
* the source reads, loaded as the build loads it: it must be named for the
|
|
5792
|
+
* theme, and the settings module's own export must be in the chain of
|
|
5793
|
+
* themes it extends (after every spread and repeated key), the very object,
|
|
5794
|
+
* not a theme named like it. Null for a source that does not parse (its
|
|
5795
|
+
* parse error is its own finding), and for one that imports the settings
|
|
5796
|
+
* but does not load (outputsDrift and the build report that).
|
|
3643
5797
|
* @param {string} cwd
|
|
3644
5798
|
* @param {string} slug
|
|
3645
|
-
* @returns {string | null}
|
|
5799
|
+
* @returns {Promise<{problem: string, fix: string} | null>}
|
|
3646
5800
|
*/
|
|
3647
|
-
export function themeSourceProblem(cwd, slug) {
|
|
5801
|
+
export async function themeSourceProblem(cwd, slug) {
|
|
3648
5802
|
const f = themeFiles(cwd, slug);
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
.
|
|
3653
|
-
|
|
5803
|
+
/** @type {string} */
|
|
5804
|
+
let code;
|
|
5805
|
+
try {
|
|
5806
|
+
code = fs.readFileSync(f.ts, 'utf8');
|
|
5807
|
+
} catch {
|
|
5808
|
+
return null;
|
|
5809
|
+
}
|
|
5810
|
+
/** @type {any} */
|
|
5811
|
+
let ast;
|
|
5812
|
+
try {
|
|
5813
|
+
ast = parseModule(code, {sourceType: 'module', plugins: ['typescript']});
|
|
5814
|
+
} catch {
|
|
5815
|
+
return null;
|
|
5816
|
+
}
|
|
3654
5817
|
const settings = `${camel(slug)}Settings`;
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
5818
|
+
const putBack = `Put back the import of ${settings} from './${slug}.settings', extends: ${settings} and name: '${slug}' in themes/${slug}/${slug}.theme.ts (your own changes stay below them).`;
|
|
5819
|
+
const fromSettings = new RegExp(`^\\./${slug}\\.settings(\\.[jt]s)?$`);
|
|
5820
|
+
const importsSettings = ast.program.body.some(
|
|
5821
|
+
(/** @type {any} */ node) =>
|
|
5822
|
+
node.type === 'ImportDeclaration' && fromSettings.test(node.source.value),
|
|
5823
|
+
);
|
|
5824
|
+
const notImported = {
|
|
5825
|
+
problem: `${slug}.theme.ts does not import ./${slug}.settings`,
|
|
5826
|
+
fix: putBack,
|
|
5827
|
+
};
|
|
5828
|
+
/** @type {{theme: any, exportName: string, extendsBase?: boolean}} */
|
|
5829
|
+
let loaded;
|
|
5830
|
+
try {
|
|
5831
|
+
loaded = await loadThemeExport(f.ts, {
|
|
5832
|
+
cwd,
|
|
5833
|
+
// The settings module's own export, by identity: a theme merely
|
|
5834
|
+
// named like it is not it.
|
|
5835
|
+
base: {
|
|
5836
|
+
file: path.join(f.dir, `${slug}.settings.ts`),
|
|
5837
|
+
exportName: settings,
|
|
5838
|
+
specifiers: ['', '.ts', '.js'].map(x => `./${slug}.settings${x}`),
|
|
5839
|
+
},
|
|
5840
|
+
});
|
|
5841
|
+
} catch {
|
|
5842
|
+
// A source that does not load is the build's own finding
|
|
5843
|
+
// (outputsDrift and every build report it); one that does not import
|
|
5844
|
+
// the settings says that first.
|
|
5845
|
+
return importsSettings ? null : notImported;
|
|
5846
|
+
}
|
|
5847
|
+
if (loaded.theme?.name !== slug) {
|
|
5848
|
+
// Another export of the file names the theme: the build reads a
|
|
5849
|
+
// different one, so say which, and how to make it read the theme.
|
|
5850
|
+
const other = exportedNames(ast).find(
|
|
5851
|
+
name =>
|
|
5852
|
+
name !== loaded.exportName &&
|
|
5853
|
+
plainText(
|
|
5854
|
+
unwrapConst(
|
|
5855
|
+
ast,
|
|
5856
|
+
exportedDefinition(ast, name)?.properties.find(
|
|
5857
|
+
(/** @type {any} */ p) =>
|
|
5858
|
+
p.type === 'ObjectProperty' &&
|
|
5859
|
+
(p.key.name ?? p.key.value) === 'name',
|
|
5860
|
+
)?.value,
|
|
5861
|
+
),
|
|
5862
|
+
) === slug,
|
|
5863
|
+
);
|
|
5864
|
+
return other
|
|
5865
|
+
? {
|
|
5866
|
+
problem: `a build of ${slug}.theme.ts reads its export ${loaded.exportName} (the theme '${loaded.theme?.name}'), not ${other}, the theme named '${slug}'`,
|
|
5867
|
+
fix: `Make ${other} the theme a build reads: export it as the default (export default ${other}), or move the other theme to a file of its own.`,
|
|
5868
|
+
}
|
|
5869
|
+
: {
|
|
5870
|
+
problem: `${slug}.theme.ts no longer names the theme '${slug}'`,
|
|
5871
|
+
fix: putBack,
|
|
5872
|
+
};
|
|
5873
|
+
}
|
|
5874
|
+
if (!loaded.extendsBase)
|
|
5875
|
+
return importsSettings
|
|
5876
|
+
? {
|
|
5877
|
+
problem: `${slug}.theme.ts no longer extends ${settings}`,
|
|
5878
|
+
fix: putBack,
|
|
5879
|
+
}
|
|
5880
|
+
: notImported;
|
|
3663
5881
|
return null;
|
|
3664
5882
|
}
|
|
3665
5883
|
|
|
5884
|
+
/**
|
|
5885
|
+
* A plain text value: a string, or a template with no expressions.
|
|
5886
|
+
* @param {any} node
|
|
5887
|
+
* @returns {string | null}
|
|
5888
|
+
*/
|
|
5889
|
+
function plainText(node) {
|
|
5890
|
+
return node?.type === 'StringLiteral'
|
|
5891
|
+
? node.value
|
|
5892
|
+
: node?.type === 'TemplateLiteral' && node.expressions.length === 0
|
|
5893
|
+
? node.quasis.map((/** @type {any} */ q) => q.value.cooked).join('')
|
|
5894
|
+
: null;
|
|
5895
|
+
}
|
|
5896
|
+
|
|
5897
|
+
/**
|
|
5898
|
+
* A module's top-level const bindings, by name.
|
|
5899
|
+
* @param {any} ast
|
|
5900
|
+
* @returns {Map<string, any>}
|
|
5901
|
+
*/
|
|
5902
|
+
function topConsts(ast) {
|
|
5903
|
+
/** @type {Map<string, any>} */
|
|
5904
|
+
const out = new Map();
|
|
5905
|
+
for (const node of ast.program.body) {
|
|
5906
|
+
const decl =
|
|
5907
|
+
node.type === 'ExportNamedDeclaration' ? node.declaration : node;
|
|
5908
|
+
if (decl?.type === 'VariableDeclaration')
|
|
5909
|
+
for (const d of decl.declarations)
|
|
5910
|
+
if (d.id.type === 'Identifier') out.set(d.id.name, d.init);
|
|
5911
|
+
}
|
|
5912
|
+
return out;
|
|
5913
|
+
}
|
|
5914
|
+
|
|
5915
|
+
/**
|
|
5916
|
+
* An expression with TypeScript wrappers and parentheses taken off, and a
|
|
5917
|
+
* name read through to the top-level const it names. An imported name stays
|
|
5918
|
+
* the identifier.
|
|
5919
|
+
* @param {any} ast
|
|
5920
|
+
* @param {any} node
|
|
5921
|
+
* @returns {any}
|
|
5922
|
+
*/
|
|
5923
|
+
function unwrapConst(ast, node) {
|
|
5924
|
+
const consts = topConsts(ast);
|
|
5925
|
+
for (let depth = 0; node && depth < 20; depth++) {
|
|
5926
|
+
if (
|
|
5927
|
+
[
|
|
5928
|
+
'TSAsExpression',
|
|
5929
|
+
'TSSatisfiesExpression',
|
|
5930
|
+
'TSNonNullExpression',
|
|
5931
|
+
'TSTypeAssertion',
|
|
5932
|
+
'ParenthesizedExpression',
|
|
5933
|
+
].includes(node.type)
|
|
5934
|
+
)
|
|
5935
|
+
node = node.expression;
|
|
5936
|
+
else if (node.type === 'Identifier' && consts.get(node.name))
|
|
5937
|
+
node = consts.get(node.name);
|
|
5938
|
+
else return node;
|
|
5939
|
+
}
|
|
5940
|
+
return node;
|
|
5941
|
+
}
|
|
5942
|
+
|
|
5943
|
+
/**
|
|
5944
|
+
* The names a module exports, `default` included.
|
|
5945
|
+
* @param {any} ast
|
|
5946
|
+
* @returns {string[]}
|
|
5947
|
+
*/
|
|
5948
|
+
function exportedNames(ast) {
|
|
5949
|
+
/** @type {string[]} */
|
|
5950
|
+
const names = [];
|
|
5951
|
+
for (const node of ast.program.body) {
|
|
5952
|
+
if (node.type === 'ExportDefaultDeclaration') names.push('default');
|
|
5953
|
+
if (node.type !== 'ExportNamedDeclaration' || node.source) continue;
|
|
5954
|
+
if (node.declaration?.type === 'VariableDeclaration')
|
|
5955
|
+
for (const d of node.declaration.declarations)
|
|
5956
|
+
if (d.id.type === 'Identifier') names.push(d.id.name);
|
|
5957
|
+
for (const spec of node.specifiers ?? [])
|
|
5958
|
+
names.push(spec.exported.name ?? spec.exported.value);
|
|
5959
|
+
}
|
|
5960
|
+
return names;
|
|
5961
|
+
}
|
|
5962
|
+
|
|
5963
|
+
/**
|
|
5964
|
+
* The object a module export defines its theme with: the argument of the
|
|
5965
|
+
* `defineTheme(...)` call, or the object itself, that the export names.
|
|
5966
|
+
* @param {any} ast
|
|
5967
|
+
* @param {string} exportName
|
|
5968
|
+
* @returns {any} an ObjectExpression, or null
|
|
5969
|
+
*/
|
|
5970
|
+
function exportedDefinition(ast, exportName) {
|
|
5971
|
+
/** @type {any} */
|
|
5972
|
+
let value = null;
|
|
5973
|
+
for (const node of ast.program.body) {
|
|
5974
|
+
if (node.type === 'ExportDefaultDeclaration' && exportName === 'default')
|
|
5975
|
+
value = node.declaration;
|
|
5976
|
+
if (node.type !== 'ExportNamedDeclaration') continue;
|
|
5977
|
+
if (node.declaration?.type === 'VariableDeclaration')
|
|
5978
|
+
for (const d of node.declaration.declarations)
|
|
5979
|
+
if (d.id.type === 'Identifier' && d.id.name === exportName)
|
|
5980
|
+
value = d.init;
|
|
5981
|
+
for (const spec of node.specifiers ?? [])
|
|
5982
|
+
if (
|
|
5983
|
+
!node.source &&
|
|
5984
|
+
(spec.exported.name ?? spec.exported.value) === exportName
|
|
5985
|
+
)
|
|
5986
|
+
value = spec.local;
|
|
5987
|
+
}
|
|
5988
|
+
value = unwrapConst(ast, value);
|
|
5989
|
+
if (value?.type === 'CallExpression')
|
|
5990
|
+
value = unwrapConst(ast, value.arguments[0]);
|
|
5991
|
+
return value?.type === 'ObjectExpression' ? value : null;
|
|
5992
|
+
}
|
|
5993
|
+
|
|
3666
5994
|
/**
|
|
3667
5995
|
* Why the person's own theme source does not parse (its line and the
|
|
3668
5996
|
* parser's message), or null when it parses or is not there.
|
|
@@ -3692,103 +6020,17 @@ export function themeSourceParseError(cwd, slug) {
|
|
|
3692
6020
|
* would load the person's own theme source when it does not parse: the
|
|
3693
6021
|
* build would fail part way.
|
|
3694
6022
|
* @param {string} cwd
|
|
3695
|
-
* @param {string} slug
|
|
3696
|
-
*/
|
|
3697
|
-
export function refuseThemeParse(cwd, slug) {
|
|
3698
|
-
const why = themeSourceParseError(cwd, slug);
|
|
3699
|
-
if (why)
|
|
3700
|
-
throw new AstryxError(
|
|
3701
|
-
`${why}, so the theme cannot build. It is your own file: fix it, then run the command again. Nothing was changed.`,
|
|
3702
|
-
[
|
|
3703
|
-
{
|
|
3704
|
-
name: `cat ${path.posix.join('themes', slug, `${slug}.theme.ts`)}`,
|
|
3705
|
-
reason: 'see the file',
|
|
3706
|
-
},
|
|
3707
|
-
],
|
|
3708
|
-
ERROR_CODES.ERR_THEME_INVALID,
|
|
3709
|
-
);
|
|
3710
|
-
}
|
|
3711
|
-
|
|
3712
|
-
/**
|
|
3713
|
-
* Refuse a build whose theme source cannot carry the settings, naming the
|
|
3714
|
-
* lines that put it back.
|
|
3715
|
-
* @param {string} cwd
|
|
3716
|
-
* @param {string} slug
|
|
3717
|
-
*/
|
|
3718
|
-
export function refuseThemeSource(cwd, slug) {
|
|
3719
|
-
refuseThemeParse(cwd, slug);
|
|
3720
|
-
const why = themeSourceProblem(cwd, slug);
|
|
3721
|
-
if (why)
|
|
3722
|
-
throw new AstryxError(
|
|
3723
|
-
`${why}, so settings cannot take effect. Put back the import of ${camel(slug)}Settings from './${slug}.settings', extends: ${camel(slug)}Settings and name: '${slug}' in themes/${slug}/${slug}.theme.ts (your own changes stay below them). Nothing was changed.`,
|
|
3724
|
-
[{name: `${P} show ${slug}`, reason: 'see the theme as it is'}],
|
|
3725
|
-
ERROR_CODES.ERR_THEME_INVALID,
|
|
3726
|
-
);
|
|
3727
|
-
}
|
|
3728
|
-
|
|
3729
|
-
/**
|
|
3730
|
-
* Refuse a [light, dark] pair the theme's own source gives a token that is
|
|
3731
|
-
* not a color (a font family, a size, a weight): the build would write
|
|
3732
|
-
* `light-dark()`, which browsers take only for colors. The editor refuses
|
|
3733
|
-
* `--mode` on those tokens for the same reason.
|
|
3734
|
-
* @param {Ctx} ctx
|
|
3735
|
-
* @param {string} cwd
|
|
3736
|
-
* @param {string} slug
|
|
3737
|
-
*/
|
|
3738
|
-
function refuseOwnPairs(ctx, cwd, slug) {
|
|
3739
|
-
const f = themeFiles(cwd, slug);
|
|
3740
|
-
/** @type {string[]} */
|
|
3741
|
-
const bad = [];
|
|
3742
|
-
try {
|
|
3743
|
-
const ast = /** @type {any} */ (
|
|
3744
|
-
parseModule(fs.readFileSync(f.ts, 'utf8'), {
|
|
3745
|
-
sourceType: 'module',
|
|
3746
|
-
plugins: ['typescript'],
|
|
3747
|
-
})
|
|
3748
|
-
);
|
|
3749
|
-
/** @param {any} node */
|
|
3750
|
-
const visit = node => {
|
|
3751
|
-
if (!node || typeof node !== 'object') return;
|
|
3752
|
-
if (
|
|
3753
|
-
node.type === 'ObjectProperty' &&
|
|
3754
|
-
typeof (node.key?.value ?? node.key?.name) === 'string' &&
|
|
3755
|
-
String(node.key?.value ?? node.key?.name).startsWith('--') &&
|
|
3756
|
-
node.value?.type === 'ArrayExpression' &&
|
|
3757
|
-
node.value.elements.length === 2 &&
|
|
3758
|
-
// A pair holding a value that is not text is named by the value
|
|
3759
|
-
// checks, with the fix for that value.
|
|
3760
|
-
node.value.elements.every(
|
|
3761
|
-
(/** @type {any} */ e) =>
|
|
3762
|
-
e?.type === 'StringLiteral' || e?.type === 'TemplateLiteral',
|
|
3763
|
-
)
|
|
3764
|
-
) {
|
|
3765
|
-
const k = String(node.key.value ?? node.key.name);
|
|
3766
|
-
const type = ctx.C.typeOf(k);
|
|
3767
|
-
// A size given as a bare number is named by the value check, with
|
|
3768
|
-
// the unit it needs.
|
|
3769
|
-
const unitless =
|
|
3770
|
-
type === 'size' &&
|
|
3771
|
-
node.value.elements.some(
|
|
3772
|
-
(/** @type {any} */ e) =>
|
|
3773
|
-
e?.type === 'StringLiteral' && /^\d*\.?\d+$/.test(e.value.trim()),
|
|
3774
|
-
);
|
|
3775
|
-
if (type !== 'color' && !unitless) bad.push(k);
|
|
3776
|
-
}
|
|
3777
|
-
for (const v of Object.values(node))
|
|
3778
|
-
if (Array.isArray(v)) v.forEach(visit);
|
|
3779
|
-
else if (v && typeof v === 'object' && v.type) visit(v);
|
|
3780
|
-
};
|
|
3781
|
-
visit(ast.program);
|
|
3782
|
-
} catch {
|
|
3783
|
-
return;
|
|
3784
|
-
}
|
|
3785
|
-
if (bad.length)
|
|
6023
|
+
* @param {string} slug
|
|
6024
|
+
*/
|
|
6025
|
+
export function refuseThemeParse(cwd, slug) {
|
|
6026
|
+
const why = themeSourceParseError(cwd, slug);
|
|
6027
|
+
if (why)
|
|
3786
6028
|
throw new AstryxError(
|
|
3787
|
-
|
|
6029
|
+
`${why}, so the theme cannot build. It is your own file: fix it, then run the command again. Nothing was changed.`,
|
|
3788
6030
|
[
|
|
3789
6031
|
{
|
|
3790
|
-
name: `
|
|
3791
|
-
reason: 'see the
|
|
6032
|
+
name: `cat ${path.posix.join('themes', slug, `${slug}.theme.ts`)}`,
|
|
6033
|
+
reason: 'see the file',
|
|
3792
6034
|
},
|
|
3793
6035
|
],
|
|
3794
6036
|
ERROR_CODES.ERR_THEME_INVALID,
|
|
@@ -3796,48 +6038,59 @@ function refuseOwnPairs(ctx, cwd, slug) {
|
|
|
3796
6038
|
}
|
|
3797
6039
|
|
|
3798
6040
|
/**
|
|
3799
|
-
*
|
|
3800
|
-
*
|
|
3801
|
-
*
|
|
3802
|
-
*
|
|
3803
|
-
|
|
6041
|
+
* Refuse a build whose theme source cannot carry the settings, naming the
|
|
6042
|
+
* lines that put it back.
|
|
6043
|
+
* @param {string} cwd
|
|
6044
|
+
* @param {string} slug
|
|
6045
|
+
*/
|
|
6046
|
+
export async function refuseThemeSource(cwd, slug) {
|
|
6047
|
+
refuseThemeParse(cwd, slug);
|
|
6048
|
+
const refusal = await themeSourceRefusal(cwd, slug);
|
|
6049
|
+
if (refusal) throw refusal;
|
|
6050
|
+
}
|
|
6051
|
+
|
|
6052
|
+
/**
|
|
6053
|
+
* The refusal for a theme source that cannot carry the settings (it no
|
|
6054
|
+
* longer imports or extends them, or names another theme), naming the
|
|
6055
|
+
* lines that put it back; null when it carries them. Every command that
|
|
6056
|
+
* builds the theme refuses with it.
|
|
6057
|
+
* @param {string} cwd
|
|
6058
|
+
* @param {string} slug
|
|
6059
|
+
* @returns {Promise<AstryxError | null>}
|
|
6060
|
+
*/
|
|
6061
|
+
export async function themeSourceRefusal(cwd, slug) {
|
|
6062
|
+
const why = await themeSourceProblem(cwd, slug);
|
|
6063
|
+
return why
|
|
6064
|
+
? new AstryxError(
|
|
6065
|
+
`${why.problem}, so settings cannot take effect. ${why.fix} Nothing was changed.`,
|
|
6066
|
+
[{name: `${P} show ${slug}`, reason: 'see the theme as it is'}],
|
|
6067
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
6068
|
+
)
|
|
6069
|
+
: null;
|
|
6070
|
+
}
|
|
6071
|
+
|
|
6072
|
+
/**
|
|
6073
|
+
* The input the theme's own source gives `defineTheme()`, evaluated as a
|
|
6074
|
+
* build evaluates it, with the theme Core made of it (or Core's error when
|
|
6075
|
+
* it refuses the input); null when the source does not load.
|
|
3804
6076
|
* @param {Ctx} ctx
|
|
3805
6077
|
* @param {string} slug
|
|
3806
|
-
* @
|
|
6078
|
+
* @param {string | null} like the base theme, whose built tokens are `base`
|
|
6079
|
+
* @returns {Promise<{input: any, theme: any, error: Error | null, base: Map<string, string | null>} | null>}
|
|
3807
6080
|
*/
|
|
3808
|
-
export async function
|
|
6081
|
+
export async function ownThemeInput(ctx, slug, like) {
|
|
3809
6082
|
const {cwd} = ctx.project;
|
|
3810
|
-
const
|
|
3811
|
-
// The build writes only inside the project: the cache folder when it is
|
|
3812
|
-
// there, else a hidden folder in the theme's (removed after).
|
|
3813
|
-
const name = `.${slug}.pairs-${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
|
|
3814
|
-
const inside = !path.relative(cwd, ctx.project.cacheDir).startsWith('..');
|
|
3815
|
-
const dir = path.join(inside ? ctx.project.cacheDir : f.dir, name);
|
|
3816
|
-
const silent = logger.silent;
|
|
3817
|
-
logger.setSilent(true);
|
|
6083
|
+
const baseFile = like ? baseModule(cwd, like) : null;
|
|
3818
6084
|
try {
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
{
|
|
6085
|
+
const loaded = await loadThemeInput(themeFiles(cwd, slug).ts, {
|
|
6086
|
+
cwd,
|
|
6087
|
+
name: slug,
|
|
6088
|
+
});
|
|
6089
|
+
return (
|
|
6090
|
+
loaded && {...loaded, base: baseFile ? topTokensOf(baseFile) : new Map()}
|
|
3825
6091
|
);
|
|
3826
|
-
const js = fs
|
|
3827
|
-
.readdirSync(dir)
|
|
3828
|
-
.find(n => n.endsWith('.js') && !n.endsWith('.d.js'));
|
|
3829
|
-
if (!js) return [];
|
|
3830
|
-
return [...topTokensOf(path.join(dir, js))]
|
|
3831
|
-
.filter(
|
|
3832
|
-
([k, v]) =>
|
|
3833
|
-
/^light-dark\(/.test(String(v ?? '')) && ctx.C.typeOf(k) !== 'color',
|
|
3834
|
-
)
|
|
3835
|
-
.map(([k]) => k);
|
|
3836
6092
|
} catch {
|
|
3837
|
-
return
|
|
3838
|
-
} finally {
|
|
3839
|
-
logger.setSilent(silent);
|
|
3840
|
-
fs.rmSync(dir, {recursive: true, force: true});
|
|
6093
|
+
return null;
|
|
3841
6094
|
}
|
|
3842
6095
|
}
|
|
3843
6096
|
|
|
@@ -3850,6 +6103,21 @@ export function ownPairMessage(slug, names) {
|
|
|
3850
6103
|
return `themes/${slug}/${slug}.theme.ts gives ${names.join(', ')} a [light, dark] pair, but only colors differ by mode: the build would write light-dark(), which browsers take only for colors. Give ${names.length > 1 ? 'each' : 'it'} one value in ${slug}.theme.ts. Nothing was changed.`;
|
|
3851
6104
|
}
|
|
3852
6105
|
|
|
6106
|
+
/**
|
|
6107
|
+
* The check of a theme's own token values, run before every build, set by
|
|
6108
|
+
* the editor (which owns the checks a value meets): null until it is set.
|
|
6109
|
+
* @type {((ctx: Ctx, slug: string, data: ThemeData) => Promise<void>) | null}
|
|
6110
|
+
*/
|
|
6111
|
+
let ownTokenCheck = null;
|
|
6112
|
+
|
|
6113
|
+
/**
|
|
6114
|
+
* Set the check of a theme's own token values run before every build.
|
|
6115
|
+
* @param {((ctx: Ctx, slug: string, data: ThemeData) => Promise<void>) | null} fn
|
|
6116
|
+
*/
|
|
6117
|
+
export function setOwnTokenCheck(fn) {
|
|
6118
|
+
ownTokenCheck = fn;
|
|
6119
|
+
}
|
|
6120
|
+
|
|
3853
6121
|
/**
|
|
3854
6122
|
* What runs after every build of a theme, set by the adapter (the app copy
|
|
3855
6123
|
* of the theme): null until it is set.
|
|
@@ -3888,13 +6156,15 @@ export function holdAfterBuild(ctx) {
|
|
|
3888
6156
|
* @param {Ctx} ctx
|
|
3889
6157
|
* @param {string} slug
|
|
3890
6158
|
* @param {ThemeData} data
|
|
3891
|
-
* @returns {Promise<{warnings: string[], lifted: string[], notes: string[]}>} `notes`: what the color config kept or replaced of a hand edit
|
|
6159
|
+
* @returns {Promise<{warnings: string[], lifted: string[], notes: string[], removedPictures: string[]}>} `notes`: what the color config kept or replaced of a hand edit; `removedPictures`: the editor's pictures of the look before, removed
|
|
3892
6160
|
*/
|
|
3893
6161
|
export async function buildTheme(ctx, slug, data) {
|
|
3894
6162
|
const {cwd} = ctx.project;
|
|
3895
6163
|
const f = themeFiles(cwd, slug);
|
|
3896
|
-
refuseThemeSource(cwd, slug);
|
|
3897
|
-
|
|
6164
|
+
await refuseThemeSource(cwd, slug);
|
|
6165
|
+
// The theme's own token values, judged as a build of the source writes
|
|
6166
|
+
// them, before anything is written.
|
|
6167
|
+
await ownTokenCheck?.(ctx, slug, data);
|
|
3898
6168
|
if (data.like) baseThemeFile(ctx.project, data.like);
|
|
3899
6169
|
const base = await baseResolved(ctx.project, data.like);
|
|
3900
6170
|
fs.mkdirSync(f.dir, {recursive: true});
|
|
@@ -3933,7 +6203,17 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
3933
6203
|
ctx.keepStops,
|
|
3934
6204
|
);
|
|
3935
6205
|
await step(() => {
|
|
3936
|
-
writeWhole(
|
|
6206
|
+
writeWhole(
|
|
6207
|
+
f.settingsTs,
|
|
6208
|
+
settingsSource(
|
|
6209
|
+
slug,
|
|
6210
|
+
data,
|
|
6211
|
+
theme,
|
|
6212
|
+
pal,
|
|
6213
|
+
cwd,
|
|
6214
|
+
/** @type {any} */ (base).components ?? null,
|
|
6215
|
+
),
|
|
6216
|
+
);
|
|
3937
6217
|
syncPaletteRefs(cwd, slug, fs.readFileSync(f.settingsTs, 'utf8'), pal);
|
|
3938
6218
|
if (!fs.existsSync(f.ts)) writeWhole(f.ts, themeModuleSource(slug));
|
|
3939
6219
|
});
|
|
@@ -3942,16 +6222,23 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
3942
6222
|
// Only a picture the editor recorded writing, unchanged since, goes: a
|
|
3943
6223
|
// file of the user's is never touched. Section sheets stay, marked as
|
|
3944
6224
|
// out of date like sheet.html.
|
|
6225
|
+
/** The pictures removed: named in the result. @type {string[]} */
|
|
6226
|
+
let removedPictures = [];
|
|
3945
6227
|
await step(() => {
|
|
6228
|
+
// The full sheet's and the preview's pictures, in each mode; a section
|
|
6229
|
+
// sheet's (`sheet-inputs.png`) is not one of them.
|
|
3946
6230
|
const pictures = fs.existsSync(f.dir)
|
|
3947
6231
|
? fs
|
|
3948
6232
|
.readdirSync(f.dir)
|
|
3949
6233
|
.filter(
|
|
3950
6234
|
n =>
|
|
3951
|
-
/^(sheet|preview)(-
|
|
3952
|
-
|
|
6235
|
+
/^(sheet|preview)(-dark)?\.png$/.test(n) &&
|
|
6236
|
+
editorRecorded(cwd, slug, path.join(f.dir, n)),
|
|
3953
6237
|
)
|
|
3954
6238
|
: [];
|
|
6239
|
+
removedPictures = pictures.map(n =>
|
|
6240
|
+
path.relative(cwd, path.join(f.dir, n)).split(path.sep).join('/'),
|
|
6241
|
+
);
|
|
3955
6242
|
for (const file of [
|
|
3956
6243
|
path.join(f.dir, `${slug}.variants.d.ts`),
|
|
3957
6244
|
...pictures.map(n => path.join(f.dir, n)),
|
|
@@ -3959,9 +6246,47 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
3959
6246
|
fs.rmSync(file, {force: true});
|
|
3960
6247
|
forgetWritten(cwd, slug, pictures);
|
|
3961
6248
|
});
|
|
3962
|
-
let receipt = await step(() => runBuild(ctx, slug));
|
|
6249
|
+
let receipt = await step(() => runBuild(ctx, slug, data));
|
|
6250
|
+
// A state setting on the plain part that the own source's same state
|
|
6251
|
+
// rule wins over is not repeated on the variants either: the own source
|
|
6252
|
+
// wins on every variant, as the result says.
|
|
6253
|
+
/** @type {Map<string, Map<string, string | null>>} */
|
|
6254
|
+
const ownStates = new Map();
|
|
6255
|
+
/** @param {string} target @param {string} state @param {string} prop */
|
|
6256
|
+
const ownHolds = (target, state, prop) => {
|
|
6257
|
+
const k = `${target} ${state}`;
|
|
6258
|
+
const own =
|
|
6259
|
+
ownStates.get(k) ?? ownPartTokens(cwd, slug, target, 'base', state);
|
|
6260
|
+
ownStates.set(k, own);
|
|
6261
|
+
const css = kebab(prop);
|
|
6262
|
+
return [...own.keys()].some(
|
|
6263
|
+
o => o === css || coversProperty(o, css) || coversProperty(css, o),
|
|
6264
|
+
);
|
|
6265
|
+
};
|
|
3963
6266
|
const found = findOverrides(fs.readFileSync(f.css, 'utf8'), theme).filter(
|
|
3964
|
-
x =>
|
|
6267
|
+
x =>
|
|
6268
|
+
// By property: a variant whose rule the settings give this property
|
|
6269
|
+
// or a shorthand covering it needs no copy; nor one holding a
|
|
6270
|
+
// longhand of it that the base theme's rule also holds and that has
|
|
6271
|
+
// no sides to be written as (a background color, one-mode or not):
|
|
6272
|
+
// the copy would come after it there and win. Any other longhand
|
|
6273
|
+
// takes the copy, then is written after it (as its sides where the
|
|
6274
|
+
// base holds it).
|
|
6275
|
+
!Object.entries(
|
|
6276
|
+
(x.state
|
|
6277
|
+
? theme.components[x.target][x.key]?.[x.state]
|
|
6278
|
+
: theme.components[x.target][x.key]) ?? {},
|
|
6279
|
+
).some(([k, v]) => {
|
|
6280
|
+
if (k.startsWith(':')) return false;
|
|
6281
|
+
if (k === x.prop || coversProperty(kebab(k), kebab(x.prop)))
|
|
6282
|
+
return true;
|
|
6283
|
+
if (!coversProperty(kebab(x.prop), kebab(k))) return false;
|
|
6284
|
+
const baseRule = /** @type {any} */ (base).components?.[x.target]?.[
|
|
6285
|
+
x.key
|
|
6286
|
+
];
|
|
6287
|
+
const held = (x.state ? baseRule?.[x.state] : baseRule)?.[k];
|
|
6288
|
+
return held != null && sidesOf(kebab(k), String(v)) == null;
|
|
6289
|
+
}) && !(x.state && ownHolds(x.target, x.state, x.prop)),
|
|
3965
6290
|
);
|
|
3966
6291
|
/** @type {string[]} */
|
|
3967
6292
|
const lifted = [];
|
|
@@ -3972,19 +6297,42 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
3972
6297
|
// longhand or shorthand the variant sets itself (border-color after
|
|
3973
6298
|
// a base border) still wins over it.
|
|
3974
6299
|
const own = t[x.key] ?? {};
|
|
6300
|
+
const ownState = x.state ? (own[x.state] ?? {}) : own;
|
|
3975
6301
|
// A variant that sets every longhand of the property itself needs
|
|
3976
6302
|
// none of the base value (and a lifted radius would feed Core's copy
|
|
3977
6303
|
// of it into the content's radius, after the variant's own).
|
|
3978
6304
|
const longs = [...longhandsOf(kebab(x.prop))];
|
|
3979
|
-
if (longs.length && longs.every(l => camel(l) in
|
|
3980
|
-
t[x.key] =
|
|
3981
|
-
|
|
6305
|
+
if (longs.length && longs.every(l => camel(l) in ownState)) continue;
|
|
6306
|
+
t[x.key] = x.state
|
|
6307
|
+
? {...own, [x.state]: {[x.prop]: x.value, ...ownState}}
|
|
6308
|
+
: {[x.prop]: x.value, ...own};
|
|
6309
|
+
// The variant's own longhands come after the copied shorthand; one
|
|
6310
|
+
// the base theme holds would keep the base's earlier place and lose,
|
|
6311
|
+
// so it is written as its sides.
|
|
6312
|
+
const baseRule = /** @type {any} */ (base).components?.[x.target]?.[
|
|
6313
|
+
x.key
|
|
6314
|
+
];
|
|
6315
|
+
laterLonghandsAsSides(
|
|
6316
|
+
x.state ? t[x.key][x.state] : t[x.key],
|
|
6317
|
+
x.state ? baseRule?.[x.state] : baseRule,
|
|
6318
|
+
);
|
|
6319
|
+
lifted.push(`${x.target} ${x.key}${x.state ?? ''} ${kebab(x.prop)}`);
|
|
3982
6320
|
}
|
|
3983
6321
|
await step(() => {
|
|
3984
|
-
writeWhole(
|
|
6322
|
+
writeWhole(
|
|
6323
|
+
f.settingsTs,
|
|
6324
|
+
settingsSource(
|
|
6325
|
+
slug,
|
|
6326
|
+
data,
|
|
6327
|
+
theme,
|
|
6328
|
+
pal,
|
|
6329
|
+
cwd,
|
|
6330
|
+
/** @type {any} */ (base).components ?? null,
|
|
6331
|
+
),
|
|
6332
|
+
);
|
|
3985
6333
|
syncPaletteRefs(cwd, slug, fs.readFileSync(f.settingsTs, 'utf8'), pal);
|
|
3986
6334
|
});
|
|
3987
|
-
receipt = await step(() => runBuild(ctx, slug));
|
|
6335
|
+
receipt = await step(() => runBuild(ctx, slug, data));
|
|
3988
6336
|
}
|
|
3989
6337
|
for (const out of [f.css, f.js])
|
|
3990
6338
|
if (fs.existsSync(out))
|
|
@@ -4002,11 +6350,37 @@ export async function buildTheme(ctx, slug, data) {
|
|
|
4002
6350
|
await step(() => {
|
|
4003
6351
|
afterBuild?.(ctx, slug, data);
|
|
4004
6352
|
});
|
|
4005
|
-
return {warnings, lifted, notes};
|
|
6353
|
+
return {warnings, lifted, notes, removedPictures};
|
|
6354
|
+
}
|
|
6355
|
+
|
|
6356
|
+
/**
|
|
6357
|
+
* The one color mode of a light-only or dark-only theme, from its settings,
|
|
6358
|
+
* for the build to write into the built module (`colorModes`); null for a
|
|
6359
|
+
* theme with both modes.
|
|
6360
|
+
* @param {string} cwd
|
|
6361
|
+
* @param {string} slug
|
|
6362
|
+
* @returns {['light'] | ['dark'] | null}
|
|
6363
|
+
*/
|
|
6364
|
+
export function buildColorModes(cwd, slug) {
|
|
6365
|
+
try {
|
|
6366
|
+
return oneColorMode(
|
|
6367
|
+
modesOf(
|
|
6368
|
+
settingsJson(fs.readFileSync(themeFiles(cwd, slug).settings, 'utf8')),
|
|
6369
|
+
),
|
|
6370
|
+
);
|
|
6371
|
+
} catch {
|
|
6372
|
+
return null;
|
|
6373
|
+
}
|
|
4006
6374
|
}
|
|
4007
6375
|
|
|
4008
|
-
/**
|
|
4009
|
-
|
|
6376
|
+
/**
|
|
6377
|
+
* @param {Ctx} ctx
|
|
6378
|
+
* @param {string} slug
|
|
6379
|
+
* @param {{mode?: string}} [data] the settings being built (saved after the
|
|
6380
|
+
* build), whose mode the built module exports
|
|
6381
|
+
* @returns {Promise<any>}
|
|
6382
|
+
*/
|
|
6383
|
+
async function runBuild(ctx, slug, data) {
|
|
4010
6384
|
const {cwd} = ctx.project;
|
|
4011
6385
|
const f = themeFiles(cwd, slug);
|
|
4012
6386
|
const silent = logger.silent;
|
|
@@ -4020,6 +6394,11 @@ async function runBuild(ctx, slug) {
|
|
|
4020
6394
|
{},
|
|
4021
6395
|
{cwd},
|
|
4022
6396
|
{compact: true},
|
|
6397
|
+
{
|
|
6398
|
+
colorModes: data
|
|
6399
|
+
? oneColorMode(modesOf(data))
|
|
6400
|
+
: buildColorModes(cwd, slug),
|
|
6401
|
+
},
|
|
4023
6402
|
);
|
|
4024
6403
|
return receipt;
|
|
4025
6404
|
} finally {
|
|
@@ -4046,6 +6425,7 @@ export async function outputsDrift(ctx, slug) {
|
|
|
4046
6425
|
{check: true},
|
|
4047
6426
|
{cwd},
|
|
4048
6427
|
{compact: true},
|
|
6428
|
+
{colorModes: buildColorModes(cwd, slug)},
|
|
4049
6429
|
);
|
|
4050
6430
|
const first = /** @type {any} */ (receipt)?.data?.stale?.[0];
|
|
4051
6431
|
if (!first) return null;
|
|
@@ -4054,11 +6434,10 @@ export async function outputsDrift(ctx, slug) {
|
|
|
4054
6434
|
? `${name} was missing`
|
|
4055
6435
|
: `${name} differs from what the settings build, after an edit by hand or by another tool`;
|
|
4056
6436
|
} catch (e) {
|
|
4057
|
-
//
|
|
4058
|
-
//
|
|
6437
|
+
// The outputs could not be compared (the source does not load, or
|
|
6438
|
+
// anything else): the theme is built again, and a build that fails
|
|
6439
|
+
// says why. Nothing records the source as built.
|
|
4059
6440
|
const err = /** @type {any} */ (e);
|
|
4060
|
-
if (err instanceof AstryxError && err.code === ERROR_CODES.ERR_THEME_LOAD)
|
|
4061
|
-
return null;
|
|
4062
6441
|
return `the outputs could not be checked (${String(err?.message ?? e)
|
|
4063
6442
|
.split('\n')[0]
|
|
4064
6443
|
.slice(0, 160)})`;
|
|
@@ -4220,6 +6599,27 @@ function nonTextToken(t, typeOf = (/** @type {string} */ _k) => 'other') {
|
|
|
4220
6599
|
return null;
|
|
4221
6600
|
}
|
|
4222
6601
|
|
|
6602
|
+
/**
|
|
6603
|
+
* A value as a message names it, whatever it is: text in quotes, a number,
|
|
6604
|
+
* a BigInt with its `n`, a symbol, a function, an array or an object as
|
|
6605
|
+
* JSON where it has one. Never throws.
|
|
6606
|
+
* @param {unknown} value
|
|
6607
|
+
* @returns {string}
|
|
6608
|
+
*/
|
|
6609
|
+
export function describeValue(value) {
|
|
6610
|
+
if (typeof value === 'bigint') return `${value}n`;
|
|
6611
|
+
if (typeof value === 'symbol') return String(value);
|
|
6612
|
+
if (typeof value === 'function') return 'a function';
|
|
6613
|
+
if (value === undefined) return 'undefined';
|
|
6614
|
+
try {
|
|
6615
|
+
return JSON.stringify(value, (_k, v) =>
|
|
6616
|
+
typeof v === 'bigint' ? `${v}n` : typeof v === 'symbol' ? String(v) : v,
|
|
6617
|
+
);
|
|
6618
|
+
} catch {
|
|
6619
|
+
return String(value);
|
|
6620
|
+
}
|
|
6621
|
+
}
|
|
6622
|
+
|
|
4223
6623
|
/**
|
|
4224
6624
|
* Why a token value is not one, with a value of the token's own type to
|
|
4225
6625
|
* write instead (a size in px, a weight as a number in quotes), or none.
|
|
@@ -4227,11 +6627,11 @@ function nonTextToken(t, typeOf = (/** @type {string} */ _k) => 'other') {
|
|
|
4227
6627
|
* @param {string} slug
|
|
4228
6628
|
* @param {[string, unknown, ('object' | 'pair' | 'unit' | 'value' | 'mode')?]} bad
|
|
4229
6629
|
*/
|
|
4230
|
-
function nonTextMessage(ctx, slug, [name, value, kind = 'value']) {
|
|
6630
|
+
export function nonTextMessage(ctx, slug, [name, value, kind = 'value']) {
|
|
4231
6631
|
const type = ctx.C.typeOf(name);
|
|
4232
6632
|
if (kind === 'mode') return ownPairMessage(slug, [name]);
|
|
4233
6633
|
if (kind === 'object')
|
|
4234
|
-
return `${slug}.theme.ts gives ${name} an object (${
|
|
6634
|
+
return `${slug}.theme.ts gives ${name} an object (${describeValue(value)}); a token value is a CSS value in quotes, such as '8px' (only a color takes a [light, dark] pair).`;
|
|
4235
6635
|
if (kind === 'unit') {
|
|
4236
6636
|
const n = /** @type {unknown[]} */ (value).find(
|
|
4237
6637
|
x => typeof x === 'number' && x !== 0,
|
|
@@ -4250,7 +6650,7 @@ function nonTextMessage(ctx, slug, [name, value, kind = 'value']) {
|
|
|
4250
6650
|
const shape = Array.isArray(value)
|
|
4251
6651
|
? 'a [light, dark] pair of two CSS values in quotes'
|
|
4252
6652
|
: 'a CSS value in quotes';
|
|
4253
|
-
return `${slug}.theme.ts gives ${name} the value ${
|
|
6653
|
+
return `${slug}.theme.ts gives ${name} the value ${describeValue(value)}, which is not text: a token value is ${shape}${example ? ` (such as ${Array.isArray(value) ? `[${example}, ${example}]` : example})` : ''}.`;
|
|
4254
6654
|
}
|
|
4255
6655
|
|
|
4256
6656
|
/**
|
|
@@ -4442,18 +6842,180 @@ export async function contrastProblems(ctx, data, res) {
|
|
|
4442
6842
|
* @returns {ThemeData | null}
|
|
4443
6843
|
*/
|
|
4444
6844
|
function parseSettings(text) {
|
|
4445
|
-
|
|
6845
|
+
const data = settingsJson(text);
|
|
6846
|
+
return entriesReadable(data) ? data : null;
|
|
6847
|
+
}
|
|
6848
|
+
|
|
6849
|
+
/**
|
|
6850
|
+
* A settings file's or an undo step's text as JSON, a byte-order mark
|
|
6851
|
+
* aside; undefined when there is no text or it does not parse. Every reader
|
|
6852
|
+
* of settings text reads it here.
|
|
6853
|
+
* @param {string | null | undefined} text
|
|
6854
|
+
* @returns {any}
|
|
6855
|
+
*/
|
|
6856
|
+
export function settingsJson(text) {
|
|
6857
|
+
if (text == null) return undefined;
|
|
4446
6858
|
try {
|
|
4447
|
-
|
|
4448
|
-
return settingsShapeOk(data) ? data : null;
|
|
6859
|
+
return JSON.parse(text.replace(/^\uFEFF/, ''));
|
|
4449
6860
|
} catch {
|
|
4450
|
-
return
|
|
6861
|
+
return undefined;
|
|
6862
|
+
}
|
|
6863
|
+
}
|
|
6864
|
+
|
|
6865
|
+
/**
|
|
6866
|
+
* Whether settings data has its shape and every entry is an object with a
|
|
6867
|
+
* thing name: what a reader may go through entry by entry. Any other entry
|
|
6868
|
+
* (null, a number, text) is damage the file check names.
|
|
6869
|
+
* @param {any} data
|
|
6870
|
+
*/
|
|
6871
|
+
export function entriesReadable(data) {
|
|
6872
|
+
return (
|
|
6873
|
+
settingsShapeOk(data) &&
|
|
6874
|
+
data.settings.every(
|
|
6875
|
+
(/** @type {any} */ s) =>
|
|
6876
|
+
s != null &&
|
|
6877
|
+
typeof s === 'object' &&
|
|
6878
|
+
!Array.isArray(s) &&
|
|
6879
|
+
typeof s.thing === 'string',
|
|
6880
|
+
)
|
|
6881
|
+
);
|
|
6882
|
+
}
|
|
6883
|
+
|
|
6884
|
+
/**
|
|
6885
|
+
* Read a damaged mode or base the way a change reads it, in place: one
|
|
6886
|
+
* written in other letters, as a package name, as a mode word or with a
|
|
6887
|
+
* typo (`Dark-Only`, `@astryxdesign/theme-neutral`, `light`, `neutrl`) is
|
|
6888
|
+
* the value meant, as `set` reads it. Any other is refused for a change
|
|
6889
|
+
* (slug given), with the line that sets it; read for an undo step, it goes
|
|
6890
|
+
* back to its default. `like: "none"` is no base. The field a command sets
|
|
6891
|
+
* itself is left to that command.
|
|
6892
|
+
* @param {any} data
|
|
6893
|
+
* @param {string} cwd
|
|
6894
|
+
* @param {string} [fixing] `mode` or `base` when the command sets it
|
|
6895
|
+
* @param {string} [slug] the theme a change is made to
|
|
6896
|
+
* @returns {string[]} a note for each field read differently from the file
|
|
6897
|
+
*/
|
|
6898
|
+
export function readFields(data, cwd, fixing, slug) {
|
|
6899
|
+
/** @type {string[]} */
|
|
6900
|
+
const notes = [];
|
|
6901
|
+
const meant = fieldsMeant(data);
|
|
6902
|
+
// A field written as no value the reader can read: for a change (slug
|
|
6903
|
+
// given), refused with the line that sets it, never dropped on the way.
|
|
6904
|
+
// The lines offered set it again: the value it reads as when there is
|
|
6905
|
+
// one, else each value it can be (each installed base, each mode), never
|
|
6906
|
+
// a default that would drop what the file held.
|
|
6907
|
+
// A refusal about what the file holds, not what was typed: its lines
|
|
6908
|
+
// set the field again, and are not read against the typed value.
|
|
6909
|
+
const damaged = (
|
|
6910
|
+
/** @type {string} */ field,
|
|
6911
|
+
/** @type {string[]} */ fixes,
|
|
6912
|
+
) => {
|
|
6913
|
+
throw Object.assign(
|
|
6914
|
+
new AstryxError(
|
|
6915
|
+
`themes/${slug}/${slug}.settings.json: ${field === 'mode' ? 'mode' : 'base'} "${field === 'mode' ? data.mode : data.like}" is no value the editor reads, and a change never drops it. Set it again. Nothing was changed.`,
|
|
6916
|
+
(fixes.length
|
|
6917
|
+
? fixes.map(fix => `${P} set ${slug} ${field} ${fix}`)
|
|
6918
|
+
: [`${P} how ${field}`]
|
|
6919
|
+
).map(name => ({name, reason: 'do this instead'})),
|
|
6920
|
+
ERROR_CODES.ERR_THEME_INVALID,
|
|
6921
|
+
),
|
|
6922
|
+
{aboutSaved: true},
|
|
6923
|
+
);
|
|
6924
|
+
};
|
|
6925
|
+
if (
|
|
6926
|
+
fixing !== 'mode' &&
|
|
6927
|
+
!['both', 'light-only', 'dark-only'].includes(data.mode)
|
|
6928
|
+
) {
|
|
6929
|
+
if (['both', 'light-only', 'dark-only'].includes(meant.mode)) {
|
|
6930
|
+
notes.push(
|
|
6931
|
+
`mode was written "${data.mode}", so it is saved as ${meant.mode}`,
|
|
6932
|
+
);
|
|
6933
|
+
data.mode = meant.mode;
|
|
6934
|
+
} else if (slug && data.mode != null && String(data.mode).trim()) {
|
|
6935
|
+
damaged('mode', ['both', 'light-only', 'dark-only']);
|
|
6936
|
+
} else {
|
|
6937
|
+
data.mode = 'both';
|
|
6938
|
+
notes.push('mode was missing or damaged, so it is both again');
|
|
6939
|
+
}
|
|
6940
|
+
}
|
|
6941
|
+
if (data.like === 'none') delete data.like;
|
|
6942
|
+
if (fixing !== 'base' && data.like != null && !LIKE.includes(data.like)) {
|
|
6943
|
+
if (LIKE.includes(meant.like) && baseInstalled(cwd, meant.like)) {
|
|
6944
|
+
notes.push(
|
|
6945
|
+
`base was written "${data.like}", so it is saved as ${meant.like}`,
|
|
6946
|
+
);
|
|
6947
|
+
data.like = meant.like;
|
|
6948
|
+
} else if (slug) {
|
|
6949
|
+
// The base the theme was built on, when its module or history still
|
|
6950
|
+
// says it; else each installed base.
|
|
6951
|
+
damaged(
|
|
6952
|
+
'base',
|
|
6953
|
+
LIKE.includes(meant.like)
|
|
6954
|
+
? [meant.like]
|
|
6955
|
+
: (b => (b ? [b] : LIKE.filter(x => baseInstalled(cwd, x))))(
|
|
6956
|
+
builtOnBase(cwd, slug),
|
|
6957
|
+
),
|
|
6958
|
+
);
|
|
6959
|
+
} else {
|
|
6960
|
+
delete data.like;
|
|
6961
|
+
notes.push('base was not a built-in theme, so it is none again');
|
|
6962
|
+
}
|
|
4451
6963
|
}
|
|
6964
|
+
return notes;
|
|
6965
|
+
}
|
|
6966
|
+
|
|
6967
|
+
/**
|
|
6968
|
+
* The text an undo step keeps for the settings before a change: the file
|
|
6969
|
+
* as the change read it (entry words and fields read as meant), so the
|
|
6970
|
+
* step back restores what the change started from and passes undo's check.
|
|
6971
|
+
* Text that reads as written is kept byte for byte.
|
|
6972
|
+
* @param {string} text
|
|
6973
|
+
* @param {string} cwd
|
|
6974
|
+
* @param {Record<string, string> | null} radii the whole-theme radii it was built with
|
|
6975
|
+
*/
|
|
6976
|
+
function stepText(text, cwd, radii) {
|
|
6977
|
+
const data = settingsJson(text);
|
|
6978
|
+
if (!entriesReadable(data)) return text;
|
|
6979
|
+
const asWritten = JSON.stringify(data);
|
|
6980
|
+
readAsMeant(data, cwd);
|
|
6981
|
+
return JSON.stringify(data) !== asWritten
|
|
6982
|
+
? `${JSON.stringify(radii ? {...data, built: {radii}} : data, null, 2)}\n`
|
|
6983
|
+
: withBuilt(text, radii);
|
|
6984
|
+
}
|
|
6985
|
+
|
|
6986
|
+
/**
|
|
6987
|
+
* Read settings data as a change reads it, in place: entry modes and
|
|
6988
|
+
* sources in other words, and a damaged mode or base (see
|
|
6989
|
+
* {@link readFields}). The settings file and every undo step are read
|
|
6990
|
+
* through here.
|
|
6991
|
+
* @param {any} data
|
|
6992
|
+
* @param {string} cwd
|
|
6993
|
+
* @param {string} [fixing] `mode` or `base` when the command sets it
|
|
6994
|
+
* @returns {string[]} a note for each thing read differently from the text
|
|
6995
|
+
*/
|
|
6996
|
+
export function readAsMeant(data, cwd, fixing) {
|
|
6997
|
+
readEntryWords(data);
|
|
6998
|
+
return [...entryNotes(data), ...readFields(data, cwd, fixing)];
|
|
6999
|
+
}
|
|
7000
|
+
|
|
7001
|
+
/**
|
|
7002
|
+
* Whether every settings entry has the parts the editor reads (a thing, a
|
|
7003
|
+
* value and a source); one without is damage `new` keeps aside.
|
|
7004
|
+
* @param {any} data
|
|
7005
|
+
*/
|
|
7006
|
+
export function settingsEntriesOk(data) {
|
|
7007
|
+
return (
|
|
7008
|
+
entriesReadable(data) &&
|
|
7009
|
+
data.settings.every(
|
|
7010
|
+
(/** @type {any} */ s) =>
|
|
7011
|
+
'value' in s && ['dial', 'token', 'property'].includes(s.source),
|
|
7012
|
+
)
|
|
7013
|
+
);
|
|
4452
7014
|
}
|
|
4453
7015
|
|
|
4454
7016
|
/**
|
|
4455
|
-
* Whether
|
|
4456
|
-
* list
|
|
7017
|
+
* Whether settings data has the shape the editor reads: an object with a
|
|
7018
|
+
* settings list.
|
|
4457
7019
|
* @param {any} data
|
|
4458
7020
|
*/
|
|
4459
7021
|
export function settingsShapeOk(data) {
|
|
@@ -4465,6 +7027,23 @@ export function settingsShapeOk(data) {
|
|
|
4465
7027
|
);
|
|
4466
7028
|
}
|
|
4467
7029
|
|
|
7030
|
+
/**
|
|
7031
|
+
* The settings as the file keeps them: a setting's `note` is what the
|
|
7032
|
+
* command that made it says in its result, not part of the setting.
|
|
7033
|
+
* @param {ThemeData} data
|
|
7034
|
+
* @returns {ThemeData}
|
|
7035
|
+
*/
|
|
7036
|
+
function savedForm(data) {
|
|
7037
|
+
return {
|
|
7038
|
+
...data,
|
|
7039
|
+
settings: data.settings.map(s => {
|
|
7040
|
+
const entry = {...s};
|
|
7041
|
+
delete entry.note;
|
|
7042
|
+
return entry;
|
|
7043
|
+
}),
|
|
7044
|
+
};
|
|
7045
|
+
}
|
|
7046
|
+
|
|
4468
7047
|
/**
|
|
4469
7048
|
* Every regular file under `dir` and the hash of its bytes; empty when
|
|
4470
7049
|
* there is no folder.
|
|
@@ -4521,6 +7100,106 @@ export function focusStateOf(ctx, part) {
|
|
|
4521
7100
|
return null;
|
|
4522
7101
|
}
|
|
4523
7102
|
|
|
7103
|
+
/**
|
|
7104
|
+
* The radius token a part's corners follow when Core draws its corners one
|
|
7105
|
+
* by one from that token by position (a button squares the joins inside a
|
|
7106
|
+
* button group; a button group's outer corners are its end buttons'), or
|
|
7107
|
+
* null. A radius for such a part is written as that
|
|
7108
|
+
* token on the part, so every position keeps its own corner; a radius
|
|
7109
|
+
* written on the part would override Core's per-corner rules.
|
|
7110
|
+
* @param {Ctx} ctx
|
|
7111
|
+
* @param {string} part
|
|
7112
|
+
* @returns {string | null}
|
|
7113
|
+
*/
|
|
7114
|
+
export function positionalCornerToken(ctx, part) {
|
|
7115
|
+
const own = ctx.C.MAP[part]?.own ?? {};
|
|
7116
|
+
for (const prop of [
|
|
7117
|
+
'border-start-start-radius',
|
|
7118
|
+
'border-start-end-radius',
|
|
7119
|
+
'border-end-start-radius',
|
|
7120
|
+
'border-end-end-radius',
|
|
7121
|
+
]) {
|
|
7122
|
+
const e = own[prop];
|
|
7123
|
+
const token = Object.keys(e?.tokens ?? {}).find(t => /^--radius-/.test(t));
|
|
7124
|
+
if (token && (e.values ?? []).some((/** @type {string} */ v) => v === '0'))
|
|
7125
|
+
return token;
|
|
7126
|
+
}
|
|
7127
|
+
// A part with no corners of its own whose outer corners are the corners
|
|
7128
|
+
// Core draws by place on the parts inside it (a button group's first and
|
|
7129
|
+
// last buttons): the one token those read, set on the part.
|
|
7130
|
+
const inner = ctx.C.MAP[part]?.innerCorners ?? [];
|
|
7131
|
+
if (
|
|
7132
|
+
inner.length === 1 &&
|
|
7133
|
+
!Object.keys(own).some(k => /^border-.*radius$/.test(k))
|
|
7134
|
+
)
|
|
7135
|
+
return inner[0];
|
|
7136
|
+
return null;
|
|
7137
|
+
}
|
|
7138
|
+
|
|
7139
|
+
/** The radius tokens the corners dial moves. */
|
|
7140
|
+
export const DIAL_RADII = [
|
|
7141
|
+
'--radius-element',
|
|
7142
|
+
'--radius-inner',
|
|
7143
|
+
'--radius-container',
|
|
7144
|
+
'--radius-page',
|
|
7145
|
+
];
|
|
7146
|
+
|
|
7147
|
+
/**
|
|
7148
|
+
* The parts that draw a part's corners: the part itself, or, when Core gives
|
|
7149
|
+
* it no radius of its own, the parts inside it that it has (a date-time
|
|
7150
|
+
* input's date and time boxes).
|
|
7151
|
+
* @param {Ctx} ctx
|
|
7152
|
+
* @param {string} part
|
|
7153
|
+
* @returns {string[]}
|
|
7154
|
+
*/
|
|
7155
|
+
export function cornerParts(ctx, part) {
|
|
7156
|
+
const rec = ctx.C.MAP[part];
|
|
7157
|
+
if (!rec || rec.own?.['border-radius']) return [part];
|
|
7158
|
+
const inner = (rec.contains ?? []).filter(
|
|
7159
|
+
(/** @type {string} */ t) =>
|
|
7160
|
+
ctx.C.MAP[t]?.own?.['border-radius'] &&
|
|
7161
|
+
t.startsWith(`${part}-`) &&
|
|
7162
|
+
ctx.project.target.has(t),
|
|
7163
|
+
);
|
|
7164
|
+
return inner.length ? inner : [part];
|
|
7165
|
+
}
|
|
7166
|
+
|
|
7167
|
+
/** The table boxes CSS draws no padding on: rows, row groups and columns. */
|
|
7168
|
+
const UNPADDED_BOXES = new Set([
|
|
7169
|
+
'tr',
|
|
7170
|
+
'thead',
|
|
7171
|
+
'tbody',
|
|
7172
|
+
'tfoot',
|
|
7173
|
+
'col',
|
|
7174
|
+
'colgroup',
|
|
7175
|
+
]);
|
|
7176
|
+
|
|
7177
|
+
/**
|
|
7178
|
+
* The parts that take a part's padding: the part itself, or, when Core
|
|
7179
|
+
* renders it as a box CSS draws no padding on (a table row) and gives it no
|
|
7180
|
+
* other display, the parts inside it with padding of their own (its cells).
|
|
7181
|
+
* @param {Ctx} ctx
|
|
7182
|
+
* @param {string} part
|
|
7183
|
+
* @returns {string[]}
|
|
7184
|
+
*/
|
|
7185
|
+
export function paddedParts(ctx, part) {
|
|
7186
|
+
const rec = ctx.C.MAP[part];
|
|
7187
|
+
const tags = rec?.tags ?? [];
|
|
7188
|
+
if (
|
|
7189
|
+
!rec ||
|
|
7190
|
+
!tags.length ||
|
|
7191
|
+
!tags.every(t => UNPADDED_BOXES.has(t)) ||
|
|
7192
|
+
rec.own?.display
|
|
7193
|
+
)
|
|
7194
|
+
return [part];
|
|
7195
|
+
const inner = (rec.contains ?? []).filter(
|
|
7196
|
+
(/** @type {string} */ t) =>
|
|
7197
|
+
ctx.project.target.has(t) &&
|
|
7198
|
+
Object.keys(ctx.C.MAP[t]?.own ?? {}).some(k => /^padding/.test(k)),
|
|
7199
|
+
);
|
|
7200
|
+
return inner.length ? inner : [part];
|
|
7201
|
+
}
|
|
7202
|
+
|
|
4524
7203
|
/**
|
|
4525
7204
|
* The radius token a part's resting corners read. A part the census saw
|
|
4526
7205
|
* closed (a menu, a popover) has none recorded: a container or menu reads
|
|
@@ -4583,6 +7262,47 @@ export function stateKey(state, wrapsControl = false) {
|
|
|
4583
7262
|
: ':is(:disabled,[aria-disabled="true"])';
|
|
4584
7263
|
}
|
|
4585
7264
|
|
|
7265
|
+
/**
|
|
7266
|
+
* The text color tokens Core's elements inside a part read for their own
|
|
7267
|
+
* color (a top nav heading's title span reads --color-text-primary itself),
|
|
7268
|
+
* so a color set on the part never reaches that text. Only the main and
|
|
7269
|
+
* supporting text colors count: the disabled, placeholder and accent ones
|
|
7270
|
+
* mark a state or a role, not the part's text. With `deep`, the parts inside
|
|
7271
|
+
* it count too (a top nav's heading and items).
|
|
7272
|
+
* @param {{MAP: Record<string, any>}} C
|
|
7273
|
+
* @param {string} part
|
|
7274
|
+
* @param {boolean} [deep]
|
|
7275
|
+
* @returns {string[]}
|
|
7276
|
+
*/
|
|
7277
|
+
export function innerTextTokens(C, part, deep = false) {
|
|
7278
|
+
const TEXT_ROLES = ['--color-text-primary', '--color-text-secondary'];
|
|
7279
|
+
/** @type {Set<string>} */
|
|
7280
|
+
const out = new Set();
|
|
7281
|
+
/** @param {Record<string, string[]> | undefined} reads */
|
|
7282
|
+
const add = reads => {
|
|
7283
|
+
for (const [tok, props] of Object.entries(reads ?? {}))
|
|
7284
|
+
if (TEXT_ROLES.includes(tok) && props.includes('color')) out.add(tok);
|
|
7285
|
+
};
|
|
7286
|
+
const rec = C.MAP[part];
|
|
7287
|
+
add(rec?.inside);
|
|
7288
|
+
if (deep)
|
|
7289
|
+
for (const inner of rec?.contains ?? []) {
|
|
7290
|
+
const r = C.MAP[inner];
|
|
7291
|
+
add(r?.inside);
|
|
7292
|
+
// A part's own color counts where it reads the token always, not
|
|
7293
|
+
// only in a variant or a state.
|
|
7294
|
+
for (const [tok, conds] of Object.entries(r?.own?.color?.tokens ?? {}))
|
|
7295
|
+
if (
|
|
7296
|
+
TEXT_ROLES.includes(tok) &&
|
|
7297
|
+
/** @type {string[]} */ (conds).some(
|
|
7298
|
+
w => !w.trim() || w.trim() === 'always',
|
|
7299
|
+
)
|
|
7300
|
+
)
|
|
7301
|
+
out.add(tok);
|
|
7302
|
+
}
|
|
7303
|
+
return [...out].sort();
|
|
7304
|
+
}
|
|
7305
|
+
|
|
4586
7306
|
/**
|
|
4587
7307
|
* The component docs of a part: its own component's, and the one named like
|
|
4588
7308
|
* the part (tab is Tab, under TabList), each with its name, for the ones the
|
|
@@ -4651,13 +7371,9 @@ export function radiiIn(source) {
|
|
|
4651
7371
|
*/
|
|
4652
7372
|
function withBuilt(text, radii) {
|
|
4653
7373
|
if (!radii) return text;
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
return `${JSON.stringify({...data, built: {radii}}, null, 2)}\n`;
|
|
4658
|
-
} catch {
|
|
4659
|
-
return text;
|
|
4660
|
-
}
|
|
7374
|
+
const data = settingsJson(text);
|
|
7375
|
+
if (!settingsShapeOk(data)) return text;
|
|
7376
|
+
return `${JSON.stringify({...data, built: {radii}}, null, 2)}\n`;
|
|
4661
7377
|
}
|
|
4662
7378
|
|
|
4663
7379
|
/**
|
|
@@ -4710,6 +7426,37 @@ export function radiusAt(base, value, token) {
|
|
|
4710
7426
|
* editor's header).
|
|
4711
7427
|
* @param {string} file
|
|
4712
7428
|
*/
|
|
7429
|
+
/**
|
|
7430
|
+
* Whether the editor made a theme's folder, though its settings may be
|
|
7431
|
+
* gone: a stopped `new` left its marker, or the folder holds what only the
|
|
7432
|
+
* editor writes (the generated settings module, an undo history, the theme
|
|
7433
|
+
* source with the header the editor wrote it with).
|
|
7434
|
+
* @param {string} cwd
|
|
7435
|
+
* @param {string} slug
|
|
7436
|
+
*/
|
|
7437
|
+
export function editorMadeFolder(cwd, slug) {
|
|
7438
|
+
const f = themeFiles(cwd, slug);
|
|
7439
|
+
const firstLine = (/** @type {string} */ file) => {
|
|
7440
|
+
try {
|
|
7441
|
+
return fs.readFileSync(file, 'utf8').split('\n', 1)[0];
|
|
7442
|
+
} catch {
|
|
7443
|
+
return '';
|
|
7444
|
+
}
|
|
7445
|
+
};
|
|
7446
|
+
return (
|
|
7447
|
+
fs.existsSync(path.join(f.dir, NEW_MARKER)) ||
|
|
7448
|
+
madeByEditor(f.settingsTs) ||
|
|
7449
|
+
// Only editor commands write the undo history.
|
|
7450
|
+
readHistory(f.history).steps.length > 0 ||
|
|
7451
|
+
firstLine(f.ts) ===
|
|
7452
|
+
`// The ${slug} theme. \`${P}\` wrote this file once and never changes it again.`
|
|
7453
|
+
);
|
|
7454
|
+
}
|
|
7455
|
+
|
|
7456
|
+
/**
|
|
7457
|
+
* Whether a file carries the editor's generated header.
|
|
7458
|
+
* @param {string} file
|
|
7459
|
+
*/
|
|
4713
7460
|
export function madeByEditor(file) {
|
|
4714
7461
|
try {
|
|
4715
7462
|
return fs
|
|
@@ -4939,12 +7686,15 @@ export function refuseLinks(cwd, slug) {
|
|
|
4939
7686
|
* @param {ThemeData} data
|
|
4940
7687
|
*/
|
|
4941
7688
|
export function settingsStamp(data) {
|
|
7689
|
+
// The settings as the file keeps them, so the stamp of a build matches
|
|
7690
|
+
// the file it was built from.
|
|
7691
|
+
const saved = savedForm(data);
|
|
4942
7692
|
const hash = createHash('sha256')
|
|
4943
7693
|
.update(
|
|
4944
7694
|
JSON.stringify({
|
|
4945
|
-
like:
|
|
4946
|
-
mode:
|
|
4947
|
-
settings:
|
|
7695
|
+
like: saved.like ?? null,
|
|
7696
|
+
mode: saved.mode,
|
|
7697
|
+
settings: saved.settings,
|
|
4948
7698
|
}),
|
|
4949
7699
|
)
|
|
4950
7700
|
.digest('hex')
|
|
@@ -5052,7 +7802,9 @@ export function builtFiles(cwd, slug, data, some = false) {
|
|
|
5052
7802
|
const colors = data.settings.some(
|
|
5053
7803
|
s => s.source === 'dial' && PALETTE_DIALS.includes(s.thing),
|
|
5054
7804
|
)
|
|
5055
|
-
? [pal.palette, pal.refs, pal.receipt].map(p =>
|
|
7805
|
+
? [pal.config, pal.palette, pal.refs, pal.receipt].map(p =>
|
|
7806
|
+
path.join(cwd, p),
|
|
7807
|
+
)
|
|
5056
7808
|
: [];
|
|
5057
7809
|
return [
|
|
5058
7810
|
f.settingsTs,
|
|
@@ -5067,6 +7819,21 @@ export function builtFiles(cwd, slug, data, some = false) {
|
|
|
5067
7819
|
];
|
|
5068
7820
|
}
|
|
5069
7821
|
|
|
7822
|
+
/**
|
|
7823
|
+
* The file's stat, or undefined when it is not there. On Node 22.13 a folder
|
|
7824
|
+
* above the file that is a file throws ENOTDIR even with throwIfNoEntry
|
|
7825
|
+
* false; that file is not there either, as on later Node.
|
|
7826
|
+
* @param {string} file
|
|
7827
|
+
*/
|
|
7828
|
+
function statIfThere(file) {
|
|
7829
|
+
try {
|
|
7830
|
+
return fs.statSync(file, {throwIfNoEntry: false});
|
|
7831
|
+
} catch (e) {
|
|
7832
|
+
if (/** @type {any} */ (e)?.code === 'ENOTDIR') return undefined;
|
|
7833
|
+
throw e;
|
|
7834
|
+
}
|
|
7835
|
+
}
|
|
7836
|
+
|
|
5070
7837
|
/**
|
|
5071
7838
|
* Refuse, by name, a built file that is there but cannot be read: its
|
|
5072
7839
|
* permissions, or a folder where the file goes.
|
|
@@ -5075,7 +7842,7 @@ export function builtFiles(cwd, slug, data, some = false) {
|
|
|
5075
7842
|
*/
|
|
5076
7843
|
function refuseUnreadable(cwd, files) {
|
|
5077
7844
|
for (const file of files) {
|
|
5078
|
-
const st =
|
|
7845
|
+
const st = statIfThere(file);
|
|
5079
7846
|
if (!st) continue;
|
|
5080
7847
|
const rel = shown(cwd, file);
|
|
5081
7848
|
if (st.isDirectory())
|
|
@@ -5166,10 +7933,7 @@ export function readHistory(file) {
|
|
|
5166
7933
|
if (!Array.isArray(h)) return {steps: [], dropped: 1};
|
|
5167
7934
|
const ok = (/** @type {unknown} */ x) => {
|
|
5168
7935
|
try {
|
|
5169
|
-
return (
|
|
5170
|
-
typeof x === 'string' &&
|
|
5171
|
-
Array.isArray(JSON.parse(x.replace(/^\uFEFF/, '')).settings)
|
|
5172
|
-
);
|
|
7936
|
+
return typeof x === 'string' && settingsShapeOk(settingsJson(x));
|
|
5173
7937
|
} catch {
|
|
5174
7938
|
return false;
|
|
5175
7939
|
}
|
|
@@ -5204,7 +7968,7 @@ export async function commitTheme(
|
|
|
5204
7968
|
const {cwd} = ctx.project;
|
|
5205
7969
|
const f = themeFiles(cwd, slug);
|
|
5206
7970
|
refuseLinks(cwd, slug);
|
|
5207
|
-
refuseThemeSource(cwd, slug);
|
|
7971
|
+
await refuseThemeSource(cwd, slug);
|
|
5208
7972
|
const snapBefore = fs.existsSync(f.settings)
|
|
5209
7973
|
? fs.readFileSync(f.settings, 'utf8')
|
|
5210
7974
|
: null;
|
|
@@ -5225,8 +7989,8 @@ export async function commitTheme(
|
|
|
5225
7989
|
// no undo step.
|
|
5226
7990
|
const same = (/** @type {ThemeData | null} */ a) =>
|
|
5227
7991
|
a != null &&
|
|
5228
|
-
JSON.stringify({...a, lifted: undefined}) ===
|
|
5229
|
-
JSON.stringify({...data, lifted: undefined});
|
|
7992
|
+
JSON.stringify({...savedForm(a), lifted: undefined}) ===
|
|
7993
|
+
JSON.stringify({...savedForm(data), lifted: undefined});
|
|
5230
7994
|
// A no-op change still mends a build that does not match the settings
|
|
5231
7995
|
// (an undo stopped part way): it is built again, with no new step.
|
|
5232
7996
|
const noop = history && same(parseSettings(snapBefore));
|
|
@@ -5324,7 +8088,7 @@ export async function commitTheme(
|
|
|
5324
8088
|
const wrote = new Map();
|
|
5325
8089
|
ctx.wrote = wrote;
|
|
5326
8090
|
fs.mkdirSync(f.dir, {recursive: true});
|
|
5327
|
-
/** @type {{warnings: string[], lifted: string[], notes: string[]}} */
|
|
8091
|
+
/** @type {{warnings: string[], lifted: string[], notes: string[], removedPictures: string[]}} */
|
|
5328
8092
|
let built;
|
|
5329
8093
|
// The token loops the last build had, so a change that adds one (through
|
|
5330
8094
|
// tokens the own source sets in a way no read of the file follows) is
|
|
@@ -5361,7 +8125,7 @@ export async function commitTheme(
|
|
|
5361
8125
|
// for an undo, a top step equal to the settings, which undo skips),
|
|
5362
8126
|
// never a step that undoes nothing or a lost step.
|
|
5363
8127
|
await track(f.dir, wrote, () => {
|
|
5364
|
-
writeWhole(f.settings, `${JSON.stringify(data, null, 2)}\n`);
|
|
8128
|
+
writeWhole(f.settings, `${JSON.stringify(savedForm(data), null, 2)}\n`);
|
|
5365
8129
|
if (historyAfter)
|
|
5366
8130
|
writeWhole(
|
|
5367
8131
|
f.history,
|
|
@@ -5371,7 +8135,9 @@ export async function commitTheme(
|
|
|
5371
8135
|
writeWhole(
|
|
5372
8136
|
f.history,
|
|
5373
8137
|
JSON.stringify(
|
|
5374
|
-
[...past,
|
|
8138
|
+
[...past, stepText(snapBefore, cwd, radiiNow)].slice(
|
|
8139
|
+
-HISTORY_STEPS,
|
|
8140
|
+
),
|
|
5375
8141
|
),
|
|
5376
8142
|
);
|
|
5377
8143
|
});
|
|
@@ -5471,6 +8237,10 @@ export async function commitTheme(
|
|
|
5471
8237
|
);
|
|
5472
8238
|
}
|
|
5473
8239
|
notes.push(...built.notes);
|
|
8240
|
+
if (built.removedPictures.length)
|
|
8241
|
+
notes.push(
|
|
8242
|
+
`removed ${built.removedPictures.join(', ')}: ${built.removedPictures.length === 1 ? 'a picture' : 'pictures'} of the look before this change; ${P} show ${slug} --png takes new ones`,
|
|
8243
|
+
);
|
|
5474
8244
|
// The change is saved from here on: a failure to check it is reported
|
|
5475
8245
|
// as "saved, not checked", never as a failed change.
|
|
5476
8246
|
/** @type {Record<'light' | 'dark', Record<string, string>>} */
|
|
@@ -5536,9 +8306,66 @@ export async function commitTheme(
|
|
|
5536
8306
|
};
|
|
5537
8307
|
}
|
|
5538
8308
|
|
|
5539
|
-
/**
|
|
5540
|
-
|
|
5541
|
-
|
|
8309
|
+
/**
|
|
8310
|
+
* The @fontsource package for a font family: an open font's own package
|
|
8311
|
+
* as the editor's font table names it (Geist is `@fontsource/geist-sans`),
|
|
8312
|
+
* else the family's name in Fontsource's form (`Open Sans` is
|
|
8313
|
+
* `@fontsource/open-sans`).
|
|
8314
|
+
* @param {string} family
|
|
8315
|
+
* @returns {string}
|
|
8316
|
+
*/
|
|
8317
|
+
export function fontsourcePackageOf(family) {
|
|
8318
|
+
const name = String(family).replace(/["']/g, '').trim();
|
|
8319
|
+
const open = Object.values(OPEN_FONTS).find(
|
|
8320
|
+
f =>
|
|
8321
|
+
f.family.replace(/["']/g, '').trim().toLowerCase() === name.toLowerCase(),
|
|
8322
|
+
);
|
|
8323
|
+
return open?.pkg ?? `@fontsource/${name.toLowerCase().replace(/\s+/g, '-')}`;
|
|
8324
|
+
}
|
|
8325
|
+
|
|
8326
|
+
/**
|
|
8327
|
+
* The font families the theme's last build names (its `--font-family-*`
|
|
8328
|
+
* tokens, every part's `font-family`, and a `font` shorthand's families), by lowercase name; null before the
|
|
8329
|
+
* theme has been built.
|
|
8330
|
+
* @param {Ctx} ctx
|
|
8331
|
+
* @param {string} slug
|
|
8332
|
+
* @returns {Map<string, string> | null}
|
|
8333
|
+
*/
|
|
8334
|
+
export function builtFontFamilies(ctx, slug) {
|
|
8335
|
+
/** @type {string} */
|
|
8336
|
+
let css;
|
|
8337
|
+
try {
|
|
8338
|
+
css = fs.readFileSync(themeFiles(ctx.project.cwd, slug).css, 'utf8');
|
|
8339
|
+
} catch {
|
|
8340
|
+
return null;
|
|
8341
|
+
}
|
|
8342
|
+
/** @type {Map<string, string>} */
|
|
8343
|
+
const out = new Map();
|
|
8344
|
+
// Every family the build writes: the font tokens, a part's own
|
|
8345
|
+
// font-family, and the families a `font` shorthand names last.
|
|
8346
|
+
const lists = [
|
|
8347
|
+
...declarationValues(css, /^(--font-family-.*|font-family)$/),
|
|
8348
|
+
...declarationValues(css, /^font$/).map(fontShorthandFamilyList),
|
|
8349
|
+
];
|
|
8350
|
+
for (const value of lists)
|
|
8351
|
+
for (const family of namedFontFamilies(value))
|
|
8352
|
+
if (!out.has(family.toLowerCase())) out.set(family.toLowerCase(), family);
|
|
8353
|
+
return out;
|
|
8354
|
+
}
|
|
8355
|
+
|
|
8356
|
+
/**
|
|
8357
|
+
* The font packages that load the families the theme builds: a font
|
|
8358
|
+
* setting's package while the build still uses its family, and the
|
|
8359
|
+
* installed @fontsource package of a family the theme's own source sets.
|
|
8360
|
+
* Font notes, the previews and the app copy all load fonts from this list.
|
|
8361
|
+
* For a new app that installs its own packages (`forNewApp`), every built
|
|
8362
|
+
* family that is an open font comes too, installed here or not.
|
|
8363
|
+
* @param {Ctx} ctx
|
|
8364
|
+
* @param {ThemeData} data
|
|
8365
|
+
* @param {{forNewApp?: boolean}} [opts]
|
|
8366
|
+
*/
|
|
8367
|
+
export function fontPackages(ctx, data, {forNewApp = false} = {}) {
|
|
8368
|
+
/** @type {Array<{pkg: string, family: string, installed: boolean, of: Setting | null}>} */
|
|
5542
8369
|
const out = [];
|
|
5543
8370
|
/** @param {string} pkg */
|
|
5544
8371
|
const isInstalled = pkg => {
|
|
@@ -5549,18 +8376,29 @@ export function fontPackages(ctx, data) {
|
|
|
5549
8376
|
return false;
|
|
5550
8377
|
}
|
|
5551
8378
|
};
|
|
8379
|
+
/** An open font's package, which a new app installs. @param {string} pkg */
|
|
8380
|
+
const isOpen = pkg => Object.values(OPEN_FONTS).some(f => f.pkg === pkg);
|
|
8381
|
+
/** Whether a package loads its family where the fonts go. @param {string} pkg */
|
|
8382
|
+
const loads = pkg => isInstalled(pkg) || (forNewApp && isOpen(pkg));
|
|
8383
|
+
const built = builtFontFamilies(ctx, data.name);
|
|
5552
8384
|
for (const s of data.settings) {
|
|
5553
8385
|
if (s.thing !== 'font') continue;
|
|
5554
8386
|
const {pkg: known, family} = fontOf(s.value);
|
|
5555
8387
|
const name = family.replace(/["']/g, '');
|
|
8388
|
+
// A family the theme's own source replaced everywhere it was set is
|
|
8389
|
+
// not in the build: nothing loads it.
|
|
8390
|
+
if (built && !built.has(name.toLowerCase())) continue;
|
|
5556
8391
|
// An installed @fontsource package of the family always loads it, even
|
|
5557
8392
|
// for a family some systems ship.
|
|
5558
|
-
const own =
|
|
5559
|
-
const pkg = known ?? (
|
|
8393
|
+
const own = fontsourcePackageOf(name);
|
|
8394
|
+
const pkg = known ?? (loads(own) ? own : null);
|
|
5560
8395
|
if (!pkg || out.some(x => x.pkg === pkg)) continue;
|
|
5561
8396
|
out.push({pkg, family: name, installed: isInstalled(pkg), of: s});
|
|
5562
8397
|
}
|
|
8398
|
+
for (const family of built?.values() ?? []) {
|
|
8399
|
+
const pkg = fontsourcePackageOf(family);
|
|
8400
|
+
if (out.some(x => x.pkg === pkg) || !loads(pkg)) continue;
|
|
8401
|
+
out.push({pkg, family, installed: isInstalled(pkg), of: null});
|
|
8402
|
+
}
|
|
5563
8403
|
return out;
|
|
5564
8404
|
}
|
|
5565
|
-
|
|
5566
|
-
export {partDefault};
|