tinyzoo 0.0.0-stage → 0.2.1
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/LICENSE +10 -0
- package/README.md +46 -2
- package/app/app.config.ts +35 -0
- package/app/assets/brand/characters/bright/bird.svg +1 -0
- package/app/assets/brand/characters/bright/bunny.svg +1 -0
- package/app/assets/brand/characters/bright/elephant.svg +1 -0
- package/app/assets/brand/characters/bright/fox.svg +1 -0
- package/app/assets/brand/characters/bright/frog.svg +1 -0
- package/app/assets/brand/characters/bright/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/bee.svg +1 -0
- package/app/assets/brand/characters/muted/bird.svg +1 -0
- package/app/assets/brand/characters/muted/butterfly.svg +1 -0
- package/app/assets/brand/characters/muted/chick.svg +1 -0
- package/app/assets/brand/characters/muted/cow.svg +1 -0
- package/app/assets/brand/characters/muted/deer.svg +1 -0
- package/app/assets/brand/characters/muted/duck.svg +1 -0
- package/app/assets/brand/characters/muted/elephant.svg +1 -0
- package/app/assets/brand/characters/muted/fox.svg +1 -0
- package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
- package/app/assets/brand/characters/muted/koala.svg +1 -0
- package/app/assets/brand/characters/muted/ladybug.svg +1 -0
- package/app/assets/brand/characters/muted/lion.svg +1 -0
- package/app/assets/brand/characters/muted/mouse.svg +1 -0
- package/app/assets/brand/characters/muted/octopus.svg +1 -0
- package/app/assets/brand/characters/muted/owl.svg +1 -0
- package/app/assets/brand/characters/muted/panda.svg +1 -0
- package/app/assets/brand/characters/muted/penguin.svg +1 -0
- package/app/assets/brand/characters/muted/pig.svg +1 -0
- package/app/assets/brand/characters/muted/poodle.svg +1 -0
- package/app/assets/brand/characters/muted/raccoon.svg +1 -0
- package/app/assets/brand/characters/muted/seal.svg +1 -0
- package/app/assets/brand/characters/muted/sheep.svg +1 -0
- package/app/assets/brand/characters/muted/snail.svg +1 -0
- package/app/assets/brand/characters/muted/turtle.svg +1 -0
- package/app/assets/brand/characters/muted/whale.svg +1 -0
- package/app/assets/brand/logo/favicon.svg +1 -0
- package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
- package/app/assets/brand/logo/logo-primary.svg +1 -0
- package/app/assets/brand/logo/mascot-reversed.svg +1 -0
- package/app/assets/brand/logo/mascot.svg +1 -0
- package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
- package/app/assets/brand/logo/wordmark.svg +1 -0
- package/app/assets/css/cms-admin.css +607 -0
- package/app/components/CmsBlocks.vue +30 -0
- package/app/components/CmsCell.vue +16 -0
- package/app/components/CmsCellBadge.vue +18 -0
- package/app/components/CmsCellBlocks.vue +9 -0
- package/app/components/CmsCellBoolean.vue +12 -0
- package/app/components/CmsCellCode.vue +7 -0
- package/app/components/CmsCellColor.vue +13 -0
- package/app/components/CmsCellDate.vue +10 -0
- package/app/components/CmsCellExcerpt.vue +21 -0
- package/app/components/CmsCellGallery.vue +10 -0
- package/app/components/CmsCellGroup.vue +23 -0
- package/app/components/CmsCellNumber.vue +7 -0
- package/app/components/CmsCellRelation.vue +8 -0
- package/app/components/CmsCellText.vue +12 -0
- package/app/components/CmsCellThumb.vue +18 -0
- package/app/components/CmsConfirmDialog.vue +55 -0
- package/app/components/CmsContent.vue +103 -0
- package/app/components/CmsDayPicker.vue +44 -0
- package/app/components/CmsEmptyState.vue +35 -0
- package/app/components/CmsField.vue +111 -0
- package/app/components/CmsFieldBlocks.vue +353 -0
- package/app/components/CmsFieldBoolean.vue +8 -0
- package/app/components/CmsFieldColor.vue +42 -0
- package/app/components/CmsFieldDate.vue +131 -0
- package/app/components/CmsFieldGallery.vue +114 -0
- package/app/components/CmsFieldGroup.vue +53 -0
- package/app/components/CmsFieldMedia.vue +219 -0
- package/app/components/CmsFieldNumber.vue +11 -0
- package/app/components/CmsFieldRelationMany.vue +54 -0
- package/app/components/CmsFieldRelationOne.vue +47 -0
- package/app/components/CmsFieldRichText.vue +45 -0
- package/app/components/CmsFieldSelect.vue +41 -0
- package/app/components/CmsFieldSlug.vue +46 -0
- package/app/components/CmsFieldText.vue +75 -0
- package/app/components/CmsFormSections.vue +150 -0
- package/app/components/CmsHighlight.vue +22 -0
- package/app/components/CmsImage.vue +39 -0
- package/app/components/CmsLinkPicker.vue +122 -0
- package/app/components/CmsLinkTarget.vue +35 -0
- package/app/components/CmsListFilters.vue +107 -0
- package/app/components/CmsLiveNotice.vue +64 -0
- package/app/components/CmsLoadError.vue +24 -0
- package/app/components/CmsMascot.vue +10 -0
- package/app/components/CmsMediaLibrary.vue +114 -0
- package/app/components/CmsMediaPreview.vue +34 -0
- package/app/components/CmsMediaThumb.vue +22 -0
- package/app/components/CmsMoment.vue +42 -0
- package/app/components/CmsNav.vue +39 -0
- package/app/components/CmsPage.vue +86 -0
- package/app/components/CmsPasswordInput.vue +22 -0
- package/app/components/CmsPresence.vue +29 -0
- package/app/components/CmsPreviewPanel.vue +131 -0
- package/app/components/CmsPublishReview.vue +102 -0
- package/app/components/CmsRevisions.vue +195 -0
- package/app/components/CmsRichTextEditor.vue +290 -0
- package/app/components/CmsSearch.vue +99 -0
- package/app/components/CmsSection.vue +20 -0
- package/app/components/CmsSingletonForm.vue +283 -0
- package/app/components/CmsStatusBadge.vue +21 -0
- package/app/components/CmsTime.vue +16 -0
- package/app/components/CmsTranslationRef.vue +27 -0
- package/app/components/CmsTreeList.vue +453 -0
- package/app/components/CmsUnsavedText.vue +34 -0
- package/app/components/CmsWordmark.vue +10 -0
- package/app/composables/useCmsAdminHead.ts +18 -0
- package/app/composables/useCmsCollab.ts +165 -0
- package/app/composables/useCmsConfirm.ts +39 -0
- package/app/composables/useCmsEntryActions.ts +75 -0
- package/app/composables/useCmsFocus.ts +13 -0
- package/app/composables/useCmsForm.ts +273 -0
- package/app/composables/useCmsI18n.ts +185 -0
- package/app/composables/useCmsLinkTargets.ts +84 -0
- package/app/composables/useCmsLive.ts +199 -0
- package/app/composables/useCmsSearch.ts +62 -0
- package/app/composables/useCmsSeo.ts +99 -0
- package/app/composables/useCmsSignedIn.ts +24 -0
- package/app/composables/useCmsUiLocale.ts +24 -0
- package/app/i18n/cs.ts +781 -0
- package/app/i18n/de.ts +781 -0
- package/app/i18n/en.ts +782 -0
- package/app/i18n/types.ts +22 -0
- package/app/layouts/admin.vue +224 -0
- package/app/layouts/cms-auth.vue +37 -0
- package/app/middleware/cms-api-not-a-page.global.ts +10 -0
- package/app/middleware/cms-auth.global.ts +16 -0
- package/app/pages/admin/[entity]/[id].vue +1109 -0
- package/app/pages/admin/[entity]/index.vue +733 -0
- package/app/pages/admin/account.vue +117 -0
- package/app/pages/admin/forgot.vue +50 -0
- package/app/pages/admin/index.vue +184 -0
- package/app/pages/admin/invite/[token].vue +73 -0
- package/app/pages/admin/login.vue +84 -0
- package/app/pages/admin/media.vue +494 -0
- package/app/pages/admin/privacy.vue +193 -0
- package/app/pages/admin/reset/[token].vue +52 -0
- package/app/pages/admin/setup.vue +87 -0
- package/app/pages/admin/styleguide.vue +261 -0
- package/app/pages/admin/team.vue +258 -0
- package/app/plugins/cmsLivePreview.client.ts +20 -0
- package/app/plugins/cmsSignedOut.client.ts +31 -0
- package/app/utils/cmsBlockType.ts +11 -0
- package/app/utils/cmsBrand.ts +38 -0
- package/app/utils/cmsDiff.ts +83 -0
- package/app/utils/cmsFetch.ts +7 -0
- package/app/utils/cmsFocusInvalid.ts +22 -0
- package/app/utils/cmsIssues.ts +128 -0
- package/app/utils/cmsJson.ts +28 -0
- package/app/utils/cmsMedia.ts +63 -0
- package/app/utils/cmsSections.ts +154 -0
- package/app/utils/cmsTime.ts +41 -0
- package/cli/backup.ts +713 -0
- package/cli/breaking.ts +174 -0
- package/cli/content-migrate.ts +239 -0
- package/cli/db.ts +86 -0
- package/cli/export.ts +336 -0
- package/cli/generate.ts +146 -0
- package/cli/import.ts +643 -0
- package/cli/index.ts +140 -0
- package/cli/links-convert.ts +291 -0
- package/cli/seed-engine.ts +705 -0
- package/cli/seed.ts +339 -0
- package/cli/status.ts +49 -0
- package/cli/tar.ts +304 -0
- package/cli/transfer.ts +677 -0
- package/cli/up.ts +27 -0
- package/cli/upgrade.ts +60 -0
- package/cli/versions.ts +60 -0
- package/forms/components/CmsForm.vue +186 -0
- package/forms/index.ts +203 -0
- package/forms/messages.ts +104 -0
- package/forms/retention.ts +48 -0
- package/forms/server/form.get.ts +12 -0
- package/forms/server/form.post.ts +109 -0
- package/forms/server/formSettings.ts +12 -0
- package/forms/server/forms.ts +127 -0
- package/module.ts +373 -0
- package/nuxt.config.ts +43 -0
- package/package.json +98 -3
- package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
- package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
- package/server/api/cms/[entity]/[id].delete.ts +52 -0
- package/server/api/cms/[entity]/[id].get.ts +21 -0
- package/server/api/cms/[entity]/[id].patch.ts +116 -0
- package/server/api/cms/[entity]/_export.get.ts +69 -0
- package/server/api/cms/[entity]/_move.post.ts +81 -0
- package/server/api/cms/[entity]/_order.post.ts +49 -0
- package/server/api/cms/[entity]/_tree.get.ts +41 -0
- package/server/api/cms/[entity]/index.get.ts +62 -0
- package/server/api/cms/[entity]/index.post.ts +38 -0
- package/server/api/cms/[entity]/options.get.ts +55 -0
- package/server/api/cms/_auth/accept.post.ts +33 -0
- package/server/api/cms/_auth/forgot.post.ts +39 -0
- package/server/api/cms/_auth/invite/[token].get.ts +18 -0
- package/server/api/cms/_auth/login.post.ts +42 -0
- package/server/api/cms/_auth/logout.post.ts +7 -0
- package/server/api/cms/_auth/me.get.ts +5 -0
- package/server/api/cms/_auth/me.patch.ts +32 -0
- package/server/api/cms/_auth/password.post.ts +29 -0
- package/server/api/cms/_auth/reset.post.ts +34 -0
- package/server/api/cms/_auth/setup.post.ts +46 -0
- package/server/api/cms/_auth/status.get.ts +18 -0
- package/server/api/cms/_collab/session.get.ts +30 -0
- package/server/api/cms/_collab/socket.ts +51 -0
- package/server/api/cms/_collab/unsaved.delete.ts +16 -0
- package/server/api/cms/_graph.get.ts +20 -0
- package/server/api/cms/_links/resolve.post.ts +17 -0
- package/server/api/cms/_links.get.ts +81 -0
- package/server/api/cms/_live.ts +30 -0
- package/server/api/cms/_media/[id]/usage.get.ts +14 -0
- package/server/api/cms/_media/[id].delete.ts +29 -0
- package/server/api/cms/_media/[id].get.ts +13 -0
- package/server/api/cms/_media/[id].patch.ts +64 -0
- package/server/api/cms/_media/exists.post.ts +15 -0
- package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
- package/server/api/cms/_media/index.get.ts +44 -0
- package/server/api/cms/_media/index.post.ts +147 -0
- package/server/api/cms/_media/raw/[...key].get.ts +85 -0
- package/server/api/cms/_media/raw/[...key].head.ts +2 -0
- package/server/api/cms/_overview.get.ts +87 -0
- package/server/api/cms/_preview.post.ts +17 -0
- package/server/api/cms/_privacy/delete.post.ts +14 -0
- package/server/api/cms/_privacy/export.post.ts +25 -0
- package/server/api/cms/_privacy/lookup.post.ts +27 -0
- package/server/api/cms/_search.get.ts +17 -0
- package/server/api/cms/_singleton/[name].get.ts +19 -0
- package/server/api/cms/_singleton/[name].put.ts +76 -0
- package/server/api/cms/_users/[id]/resend.post.ts +25 -0
- package/server/api/cms/_users/[id].delete.ts +21 -0
- package/server/api/cms/_users/[id].patch.ts +45 -0
- package/server/api/cms/_users/index.get.ts +28 -0
- package/server/api/cms/_users/index.post.ts +39 -0
- package/server/api/content/[entity]/[slug].get.ts +74 -0
- package/server/api/content/[entity]/index.get.ts +100 -0
- package/server/api/content/_global/[name].get.ts +23 -0
- package/server/api/content/_search.get.ts +28 -0
- package/server/api/content/_sitemap.get.ts +8 -0
- package/server/api/content/_tree/[entity].get.ts +31 -0
- package/server/api/content-types.ts.get.ts +17 -0
- package/server/api/health/live.get.ts +10 -0
- package/server/api/health/ready.get.ts +21 -0
- package/server/api/openapi.get.ts +48 -0
- package/server/api/openapi.json.get.ts +32 -0
- package/server/db/driver.ts +8 -0
- package/server/mail/mailer.ts +43 -0
- package/server/middleware/cms-0-headers.ts +37 -0
- package/server/middleware/cms-0-migrate-gate.ts +14 -0
- package/server/middleware/cms-csrf.ts +28 -0
- package/server/middleware/cms-limits.ts +20 -0
- package/server/middleware/cms-redirects.ts +21 -0
- package/server/middleware/cms-session.ts +41 -0
- package/server/middleware/cms-z-plugin-access.ts +15 -0
- package/server/plugins/cms-0-config-check.ts +35 -0
- package/server/plugins/cms-changes.ts +53 -0
- package/server/plugins/cms-content-etag.ts +21 -0
- package/server/plugins/cms-errors.ts +45 -0
- package/server/plugins/cms-migrate.ts +5 -0
- package/server/plugins/cms-plugins.ts +17 -0
- package/server/plugins/cms-search.ts +12 -0
- package/server/plugins/cms-setup-code.ts +15 -0
- package/server/routes/sitemap.xml.get.ts +25 -0
- package/server/store/redis.ts +95 -0
- package/server/store/types.ts +21 -0
- package/server/tasks/cms/retention.ts +10 -0
- package/server/tasks/cms/scheduled.ts +10 -0
- package/server/tasks/cms/search-reindex.ts +12 -0
- package/server/utils/cmsAudit.ts +29 -0
- package/server/utils/cmsAuth.ts +143 -0
- package/server/utils/cmsBlocks.ts +47 -0
- package/server/utils/cmsChanges.ts +222 -0
- package/server/utils/cmsCollab.ts +286 -0
- package/server/utils/cmsCollabServer.ts +451 -0
- package/server/utils/cmsCollate.ts +27 -0
- package/server/utils/cmsContent.ts +376 -0
- package/server/utils/cmsCrud.ts +361 -0
- package/server/utils/cmsCsv.ts +24 -0
- package/server/utils/cmsDb.ts +23 -0
- package/server/utils/cmsDemo.ts +36 -0
- package/server/utils/cmsEmails.ts +125 -0
- package/server/utils/cmsErrors.ts +40 -0
- package/server/utils/cmsFold.ts +17 -0
- package/server/utils/cmsImageSizes.ts +51 -0
- package/server/utils/cmsJson.ts +6 -0
- package/server/utils/cmsLinkAddress.ts +50 -0
- package/server/utils/cmsLinks.ts +248 -0
- package/server/utils/cmsListQuery.ts +93 -0
- package/server/utils/cmsLive.ts +393 -0
- package/server/utils/cmsLocales.ts +96 -0
- package/server/utils/cmsMail.ts +28 -0
- package/server/utils/cmsMediaGc.ts +188 -0
- package/server/utils/cmsMediaKeys.ts +62 -0
- package/server/utils/cmsMigrate.ts +86 -0
- package/server/utils/cmsPreviewDraft.ts +72 -0
- package/server/utils/cmsPrivacy.ts +176 -0
- package/server/utils/cmsPublished.ts +57 -0
- package/server/utils/cmsRange.ts +38 -0
- package/server/utils/cmsRateLimit.ts +82 -0
- package/server/utils/cmsRedirects.ts +160 -0
- package/server/utils/cmsRetention.ts +61 -0
- package/server/utils/cmsRevisions.ts +103 -0
- package/server/utils/cmsScheduler.ts +45 -0
- package/server/utils/cmsScrub.ts +107 -0
- package/server/utils/cmsSearch.ts +354 -0
- package/server/utils/cmsSession.ts +102 -0
- package/server/utils/cmsSetupCode.ts +97 -0
- package/server/utils/cmsSitemap.ts +176 -0
- package/server/utils/cmsStorage.ts +580 -0
- package/server/utils/cmsStore.ts +109 -0
- package/server/utils/cmsTiming.ts +14 -0
- package/server/utils/cmsTooMany.ts +9 -0
- package/server/utils/cmsTranslationWrite.ts +186 -0
- package/server/utils/cmsTree.ts +251 -0
- package/server/utils/cmsTreeAddress.ts +65 -0
- package/server/utils/cmsUpload.ts +228 -0
- package/server/utils/cmsWebhooks.ts +104 -0
- package/shared/json.ts +11 -0
- package/shared/richtext-editor.ts +56 -0
- package/shared/richtext-normalize.ts +33 -0
- package/shared/richtext-paste.ts +151 -0
- package/shared/storage-delivery.ts +53 -0
- package/src/blocks.ts +624 -0
- package/src/changes.ts +54 -0
- package/src/content-shape.ts +340 -0
- package/src/deprecate.ts +64 -0
- package/src/descriptor.ts +149 -0
- package/src/docs.ts +27 -0
- package/src/emitter.ts +94 -0
- package/src/entity.ts +624 -0
- package/src/errorReport.ts +41 -0
- package/src/fields.ts +465 -0
- package/src/i18n.ts +26 -0
- package/src/index.ts +54 -0
- package/src/linkref.ts +31 -0
- package/src/links.ts +179 -0
- package/src/locales.ts +39 -0
- package/src/media.ts +48 -0
- package/src/migrate.ts +236 -0
- package/src/openapi.ts +377 -0
- package/src/paths.ts +88 -0
- package/src/plugin.ts +505 -0
- package/src/presets.ts +230 -0
- package/src/richtext.ts +88 -0
- package/src/sdk.ts +181 -0
- package/src/search.ts +312 -0
- package/src/seed.ts +225 -0
- package/src/setupCode.ts +30 -0
- package/src/system.ts +219 -0
- package/src/tree.ts +421 -0
- package/src/upgrades.ts +137 -0
- package/starter/.env.example +10 -0
- package/starter/README.md +65 -0
- package/starter/app/app.config.ts +10 -0
- package/starter/app/app.vue +12 -0
- package/starter/app/assets/css/main.css +15 -0
- package/starter/app/components/global/BlockContact.vue +15 -0
- package/starter/app/components/global/BlockCta.vue +15 -0
- package/starter/app/components/global/BlockHero.vue +23 -0
- package/starter/app/components/global/BlockImage.vue +13 -0
- package/starter/app/components/global/BlockText.vue +12 -0
- package/starter/app/composables/useSite.ts +77 -0
- package/starter/app/error.vue +28 -0
- package/starter/app/layouts/default.vue +106 -0
- package/starter/app/lib/cms-content.ts +152 -0
- package/starter/app/pages/[...slug].vue +66 -0
- package/starter/app/pages/hledat.vue +59 -0
- package/starter/app/pages/index.vue +20 -0
- package/starter/cms.config.ts +137 -0
- package/starter/compose.yaml +15 -0
- package/starter/drizzle.config.ts +8 -0
- package/starter/gitignore.txt +8 -0
- package/starter/nuxt.config.ts +13 -0
- package/starter/package.json +38 -0
- package/starter/scripts/typecheck.mjs +38 -0
- package/starter/seed/index.ts +297 -0
- package/starter/seed/media/about.jpg +0 -0
- package/starter/seed/media/hero.jpg +0 -0
- package/starter/seed/media/logo.png +0 -0
- package/starter/seed/media/news-bread.jpg +0 -0
- package/starter/seed/media/news-morning.jpg +0 -0
- package/starter/server/db/migrations/0000_init.sql +269 -0
- package/starter/server/db/migrations/0001_page_tree.sql +5 -0
- package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
- package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
- package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
- package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
- package/starter/server/db/migrations/meta/_journal.json +27 -0
- package/starter/server/db/schema.ts +253 -0
- package/starter/server/plugins/sitemap.ts +11 -0
- package/starter/server/routes/robots.txt.get.ts +12 -0
- package/starter/tsconfig.json +9 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import type { Ref } from 'vue'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared editing for one open entry (or one translation): the rich-text fields are
|
|
5
|
+
* typed in together, with everyone's cursor shown. Server side: cmsCollab.ts and
|
|
6
|
+
* cmsCollabServer.ts.
|
|
7
|
+
*
|
|
8
|
+
* The page provides this as `cmsCollab`; the rich-text field picks it up when its
|
|
9
|
+
* field is one of `fields`. Until the shared document is ready the field shows that
|
|
10
|
+
* it is connecting; if it cannot be reached (`status` 'off') the fields edit on
|
|
11
|
+
* their own, exactly as before.
|
|
12
|
+
*/
|
|
13
|
+
export interface CmsCollab {
|
|
14
|
+
status: Ref<'connecting' | 'ready' | 'off'>
|
|
15
|
+
/** Bumped when the document restarts (a newer history replaced ours): editors remount. */
|
|
16
|
+
generation: Ref<number>
|
|
17
|
+
readOnly: Ref<boolean>
|
|
18
|
+
fields: Ref<Set<string>>
|
|
19
|
+
epoch: Ref<string | null>
|
|
20
|
+
/** Text typed here while offline that the restarted document could not take back. */
|
|
21
|
+
lost: Ref<Record<string, any> | null>
|
|
22
|
+
/** Text a closed tab left behind unsaved, aside until someone keeps or discards it. */
|
|
23
|
+
unsaved: Ref<CmsUnsavedText | null>
|
|
24
|
+
/** The left-over text is dealt with: forget it (for everyone). */
|
|
25
|
+
clearUnsaved: () => Promise<void>
|
|
26
|
+
/** Fields whose shared editor is on the page right now (a collapsed group has none). */
|
|
27
|
+
mounted: Ref<Set<string>>
|
|
28
|
+
/** Current Yjs document and provider (non-reactive on purpose). */
|
|
29
|
+
current: () => { doc: any; provider: any } | null
|
|
30
|
+
/** Put a value into a shared field, for everyone (an explicit choice: load, copy). */
|
|
31
|
+
set: (field: string, value: any) => boolean
|
|
32
|
+
/** Used by the shared editors to offer `set`. */
|
|
33
|
+
setters: Map<string, (value: any) => void>
|
|
34
|
+
user: { name: string; color: string }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CmsUnsavedText { by: string | null; at: string; values: Record<string, any> }
|
|
38
|
+
|
|
39
|
+
const COLORS = ['#e5484d', '#f76b15', '#ffc53d', '#46a758', '#12a594', '#0090ff', '#8e4ec6', '#d6409f']
|
|
40
|
+
const colorOf = (id: string) => COLORS[[...id].reduce((h, c) => (h * 31 + c.charCodeAt(0)) >>> 0, 7) % COLORS.length]!
|
|
41
|
+
|
|
42
|
+
export function useCmsCollab(opts: { entity: string; id: string; locale: string | null }): CmsCollab {
|
|
43
|
+
const { user } = useUserSession()
|
|
44
|
+
const me = (user.value ?? {}) as { id?: string; name?: string; email?: string }
|
|
45
|
+
const collab: CmsCollab = {
|
|
46
|
+
status: ref(import.meta.server ? 'connecting' : 'connecting'),
|
|
47
|
+
generation: ref(0),
|
|
48
|
+
readOnly: ref(false),
|
|
49
|
+
fields: ref(new Set<string>()),
|
|
50
|
+
epoch: ref(null),
|
|
51
|
+
mounted: ref(new Set<string>()),
|
|
52
|
+
lost: ref(null),
|
|
53
|
+
unsaved: ref(null),
|
|
54
|
+
clearUnsaved: async () => {
|
|
55
|
+
collab.unsaved.value = null
|
|
56
|
+
await cmsFetch(`/api/cms/_collab/unsaved?${query()}`, { method: 'DELETE' }).catch(() => {})
|
|
57
|
+
},
|
|
58
|
+
current: () => live,
|
|
59
|
+
setters: new Map(),
|
|
60
|
+
set: (field, value) => {
|
|
61
|
+
const fn = collab.setters.get(field)
|
|
62
|
+
if (!fn) return false
|
|
63
|
+
fn(value)
|
|
64
|
+
return true
|
|
65
|
+
},
|
|
66
|
+
user: { name: me.name || (me.email ?? '').split('@')[0] || '?', color: colorOf(me.id ?? 'x') },
|
|
67
|
+
}
|
|
68
|
+
provide('cmsCollab', collab)
|
|
69
|
+
if (import.meta.server) return collab
|
|
70
|
+
|
|
71
|
+
let live: { doc: any; provider: any; socket: any } | null = null
|
|
72
|
+
let disposed = false
|
|
73
|
+
let restarts = 0
|
|
74
|
+
let syncTimer: ReturnType<typeof setTimeout> | null = null
|
|
75
|
+
const base = (useRuntimeConfig().app.baseURL || '/').replace(/\/+$/, '')
|
|
76
|
+
|
|
77
|
+
function teardown() {
|
|
78
|
+
if (syncTimer) clearTimeout(syncTimer)
|
|
79
|
+
syncTimer = null
|
|
80
|
+
live?.provider.destroy()
|
|
81
|
+
live?.socket.destroy()
|
|
82
|
+
live?.doc.destroy()
|
|
83
|
+
live = null
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const query = () => new URLSearchParams({ entity: opts.entity, id: opts.id, ...(opts.locale ? { locale: opts.locale } : {}) }).toString()
|
|
87
|
+
|
|
88
|
+
async function start() {
|
|
89
|
+
let session: { name: string; epoch: string; fields: string[]; access: 'write' | 'read'; you?: string; unsaved?: CmsUnsavedText | null }
|
|
90
|
+
try {
|
|
91
|
+
session = await cmsFetch(`/api/cms/_collab/session?${query()}`)
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
collab.status.value = 'off' // nothing to share, not permitted, or unavailable
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
if (disposed) return
|
|
98
|
+
// The server checks the name on our cursor against the account: use its spelling.
|
|
99
|
+
if (session.you) collab.user.name = session.you
|
|
100
|
+
collab.unsaved.value = session.unsaved ?? null
|
|
101
|
+
const [{ HocuspocusProvider, HocuspocusProviderWebsocket }, Y] = await Promise.all([import('@hocuspocus/provider'), import('yjs')])
|
|
102
|
+
if (disposed) return
|
|
103
|
+
const doc = new Y.Doc()
|
|
104
|
+
const socket = new HocuspocusProviderWebsocket({
|
|
105
|
+
url: (location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + location.host + base + '/api/cms/_collab/socket',
|
|
106
|
+
autoConnect: false,
|
|
107
|
+
})
|
|
108
|
+
const provider = new HocuspocusProvider({
|
|
109
|
+
websocketProvider: socket,
|
|
110
|
+
name: session.name,
|
|
111
|
+
document: doc,
|
|
112
|
+
token: session.epoch,
|
|
113
|
+
onSynced: () => {
|
|
114
|
+
if (syncTimer) clearTimeout(syncTimer)
|
|
115
|
+
restarts = 0
|
|
116
|
+
collab.status.value = 'ready'
|
|
117
|
+
},
|
|
118
|
+
onAuthenticationFailed: async () => {
|
|
119
|
+
// Our history is no longer the document's (it restarted from a newer save
|
|
120
|
+
// while we were away), or access changed: start over, a couple of times —
|
|
121
|
+
// keeping a copy of anything typed here that never reached the others.
|
|
122
|
+
const old = live
|
|
123
|
+
if (old?.provider.hasUnsyncedChanges) {
|
|
124
|
+
const { yXmlFragmentToProsemirrorJSON } = await import('@tiptap/y-tiptap')
|
|
125
|
+
const keep: Record<string, any> = {}
|
|
126
|
+
for (const key of collab.fields.value) keep[key] = yXmlFragmentToProsemirrorJSON(old.doc.getXmlFragment(key))
|
|
127
|
+
collab.lost.value = keep
|
|
128
|
+
}
|
|
129
|
+
if (live !== old) return
|
|
130
|
+
teardown()
|
|
131
|
+
if (disposed) return
|
|
132
|
+
if (++restarts > 2) {
|
|
133
|
+
collab.status.value = 'off'
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
collab.status.value = 'connecting'
|
|
137
|
+
collab.generation.value++
|
|
138
|
+
void start()
|
|
139
|
+
},
|
|
140
|
+
})
|
|
141
|
+
provider.attach()
|
|
142
|
+
// A refused or failed connection is handled by the timeout below (and retried by
|
|
143
|
+
// the provider); it must not surface as an unhandled rejection.
|
|
144
|
+
socket.connect().catch(() => {})
|
|
145
|
+
live = { doc, provider, socket }
|
|
146
|
+
collab.fields.value = new Set(session.fields)
|
|
147
|
+
collab.readOnly.value = session.access === 'read'
|
|
148
|
+
collab.epoch.value = session.epoch
|
|
149
|
+
// Not reachable (a proxy without WebSockets): edit on our own after a few seconds.
|
|
150
|
+
if (collab.status.value !== 'ready') {
|
|
151
|
+
syncTimer = setTimeout(() => {
|
|
152
|
+
if (collab.status.value === 'ready') return
|
|
153
|
+
teardown()
|
|
154
|
+
collab.status.value = 'off'
|
|
155
|
+
}, 6_000)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
onMounted(() => { void start() })
|
|
160
|
+
onScopeDispose(() => {
|
|
161
|
+
disposed = true
|
|
162
|
+
teardown()
|
|
163
|
+
})
|
|
164
|
+
return collab
|
|
165
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Ask before something that cannot be taken back (leaving unsaved changes, discarding,
|
|
2
|
+
// deleting) — in the admin's own dialog, not the browser's grey confirm box.
|
|
3
|
+
//
|
|
4
|
+
// const confirm = useCmsConfirm()
|
|
5
|
+
// if (!await confirm({ title: t('media.deleteTitle'), confirmLabel: t('common.delete'), color: 'error' })) return
|
|
6
|
+
//
|
|
7
|
+
// The admin layout renders the one dialog (CmsConfirmDialog). Asking again while a
|
|
8
|
+
// question is open answers the old one with "no".
|
|
9
|
+
// (Closing the browser tab with unsaved changes still gets the browser's own prompt:
|
|
10
|
+
// pages cannot draw anything there.)
|
|
11
|
+
export interface CmsConfirmOptions {
|
|
12
|
+
title: string
|
|
13
|
+
description?: string
|
|
14
|
+
/** The button that goes ahead, e.g. "Leave without saving". */
|
|
15
|
+
confirmLabel?: string
|
|
16
|
+
/** The safe way out, e.g. "Stay". */
|
|
17
|
+
cancelLabel?: string
|
|
18
|
+
icon?: string
|
|
19
|
+
/** error: something is deleted for good (red). warning / primary stay calm and only pick the default icon. */
|
|
20
|
+
color?: 'error' | 'warning' | 'primary'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface CmsConfirmQuestion extends CmsConfirmOptions { resolve: (ok: boolean) => void }
|
|
24
|
+
|
|
25
|
+
// Only ever set in the browser (the dialog opens on a click), so one per tab is right.
|
|
26
|
+
const question = shallowRef<CmsConfirmQuestion | null>(null)
|
|
27
|
+
|
|
28
|
+
export function useCmsConfirm() {
|
|
29
|
+
return (options: CmsConfirmOptions) => new Promise<boolean>((resolve) => {
|
|
30
|
+
if (import.meta.server) return resolve(false)
|
|
31
|
+
question.value?.resolve(false)
|
|
32
|
+
question.value = { ...options, resolve }
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The question being asked (for the dialog, and for shortcuts that should wait while it is open). */
|
|
37
|
+
export function useCmsConfirmQuestion() {
|
|
38
|
+
return question
|
|
39
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Entry actions shared by the editor's "…" menu and the list's row menu: duplicate
|
|
2
|
+
// (a draft copy, opened at once) and delete (asked first; a refusal names what still
|
|
3
|
+
// uses the entry).
|
|
4
|
+
export function useCmsEntryActions() {
|
|
5
|
+
const { t, tl } = useCmsI18n()
|
|
6
|
+
const toast = useToast()
|
|
7
|
+
const confirm = useCmsConfirm()
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A copy of the entry as a new draft: "(copy)" after the title, "-copy" after
|
|
11
|
+
* unique addresses, not published. Translations are not copied (the copy is a new
|
|
12
|
+
* thing to translate). Returns the new id, or null when the copy was refused.
|
|
13
|
+
*/
|
|
14
|
+
async function duplicate(entity: string, meta: any, id: string): Promise<string | null> {
|
|
15
|
+
try {
|
|
16
|
+
const { row } = await cmsFetch<{ row: Record<string, any> }>(`/api/cms/${entity}/${id}`)
|
|
17
|
+
const body: Record<string, any> = {}
|
|
18
|
+
for (const f of (meta?.fields ?? []) as any[]) {
|
|
19
|
+
if (f.relation?.kind === 'one-to-many' || f.key === meta?.orderField) continue
|
|
20
|
+
const v = row[f.key]
|
|
21
|
+
if (v === undefined || (v === null && f.optional)) continue
|
|
22
|
+
body[f.key] = v
|
|
23
|
+
}
|
|
24
|
+
const tf = meta?.titleField
|
|
25
|
+
if (tf && typeof body[tf] === 'string') body[tf] = `${body[tf]} ${t('editor.copySuffix')}`
|
|
26
|
+
const p = meta?.publish
|
|
27
|
+
if (p) {
|
|
28
|
+
body[p.field] = p.draftValue
|
|
29
|
+
if (p.dateField) body[p.dateField] = null
|
|
30
|
+
}
|
|
31
|
+
// Unique values get "-copy", then "-copy-2", "-copy-3"… until one is free.
|
|
32
|
+
const unique = ((meta?.fields ?? []) as any[]).filter((f) => (f.type === 'slug' || f.unique) && typeof body[f.key] === 'string' && body[f.key])
|
|
33
|
+
const original = Object.fromEntries(unique.map((f) => [f.key, body[f.key] as string]))
|
|
34
|
+
let res: { row: { id: string } } | null = null
|
|
35
|
+
for (let n = 1; n <= 8 && !res; n++) {
|
|
36
|
+
for (const f of unique) body[f.key] = `${original[f.key]}-${t('editor.copySlug')}${n > 1 ? `-${n}` : ''}`
|
|
37
|
+
try {
|
|
38
|
+
res = await cmsFetch<{ row: { id: string } }>(`/api/cms/${entity}`, { method: 'POST', body })
|
|
39
|
+
}
|
|
40
|
+
catch (err: any) {
|
|
41
|
+
const taken = err?.statusCode === 409 && Array.isArray(err?.data?.data) && err.data.data.some((i: any) => i?.code === 'unique')
|
|
42
|
+
if (!taken || !unique.length || n === 8) throw err
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!res) return null
|
|
46
|
+
toast.add({ title: t('editor.duplicated'), icon: 'i-lucide-copy', color: 'success' })
|
|
47
|
+
return res.row.id
|
|
48
|
+
}
|
|
49
|
+
catch (err: any) {
|
|
50
|
+
toast.add({ title: t('editor.duplicateFailed'), description: cmsRequestError(err, t as any), icon: 'i-lucide-x', color: 'error' })
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Delete after asking. True when it is gone. */
|
|
56
|
+
async function remove(entity: string, meta: any, id: string, title?: string | null): Promise<boolean> {
|
|
57
|
+
const ok = await confirm({
|
|
58
|
+
title: t('editor.deleteTitle', { name: title || tl(meta?.labelSingular, entity) }),
|
|
59
|
+
description: t('editor.deleteBody'), icon: 'i-lucide-trash-2', color: 'error',
|
|
60
|
+
confirmLabel: t('common.delete'), cancelLabel: t('common.cancel'),
|
|
61
|
+
})
|
|
62
|
+
if (!ok) return false
|
|
63
|
+
try {
|
|
64
|
+
await cmsFetch(`/api/cms/${entity}/${id}`, { method: 'DELETE' })
|
|
65
|
+
toast.add({ title: t('list.deleted'), icon: 'i-lucide-check', color: 'success' })
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
catch (err: any) {
|
|
69
|
+
toast.add({ ...cmsDeleteFailure(err, t as any, (label) => tl(label as any)), icon: 'i-lucide-x', color: 'error' })
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { duplicate, remove }
|
|
75
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Focus mode: while a page that needs the room is open (the Write and Preview
|
|
3
|
+
* editors), the admin's sidebar folds to icons. The page calls `useCmsFocus(true)`;
|
|
4
|
+
* leaving the page unfolds it again — to whatever the person chose themselves.
|
|
5
|
+
*/
|
|
6
|
+
export function useCmsFocus(on?: MaybeRefOrGetter<boolean>) {
|
|
7
|
+
const focus = useState('cms-focus', () => false)
|
|
8
|
+
if (on !== undefined) {
|
|
9
|
+
watch(() => toValue(on), (v) => { focus.value = v }, { immediate: true })
|
|
10
|
+
onBeforeUnmount(() => { focus.value = false })
|
|
11
|
+
}
|
|
12
|
+
return focus
|
|
13
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import type { ComputedRef } from 'vue'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The editing state shared by the entry editor and the settings (singleton) form:
|
|
5
|
+
* field values, field errors, the unsaved-changes guard, Ctrl/⌘+S, and turning a
|
|
6
|
+
* failed save into inline field errors or a toast.
|
|
7
|
+
*/
|
|
8
|
+
export function useCmsForm(fields: ComputedRef<any[]>, isNew: ComputedRef<boolean>) {
|
|
9
|
+
const { t } = useCmsI18n()
|
|
10
|
+
const toast = useToast()
|
|
11
|
+
|
|
12
|
+
const state = reactive<Record<string, any>>({})
|
|
13
|
+
const fieldErrors = reactive<Record<string, string>>({})
|
|
14
|
+
// Every issue by its full path ("sections.2.heading"), for fields that hold
|
|
15
|
+
// nested values (blocks) and want to show an error inside a collapsed part.
|
|
16
|
+
const issues = reactive<Record<string, string>>({})
|
|
17
|
+
// Field components (e.g. slug auto-fill, blocks) read sibling values, create-mode
|
|
18
|
+
// and nested issues.
|
|
19
|
+
provide('cmsForm', { state, isNew, issues, quiet: (fn: () => void) => quiet(fn) })
|
|
20
|
+
|
|
21
|
+
const editable = computed(() => fields.value.filter((f: any) => f.relation?.kind !== 'one-to-many'))
|
|
22
|
+
|
|
23
|
+
function blankFor(f: any) {
|
|
24
|
+
// A new entry starts with the field's default (e.g. a select's first choice).
|
|
25
|
+
if (isNew.value && f.default != null) return structuredClone(f.default)
|
|
26
|
+
if (f.type === 'boolean') return false
|
|
27
|
+
if (f.relation?.kind === 'many-to-many') return []
|
|
28
|
+
if (f.type === 'richtext') return { type: 'doc', content: [] }
|
|
29
|
+
if (f.type === 'blocks') return []
|
|
30
|
+
// A group: an object of its fields (an optional one stays empty until something is typed).
|
|
31
|
+
if (f.type === 'group') return f.optional ? null : {}
|
|
32
|
+
return undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The values as last loaded from the server, per field (JSON), to tell which
|
|
36
|
+
// fields this tab changed — "save my changes on top" sends only those.
|
|
37
|
+
let loaded: Record<string, string> = {}
|
|
38
|
+
const json = cmsFormJson
|
|
39
|
+
|
|
40
|
+
/** Fill with a loaded row (and blanks for anything missing). Not a user edit. */
|
|
41
|
+
function load(row: Record<string, any> | null | undefined) {
|
|
42
|
+
Object.assign(state, row ?? {})
|
|
43
|
+
for (const f of editable.value) if (!(f.key in state)) state[f.key] = blankFor(f)
|
|
44
|
+
loaded = Object.fromEntries(editable.value.map((f: any) => [f.key, json(state[f.key])]))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The fields whose value differs from what was last loaded, with this tab's values. */
|
|
48
|
+
function changedFields(): Record<string, any> {
|
|
49
|
+
const out: Record<string, any> = {}
|
|
50
|
+
for (const f of editable.value) {
|
|
51
|
+
if (json(state[f.key]) !== loaded[f.key]) out[f.key] = structuredClone(toRaw(state[f.key]))
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The request body: every editable field, minus empty optional values. */
|
|
57
|
+
function body(): Record<string, any> {
|
|
58
|
+
const out: Record<string, any> = {}
|
|
59
|
+
for (const f of editable.value) {
|
|
60
|
+
const v = state[f.key]
|
|
61
|
+
if ((v === '' || v === undefined) && (f.optional || f.relation?.kind === 'many-to-many')) continue
|
|
62
|
+
out[f.key] = v
|
|
63
|
+
}
|
|
64
|
+
return out
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Unsaved changes: edits made after the initial load mark the form dirty.
|
|
68
|
+
const dirty = ref(false)
|
|
69
|
+
let ready = false
|
|
70
|
+
// Counts edits, so a save can tell whether anything was typed while it ran.
|
|
71
|
+
let revision = 0
|
|
72
|
+
// Unsaved = differs from what was last loaded or saved (editing a field back to its
|
|
73
|
+
// saved value, or discarding, makes the form clean again).
|
|
74
|
+
const differs = () => editable.value.some((f: any) => json(state[f.key]) !== loaded[f.key])
|
|
75
|
+
// A field's error goes away as soon as its value changes (the save checks again).
|
|
76
|
+
const erroredAt: Record<string, string> = {}
|
|
77
|
+
function clearFixedErrors() {
|
|
78
|
+
for (const k of Object.keys(fieldErrors)) {
|
|
79
|
+
if (json(state[k]) === erroredAt[k]) continue
|
|
80
|
+
delete fieldErrors[k]
|
|
81
|
+
for (const p of Object.keys(issues)) if (p === k || p.startsWith(`${k}.`)) delete issues[p]
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
watch(state, () => {
|
|
85
|
+
if (Object.keys(fieldErrors).length) clearFixedErrors()
|
|
86
|
+
if (!ready) return
|
|
87
|
+
revision++
|
|
88
|
+
dirty.value = differs()
|
|
89
|
+
}, { deep: true })
|
|
90
|
+
// Shared text (rich text typed together, see useCmsCollab) is not kept in this tab
|
|
91
|
+
// but in the shared document, which outlives the visit: the page tells us about it.
|
|
92
|
+
let shared: CmsSharedText | null = null
|
|
93
|
+
/** Whether leaving now would lose unsaved work — not when the only changes are
|
|
94
|
+
* shared text someone else is still editing (it stays with them). */
|
|
95
|
+
function leaveLoses(): boolean {
|
|
96
|
+
if (!dirty.value) return false
|
|
97
|
+
if (!shared?.othersHere()) return true
|
|
98
|
+
const fields = shared.fields()
|
|
99
|
+
return Object.keys(changedFields()).some((k) => !fields.has(k))
|
|
100
|
+
}
|
|
101
|
+
/** Leaving without saving: put the saved text back into the shared document, so it
|
|
102
|
+
* is not waiting there next time (unless others are still typing in it). */
|
|
103
|
+
function leaveWithoutSaving() {
|
|
104
|
+
if (!shared || shared.othersHere()) return
|
|
105
|
+
for (const key of shared.fields()) {
|
|
106
|
+
if (json(state[key]) !== loaded[key]) shared.reset(key, JSON.parse(loaded[key] ?? 'null'))
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function onBeforeUnload(e: BeforeUnloadEvent) {
|
|
110
|
+
if (!leaveLoses()) return
|
|
111
|
+
e.preventDefault()
|
|
112
|
+
e.returnValue = ''
|
|
113
|
+
}
|
|
114
|
+
onMounted(() => {
|
|
115
|
+
ready = true
|
|
116
|
+
dirty.value = differs()
|
|
117
|
+
window.addEventListener('beforeunload', onBeforeUnload)
|
|
118
|
+
})
|
|
119
|
+
onBeforeUnmount(() => window.removeEventListener('beforeunload', onBeforeUnload))
|
|
120
|
+
const confirm = useCmsConfirm()
|
|
121
|
+
async function askToLeave() {
|
|
122
|
+
if (!leaveLoses()) return true
|
|
123
|
+
const leave = await confirm({
|
|
124
|
+
title: t('editor.leaveTitle'), description: t('editor.unsavedConfirm'), icon: 'i-lucide-file-pen-line',
|
|
125
|
+
cancelLabel: t('editor.stay'), confirmLabel: t('editor.leave'), color: 'warning',
|
|
126
|
+
})
|
|
127
|
+
if (leave) leaveWithoutSaving()
|
|
128
|
+
return leave
|
|
129
|
+
}
|
|
130
|
+
onBeforeRouteLeave(askToLeave)
|
|
131
|
+
// Another language (?locale=…) is another form on the same page: same question.
|
|
132
|
+
onBeforeRouteUpdate((to, from) => (to.path !== from.path || to.query.locale !== from.query.locale ? askToLeave() : true))
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Load a newer version over the form (someone else saved): not a user edit, so the
|
|
136
|
+
* form is clean afterwards. Fields the row no longer has are cleared.
|
|
137
|
+
*/
|
|
138
|
+
async function replace(row: Record<string, any> | null | undefined) {
|
|
139
|
+
ready = false
|
|
140
|
+
for (const k of Object.keys(state)) if (!(k in (row ?? {}))) delete state[k]
|
|
141
|
+
load(row)
|
|
142
|
+
await nextTick()
|
|
143
|
+
// Usually clean; not when shared text here is ahead of the saved version.
|
|
144
|
+
dirty.value = differs()
|
|
145
|
+
conflict.value = null
|
|
146
|
+
ready = true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Bring in a newer saved version without losing edits made here — field by field,
|
|
151
|
+
* against what was last loaded: a field only they changed takes their value, a
|
|
152
|
+
* field only we changed keeps ours, the same change on both sides is fine. Returns
|
|
153
|
+
* false (and changes nothing) when both sides changed a field differently.
|
|
154
|
+
* `skip`: fields that merge by themselves (shared text).
|
|
155
|
+
*/
|
|
156
|
+
async function merge(row: Record<string, any>, skip: Set<string> = new Set()): Promise<boolean> {
|
|
157
|
+
const theirs: Record<string, any> = {}
|
|
158
|
+
for (const f of editable.value) {
|
|
159
|
+
const k = f.key
|
|
160
|
+
if (skip.has(k) || !(k in row)) continue
|
|
161
|
+
const base = loaded[k]
|
|
162
|
+
const mine = json(state[k])
|
|
163
|
+
const next = json(row[k])
|
|
164
|
+
if (next === base || next === mine) continue
|
|
165
|
+
if (mine !== base) return false
|
|
166
|
+
theirs[k] = row[k]
|
|
167
|
+
}
|
|
168
|
+
ready = false
|
|
169
|
+
const keys = new Set(editable.value.map((f: any) => f.key))
|
|
170
|
+
// Their values, plus everything that is not a field (version, languages…).
|
|
171
|
+
for (const [k, v] of Object.entries(row)) if (!keys.has(k) || k in theirs) state[k] = v
|
|
172
|
+
await nextTick()
|
|
173
|
+
loaded = Object.fromEntries(editable.value.map((f: any) => [f.key, json(row[f.key])]))
|
|
174
|
+
dirty.value = editable.value.some((f: any) => json(state[f.key]) !== loaded[f.key])
|
|
175
|
+
ready = true
|
|
176
|
+
return true
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Apply a change that is not this tab's edit (someone else typing in shared text,
|
|
181
|
+
* or text left in the shared document since the last save): it is no edit of this
|
|
182
|
+
* tab (a save still sends only what this tab changed), but the status stays
|
|
183
|
+
* honest — text that differs from the saved version is unsaved, whoever typed it.
|
|
184
|
+
*/
|
|
185
|
+
function quiet(fn: () => void) {
|
|
186
|
+
const wasReady = ready
|
|
187
|
+
ready = false
|
|
188
|
+
fn()
|
|
189
|
+
void nextTick(() => {
|
|
190
|
+
ready = wasReady || ready
|
|
191
|
+
if (ready) dirty.value = differs()
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Set when a save was refused because someone saved in between (409 stale). */
|
|
196
|
+
const conflict = ref<{ version: string; updatedByName: string | null } | null>(null)
|
|
197
|
+
|
|
198
|
+
const saving = ref(false)
|
|
199
|
+
/**
|
|
200
|
+
* Run a save. On a validation answer (422/409 with issues) the messages land on
|
|
201
|
+
* their fields; anything else becomes a toast. Returns whether it succeeded.
|
|
202
|
+
*/
|
|
203
|
+
/**
|
|
204
|
+
* Runs a save. `successTitle: null` skips the toast (the page shows its own moment).
|
|
205
|
+
* `run` may return `{ successTitle }` to say more than "Saved" (what the save also did).
|
|
206
|
+
*/
|
|
207
|
+
async function submit(run: () => Promise<unknown>, successTitle: string | null = t('editor.saved')): Promise<boolean> {
|
|
208
|
+
saving.value = true
|
|
209
|
+
Object.keys(fieldErrors).forEach((k) => delete fieldErrors[k])
|
|
210
|
+
Object.keys(issues).forEach((k) => delete issues[k])
|
|
211
|
+
// Let edits made just before the save (e.g. Publish setting the status) be
|
|
212
|
+
// counted first: they are part of this save, not typed during it.
|
|
213
|
+
await nextTick()
|
|
214
|
+
const startedAt = revision
|
|
215
|
+
try {
|
|
216
|
+
const outcome = await run()
|
|
217
|
+
const said = (outcome as { successTitle?: unknown } | null | undefined)?.successTitle
|
|
218
|
+
if (successTitle && typeof said === 'string') successTitle = said
|
|
219
|
+
// Edits typed while the request ran were not in it: they stay unsaved.
|
|
220
|
+
await nextTick()
|
|
221
|
+
dirty.value = revision !== startedAt
|
|
222
|
+
if (!dirty.value) loaded = Object.fromEntries(editable.value.map((f: any) => [f.key, json(state[f.key])]))
|
|
223
|
+
conflict.value = null
|
|
224
|
+
if (successTitle) toast.add({ title: successTitle, icon: 'i-lucide-check', color: 'success' })
|
|
225
|
+
return true
|
|
226
|
+
}
|
|
227
|
+
catch (e: any) {
|
|
228
|
+
if (e?.data?.data?.code === 'stale') {
|
|
229
|
+
conflict.value = e.data.data
|
|
230
|
+
// The notice sits at the top of the page; say it where the eye is too.
|
|
231
|
+
toast.add({ title: t('live.notSaved'), icon: 'i-lucide-git-compare-arrows', color: 'warning' })
|
|
232
|
+
return false
|
|
233
|
+
}
|
|
234
|
+
const issueList = e?.data?.data
|
|
235
|
+
if (Array.isArray(issueList) && issueList.some((i: any) => i?.path?.[0])) {
|
|
236
|
+
for (const i of issueList) {
|
|
237
|
+
const key = i.path?.[0]
|
|
238
|
+
if (!key) continue
|
|
239
|
+
const message = cmsIssueMessage(i, editable.value.find((f: any) => f.key === key), t as any)
|
|
240
|
+
fieldErrors[key] ??= message
|
|
241
|
+
erroredAt[key] = json(state[key])
|
|
242
|
+
issues[i.path.join('.')] = message
|
|
243
|
+
}
|
|
244
|
+
toast.add({ title: t('editor.fixFields'), icon: 'i-lucide-triangle-alert', color: 'error' })
|
|
245
|
+
// The cursor goes to the first field that is wrong, not back to <body>.
|
|
246
|
+
void cmsFocusInvalid(document.querySelector('.cms-editor'), `cms-field-${Object.keys(fieldErrors)[0]}`)
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
toast.add({ title: t('editor.saveFailed'), description: cmsRequestError(e, t as any), icon: 'i-lucide-x', color: 'error' })
|
|
250
|
+
}
|
|
251
|
+
return false
|
|
252
|
+
}
|
|
253
|
+
finally {
|
|
254
|
+
saving.value = false
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Tell the unsaved-changes guard which fields are shared text (see `shared` above). */
|
|
259
|
+
function setSharedText(s: CmsSharedText) {
|
|
260
|
+
shared = s
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return { state, fieldErrors, issues, editable, dirty, saving, conflict, load, replace, merge, body, submit, changedFields, setSharedText, quiet }
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface CmsSharedText {
|
|
267
|
+
/** The fields typed together on this page right now. */
|
|
268
|
+
fields: () => Set<string>
|
|
269
|
+
/** Whether someone else has this entry open (and so the shared text). */
|
|
270
|
+
othersHere: () => boolean
|
|
271
|
+
/** Put a value into a shared field, for everyone. */
|
|
272
|
+
reset: (key: string, value: any) => void
|
|
273
|
+
}
|