tinyzoo 0.0.0-stage → 0.2.0
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 +99 -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 +392 -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/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 +197 -0
- package/server/utils/cmsWebhooks.ts +101 -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,286 @@
|
|
|
1
|
+
import { and, eq, sql, type CmsEntity } from 'tinyzoo'
|
|
2
|
+
import * as Y from 'yjs'
|
|
3
|
+
import { prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON } from '@tiptap/y-tiptap'
|
|
4
|
+
import { editorSchema } from '../../shared/richtext-editor'
|
|
5
|
+
import { stableJson } from '../../shared/json'
|
|
6
|
+
import { withoutTrailingEmpty } from '../../shared/richtext-normalize'
|
|
7
|
+
import type { CmsUser } from './cmsSession'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Shared editing of rich text: several people type in the same text at once.
|
|
11
|
+
*
|
|
12
|
+
* One Yjs document per entry and language — `<entity>/<id>` for the entry itself,
|
|
13
|
+
* `<entity>/<id>/<locale>` for a translation — with one shared fragment per rich-text
|
|
14
|
+
* field (top-level fields only; text inside blocks is edited as before). The shared
|
|
15
|
+
* document is the working copy everyone types into; saving the entry stores its
|
|
16
|
+
* content exactly as before, so the content API, versions and webhooks are unchanged.
|
|
17
|
+
*
|
|
18
|
+
* The document's state lives in `cms_collab`, so every replica and every browser
|
|
19
|
+
* that reconnects shares one history. `epoch` names that history: a document is
|
|
20
|
+
* started afresh from the saved entry (new epoch) when nobody has it open and the
|
|
21
|
+
* entry was saved some other way since, or after a week of inactivity. A browser
|
|
22
|
+
* presents its epoch when it connects (the token); one holding an older history is
|
|
23
|
+
* refused and starts over instead of merging stale text back in.
|
|
24
|
+
*
|
|
25
|
+
* Text typed in a tab that was closed (or crashed) without saving stays in the
|
|
26
|
+
* document. The next person to open the entry with nobody else there does not get it
|
|
27
|
+
* as their own unsaved work: the document starts afresh from the saved entry, and
|
|
28
|
+
* the left-over text waits in a side row (`<name>~unsaved`, with who typed it and
|
|
29
|
+
* when) for them to keep or discard. `<name>~by` records who typed last. Both are
|
|
30
|
+
* ordinary rows of `cms_collab` under names no browser can open as a document, so
|
|
31
|
+
* they need no table of their own and are purged and dropped with it.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export interface CollabTarget { entity: CmsEntity; id: string; locale: string | null; name: string }
|
|
35
|
+
|
|
36
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
37
|
+
/** Someone is still editing it if its instance refreshed `active_at` this recently (seconds). */
|
|
38
|
+
const ACTIVE_WINDOW_S = 90
|
|
39
|
+
/** Unused this long, it starts afresh from the saved entry (seconds). */
|
|
40
|
+
const STALE_AFTER_S = 7 * 24 * 60 * 60
|
|
41
|
+
|
|
42
|
+
export function collabName(entity: string, id: string, locale: string | null): string {
|
|
43
|
+
return locale ? `${entity}/${id}/${locale}` : `${entity}/${id}`
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const unsavedName = (name: string) => `${name}~unsaved`
|
|
47
|
+
const byName = (name: string) => `${name}~by`
|
|
48
|
+
|
|
49
|
+
/** The text of one field as one comparable string: schema defaults filled in, trailing empty paragraphs ignored, empty = ''. */
|
|
50
|
+
function comparableText(value: unknown): string {
|
|
51
|
+
if (value === null || value === undefined) return ''
|
|
52
|
+
let v: any = value
|
|
53
|
+
try {
|
|
54
|
+
v = editorSchema().nodeFromJSON(value).toJSON()
|
|
55
|
+
}
|
|
56
|
+
catch { /* text the editor cannot hold is compared as stored */ }
|
|
57
|
+
v = withoutTrailingEmpty(v)
|
|
58
|
+
if (v?.type === 'doc' && !(v.content ?? []).length) return ''
|
|
59
|
+
return stableJson(v)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** What a stored document holds for each field, as editor JSON. */
|
|
63
|
+
export function documentValues(state: string, keys: string[]): Record<string, any> {
|
|
64
|
+
const doc = new Y.Doc()
|
|
65
|
+
try {
|
|
66
|
+
Y.applyUpdate(doc, decodeState(state))
|
|
67
|
+
return Object.fromEntries(keys.map((k) => [k, yXmlFragmentToProsemirrorJSON(doc.getXmlFragment(k))]))
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
doc.destroy()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The fields (with their text) where the shared document is ahead of the saved entry. */
|
|
75
|
+
export function leftoverText(state: string, saved: Record<string, any>): Record<string, any> {
|
|
76
|
+
const keys = Object.keys(saved)
|
|
77
|
+
const mine = documentValues(state, keys)
|
|
78
|
+
return Object.fromEntries(keys.filter((k) => comparableText(mine[k]) !== comparableText(saved[k])).map((k) => [k, mine[k]]))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The document a name points at, or null for a made-up one. */
|
|
82
|
+
export function parseCollabName(name: string): CollabTarget | null {
|
|
83
|
+
const m = /^([a-zA-Z][\w-]{0,63})\/([0-9a-fA-F-]{36})(?:\/([a-zA-Z-]{2,20}))?$/.exec(name)
|
|
84
|
+
if (!m || !UUID.test(m[2]!)) return null
|
|
85
|
+
const { reg } = useCms()
|
|
86
|
+
const entity = reg.byName.get(m[1]!)
|
|
87
|
+
if (!entity) return null
|
|
88
|
+
const locale = m[3] ?? null
|
|
89
|
+
if (locale !== null && !isTranslationLocale(reg, locale)) return null
|
|
90
|
+
const id = m[2]!.toLowerCase()
|
|
91
|
+
return { entity, id, locale, name: collabName(entity.name, id, locale) }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The fields edited together: top-level rich text (in a translation, the translatable ones). */
|
|
95
|
+
export function collabFields(e: CmsEntity, locale: string | null): string[] {
|
|
96
|
+
return Object.entries(e.fields)
|
|
97
|
+
.filter(([key, f]) => (f as any).type === 'richtext' && (!locale || e.localized.includes(key)))
|
|
98
|
+
.map(([key]) => key)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* What this person may do with the document: 'write' (may edit the entry, or this
|
|
103
|
+
* translation), 'read' (may read it: sees the others typing), or null.
|
|
104
|
+
*/
|
|
105
|
+
export async function collabAccess(user: CmsUser, t: CollabTarget): Promise<'write' | 'read' | null> {
|
|
106
|
+
const { db } = useCms()
|
|
107
|
+
const e = t.entity
|
|
108
|
+
if (!collabFields(e, t.locale).length) return null
|
|
109
|
+
let readFilter: any
|
|
110
|
+
try {
|
|
111
|
+
readFilter = authorize(e, 'read', user)
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return null
|
|
115
|
+
}
|
|
116
|
+
const [row] = await db.select({ id: e.table.id }).from(e.table).where(both(eq(e.table.id, t.id), readFilter))
|
|
117
|
+
if (!row) return null
|
|
118
|
+
try {
|
|
119
|
+
const writeFilter = authorize(e, 'update', user)
|
|
120
|
+
const [ok] = await db.select({ id: e.table.id }).from(e.table)
|
|
121
|
+
.where(both(both(eq(e.table.id, t.id), writeFilter), notLiveCond(e, user)))
|
|
122
|
+
return ok ? 'write' : 'read'
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return 'read'
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The entry's (or translation's) saved values of the shared fields, and its version.
|
|
131
|
+
* `db` may be a transaction: inside one, everything must go through it (a second
|
|
132
|
+
* pooled connection could wait forever for the one the transaction holds).
|
|
133
|
+
*/
|
|
134
|
+
export async function savedContent(t: CollabTarget, db: any = useCms().db): Promise<{ values: Record<string, any>; version: Date | null } | null> {
|
|
135
|
+
const e = t.entity
|
|
136
|
+
const keys = collabFields(e, t.locale)
|
|
137
|
+
const [row] = await db.select().from(e.table).where(eq(e.table.id, t.id))
|
|
138
|
+
if (!row) return null
|
|
139
|
+
if (!t.locale) return { values: Object.fromEntries(keys.map((k) => [k, row[k]])), version: row.updatedAt ?? null }
|
|
140
|
+
const tr = (await translationRows(db, e, [t.id], t.locale)).get(t.id)
|
|
141
|
+
return { values: Object.fromEntries(keys.map((k) => [k, tr?.[k] ?? null])), version: tr?.updatedAt ?? null }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Can the editor hold this stored rich text? Text written through the API may use
|
|
146
|
+
* nodes or marks the editor does not know; such a field is edited alone (not shared),
|
|
147
|
+
* so its stored content is never replaced by an empty shared copy.
|
|
148
|
+
*/
|
|
149
|
+
export function fitsEditor(value: unknown): boolean {
|
|
150
|
+
if (value === null || value === undefined) return true
|
|
151
|
+
try {
|
|
152
|
+
editorSchema().nodeFromJSON(value).check()
|
|
153
|
+
return true
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return false
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A fresh document holding the saved content, one fragment per field. */
|
|
161
|
+
export function seedDocument(values: Record<string, any>): Y.Doc {
|
|
162
|
+
const doc = new Y.Doc()
|
|
163
|
+
const schema = editorSchema()
|
|
164
|
+
for (const [key, value] of Object.entries(values)) {
|
|
165
|
+
const json = value && typeof value === 'object' && value.type === 'doc' ? value : { type: 'doc', content: [] }
|
|
166
|
+
try {
|
|
167
|
+
prosemirrorJSONToYXmlFragment(schema, json, doc.getXmlFragment(key))
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
// Content the editor's schema does not know (written by an API client): start
|
|
171
|
+
// that field empty rather than refuse the whole document.
|
|
172
|
+
console.error(`[cms] collab: could not load ${key}`, safeErrorMessage(err))
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return doc
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const encodeState = (doc: Y.Doc) => Buffer.from(Y.encodeStateAsUpdate(doc)).toString('base64')
|
|
179
|
+
export const decodeState = (state: string) => new Uint8Array(Buffer.from(state, 'base64'))
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The document to join: the current one, or — when nobody has it open and the saved
|
|
183
|
+
* entry moved on (or it sat unused for a week) — a fresh one from the saved entry.
|
|
184
|
+
* Serialised per document, so two people opening it at once get the same epoch.
|
|
185
|
+
*/
|
|
186
|
+
export async function ensureCollabDocument(t: CollabTarget): Promise<{ epoch: string }> {
|
|
187
|
+
const { db, reg } = useCms()
|
|
188
|
+
const c = reg.cmsCollab
|
|
189
|
+
return db.transaction(async (tx: any) => {
|
|
190
|
+
// Waiting for another request opening the same document: briefly, never forever.
|
|
191
|
+
await tx.execute(sql`set local lock_timeout = '5s'`)
|
|
192
|
+
await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${'cms-collab:' + t.name}))`)
|
|
193
|
+
// Times compared on the database's clock (replicas' clocks may differ).
|
|
194
|
+
const [row] = await tx.select({
|
|
195
|
+
epoch: c.epoch,
|
|
196
|
+
state: c.state,
|
|
197
|
+
baseVersion: c.baseVersion,
|
|
198
|
+
updatedAt: c.updatedAt,
|
|
199
|
+
active: sql<boolean>`coalesce(${c.activeAt} > now() - make_interval(secs => ${ACTIVE_WINDOW_S}), false)`,
|
|
200
|
+
fresh: sql<boolean>`${c.updatedAt} > now() - make_interval(secs => ${STALE_AFTER_S})`,
|
|
201
|
+
}).from(c).where(eq(c.name, t.name))
|
|
202
|
+
const saved = await savedContent(t, tx)
|
|
203
|
+
if (!saved) throw createError({ statusCode: 404, statusMessage: 'Not found' })
|
|
204
|
+
const sameBase = row && (row.baseVersion?.getTime() ?? null) === (saved.version?.getTime() ?? null)
|
|
205
|
+
if (row?.active) return { epoch: row.epoch }
|
|
206
|
+
if (row && sameBase && row.fresh) {
|
|
207
|
+
// Nobody has it open. Text in it that was never saved (a tab closed or crashed)
|
|
208
|
+
// is not this person's work: it waits aside, and they start from the saved entry.
|
|
209
|
+
// (A document stored before the entry's last save is behind it, not ahead: the
|
|
210
|
+
// save came from here and the rest of its text is still on its way in.)
|
|
211
|
+
if (saved.version && row.updatedAt.getTime() < saved.version.getTime()) return { epoch: row.epoch }
|
|
212
|
+
const left = leftoverText(row.state, saved.values)
|
|
213
|
+
if (!Object.keys(left).length) return { epoch: row.epoch }
|
|
214
|
+
const [who] = await tx.select({ name: c.state }).from(c).where(eq(c.name, byName(t.name)))
|
|
215
|
+
await putSideRow(tx, unsavedName(t.name), JSON.stringify({ by: who?.name ?? null, at: row.updatedAt.toISOString(), values: left }), saved.version)
|
|
216
|
+
}
|
|
217
|
+
const epoch = crypto.randomUUID()
|
|
218
|
+
const state = encodeState(seedDocument(saved.values))
|
|
219
|
+
await tx.insert(c).values({ name: t.name, epoch, state, baseVersion: saved.version, updatedAt: sql`now()`, activeAt: null })
|
|
220
|
+
.onConflictDoUpdate({ target: c.name, set: { epoch, state, baseVersion: saved.version, updatedAt: sql`now()`, activeAt: null } })
|
|
221
|
+
return { epoch }
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** A side row (see the top of this file): a note kept next to the document. */
|
|
226
|
+
async function putSideRow(db: any, name: string, note: string, version: Date | null) {
|
|
227
|
+
const c = useCms().reg.cmsCollab
|
|
228
|
+
await db.insert(c).values({ name, epoch: crypto.randomUUID(), state: note, baseVersion: version, updatedAt: sql`now()`, activeAt: null })
|
|
229
|
+
.onConflictDoUpdate({ target: c.name, set: { state: note, baseVersion: version, updatedAt: sql`now()` } })
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Remember who typed last in a document (called as typing is stored). */
|
|
233
|
+
export async function noteCollabEditor(documentName: string, name: string): Promise<void> {
|
|
234
|
+
const { db } = useCms()
|
|
235
|
+
await putSideRow(db, byName(documentName), name, null)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export interface UnsavedText { by: string | null; at: string; values: Record<string, any> }
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Text left in the document by a tab that closed without saving, for this entry as
|
|
242
|
+
* it is saved now (a later save makes it obsolete: gone), or null.
|
|
243
|
+
*/
|
|
244
|
+
export async function unsavedText(t: CollabTarget, savedVersion: Date | null): Promise<UnsavedText | null> {
|
|
245
|
+
const { db, reg } = useCms()
|
|
246
|
+
const c = reg.cmsCollab
|
|
247
|
+
const [row] = await db.select({ state: c.state, baseVersion: c.baseVersion }).from(c).where(eq(c.name, unsavedName(t.name)))
|
|
248
|
+
if (!row) return null
|
|
249
|
+
if ((row.baseVersion?.getTime() ?? null) !== (savedVersion?.getTime() ?? null)) {
|
|
250
|
+
await db.delete(c).where(eq(c.name, unsavedName(t.name)))
|
|
251
|
+
return null
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const note = JSON.parse(row.state)
|
|
255
|
+
const keys = new Set(collabFields(t.entity, t.locale))
|
|
256
|
+
const values = Object.fromEntries(Object.entries(note.values ?? {}).filter(([k]) => keys.has(k)))
|
|
257
|
+
return Object.keys(values).length ? { by: note.by ?? null, at: String(note.at), values } : null
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return null
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Forget the left-over text (kept or discarded). */
|
|
265
|
+
export async function dropUnsavedText(t: CollabTarget): Promise<void> {
|
|
266
|
+
const { db, reg } = useCms()
|
|
267
|
+
await db.delete(reg.cmsCollab).where(eq(reg.cmsCollab.name, unsavedName(t.name)))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* After a save made from the shared document (the browser sends its epoch in
|
|
272
|
+
* `X-CMS-Collab`): the document now matches this version of the entry.
|
|
273
|
+
*/
|
|
274
|
+
export async function noteCollabSave(event: any, t: { entity: CmsEntity; id: string; locale: string | null }, version: Date | string | null | undefined): Promise<void> {
|
|
275
|
+
const epoch = getRequestHeader(event, 'x-cms-collab')
|
|
276
|
+
if (!epoch || !UUID.test(epoch) || !version) return
|
|
277
|
+
try {
|
|
278
|
+
const { db, reg } = useCms()
|
|
279
|
+
const c = reg.cmsCollab
|
|
280
|
+
await db.update(c).set({ baseVersion: new Date(version) })
|
|
281
|
+
.where(and(eq(c.name, collabName(t.entity.name, t.id, t.locale)), eq(c.epoch, epoch)))
|
|
282
|
+
}
|
|
283
|
+
catch (err) {
|
|
284
|
+
console.error('[cms] collab: could not record the save', scrubError(err))
|
|
285
|
+
}
|
|
286
|
+
}
|