Skip to content

Commit 808857b

Browse files
authored
feat: add autolock on edit (#540)
1 parent 51c7269 commit 808857b

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

.changeset/quiet-snakes-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@blinkk/root-cms": patch
3+
---
4+
5+
feat: add autolock on edit (#540)

docs/collections/Pages.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default schema.collection({
1818
src: 'https://lh3.googleusercontent.com/c2ECbvhJtxf3xbPIjaXCSpmvAsJkkhzJwG98T9RPvWy4s30jZKClom8pvWTnupRYOnyI3qGhNXPOwqoN6sqljkDO62LIKRtR988',
1919
},
2020
},
21+
autolock: true,
2122

2223
fields: [
2324
schema.object({

packages/root-cms/core/app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ function serializeCollection(collection: Collection): Partial<Collection> {
155155
previewUrl: collection.previewUrl,
156156
preview: collection.preview,
157157
slugRegex: collection.slugRegex,
158+
autolock: collection.autolock,
159+
autolockReason: collection.autolockReason,
158160
};
159161
}
160162

packages/root-cms/core/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ export type Collection = Schema & {
293293
* string so it can be serialized to the CMS UI.
294294
*/
295295
slugRegex?: string;
296+
/**
297+
* Automatically add a publishing lock whenever the doc is edited.
298+
*/
299+
autolock?: boolean;
300+
/** Reason for the automatic publishing lock. */
301+
autolockReason?: string;
296302
};
297303

298304
export function defineCollection(

packages/root-cms/ui/hooks/useDraft.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export class DraftController extends EventListener {
5353
private cachedData: any = {};
5454
private subscribers: Subscribers = {};
5555
private saveState = SaveState.NO_CHANGES;
56+
private autolock = false;
57+
private autolockReason = 'autolock';
58+
private autolockApplied = false;
5659
started = false;
5760

5861
constructor(docId: string) {
@@ -72,6 +75,13 @@ export class DraftController extends EventListener {
7275
'Drafts',
7376
slug
7477
);
78+
const collection = window.__ROOT_CTX.collections[collectionId];
79+
if (collection) {
80+
this.autolock = !!collection.autolock;
81+
if (collection.autolockReason) {
82+
this.autolockReason = collection.autolockReason;
83+
}
84+
}
7585
}
7686

7787
/**
@@ -171,7 +181,6 @@ export class DraftController extends EventListener {
171181
* Updates a group of keys. The keys can be a nested, e.g. "meta.title".
172182
*/
173183
async updateKeys(updates: Record<string, any>) {
174-
console.log('updateKeys()', updates);
175184
for (const key in updates) {
176185
this.pendingUpdates.set(key, updates[key]);
177186
}
@@ -230,7 +239,21 @@ export class DraftController extends EventListener {
230239
const updates = Object.fromEntries(this.pendingUpdates);
231240
updates['sys.modifiedAt'] = serverTimestamp();
232241
updates['sys.modifiedBy'] = window.firebase.user.email;
233-
console.log('flush()', updates);
242+
243+
// If autolock is enabled on the collection, add a publishing lock if one
244+
// doesn't already exist on the doc.
245+
if (
246+
this.autolock &&
247+
!this.autolockApplied &&
248+
!this.cachedData?.sys?.publishingLocked
249+
) {
250+
this.autolockApplied = true;
251+
updates['sys.publishingLocked'] = {
252+
lockedAt: serverTimestamp(),
253+
lockedBy: window.firebase.user.email,
254+
reason: this.autolockReason,
255+
};
256+
}
234257

235258
// Immediately clear the pending updates so that there's no race condition
236259
// with any new updates the user makes while the changes are being saved to

0 commit comments

Comments
 (0)