Skip to content

Commit 8e201de

Browse files
committed
feat(docs): complete permission facade APIs
1 parent 743dba0 commit 8e201de

5 files changed

Lines changed: 142 additions & 8 deletions

File tree

packages/docs/src/controllers/__tests__/doc-permission.controller.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ describe('DocPermissionController', () => {
127127
const firstParagraph = document.getParagraph('paragraph-one')!;
128128
const secondParagraph = document.getParagraph('paragraph-two')!;
129129

130-
await firstSection.getPermission().setEditable(false);
130+
await firstSection.getPermission().setReadOnly();
131131

132132
expect(firstSection.getPermission().canEdit()).toBe(false);
133133
expect(firstParagraph.getPermission().canEdit()).toBe(false);
134134
expect(secondParagraph.getPermission().canEdit()).toBe(true);
135135
expect(firstParagraph.setText('Denied')).toBe(false);
136136
expect(secondParagraph.setText('Allowed')).toBe(true);
137137

138-
await document.getSection(0)!.getPermission().setEditable(true);
138+
await document.getSection(0)!.getPermission().setEditable();
139139
await document.getParagraph('paragraph-one')!.getPermission().setEditable(false);
140140
expect(document.getParagraph('paragraph-one')!.setText('Denied again')).toBe(false);
141141
});

packages/docs/src/facade/f-document-paragraph.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,16 @@ export class FDocumentParagraph extends FBaseInitialable {
106106
return this._segmentId;
107107
}
108108

109-
/** Returns the effective edit permission facade for this paragraph. */
109+
/**
110+
* Returns this Paragraph's permission facade.
111+
* @returns {FDocumentObjectPermission} Permission facade combining Document, Section, and Paragraph Edit points.
112+
* @example
113+
* ```ts
114+
* const paragraph = univerAPI.getActiveDocument()?.getParagraphs()[0];
115+
* if (!paragraph) throw new Error('Paragraph not found.');
116+
* await paragraph.getPermission().setReadOnly();
117+
* ```
118+
*/
110119
getPermission(): FDocumentObjectPermission {
111120
return new FDocumentObjectPermission(
112121
this._document.getId(),

packages/docs/src/facade/f-document-permission.ts

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,35 @@ import type { DocumentUnitPermissionAction } from '@univerjs/docs';
1919
import { canEditDocumentTargets, getDocumentPermissionValue, SetDocumentPermissionCommand } from '@univerjs/docs';
2020
import { UnitAction } from '@univerjs/protocol';
2121

22+
/**
23+
* Command-backed permissions for one Document unit.
24+
* @hideconstructor
25+
*/
2226
export class FDocumentPermission {
2327
constructor(
2428
private readonly _unitId: string,
2529
private readonly _commandService: ICommandService,
2630
private readonly _permissionService: IPermissionService
2731
) {}
2832

33+
/**
34+
* Sets one Document unit permission through the command system.
35+
*
36+
* Supported actions are Edit, Copy, Print, Export, and Comment. Await the returned promise
37+
* before reading the new value or performing an action that depends on it.
38+
*
39+
* @param {DocumentUnitPermissionAction} action Unit permission action to update.
40+
* @param {boolean} value Whether the action is allowed.
41+
* @returns {Promise<void>} Resolves after the permission command finishes.
42+
* @example Disable copying while keeping the Document editable
43+
* ```ts
44+
* import { UnitAction } from '@univerjs/protocol';
45+
*
46+
* const document = univerAPI.getActiveDocument();
47+
* if (!document) throw new Error('No active Document.');
48+
* await document.getPermission().setPoint(UnitAction.Copy, false);
49+
* ```
50+
*/
2951
async setPoint(action: DocumentUnitPermissionAction, value: boolean): Promise<void> {
3052
await this._commandService.executeCommand(SetDocumentPermissionCommand.id, {
3153
unitId: this._unitId,
@@ -35,6 +57,19 @@ export class FDocumentPermission {
3557
});
3658
}
3759

60+
/**
61+
* Returns the current value of one Document unit permission.
62+
* @param {DocumentUnitPermissionAction} action Unit permission action to query.
63+
* @returns {boolean} Whether the action is currently allowed.
64+
* @example
65+
* ```ts
66+
* import { UnitAction } from '@univerjs/protocol';
67+
*
68+
* const document = univerAPI.getActiveDocument();
69+
* const canPrint = document?.getPermission().getPoint(UnitAction.Print) ?? false;
70+
* console.log(canPrint);
71+
* ```
72+
*/
3873
getPoint(action: DocumentUnitPermissionAction): boolean {
3974
return getDocumentPermissionValue(
4075
this._permissionService,
@@ -44,19 +79,42 @@ export class FDocumentPermission {
4479
);
4580
}
4681

47-
async setEditable(editable: boolean): Promise<void> {
82+
/**
83+
* Enables or disables editing for the whole Document.
84+
* @param {boolean} [editable] Whether editing is allowed. Defaults to true.
85+
* @returns {Promise<void>} Resolves after the permission command finishes.
86+
*/
87+
async setEditable(editable = true): Promise<void> {
4888
await this.setPoint(UnitAction.Edit, editable);
4989
}
5090

91+
/**
92+
* Makes the whole Document read-only.
93+
* @returns {Promise<void>} Resolves after the permission command finishes.
94+
* @example
95+
* ```ts
96+
* const document = univerAPI.getActiveDocument();
97+
* if (!document) throw new Error('No active Document.');
98+
* await document.getPermission().setReadOnly();
99+
* ```
100+
*/
51101
async setReadOnly(): Promise<void> {
52102
await this.setEditable(false);
53103
}
54104

105+
/**
106+
* Returns whether the whole Document is currently editable.
107+
* @returns {boolean} Whether Document editing is allowed.
108+
*/
55109
canEdit(): boolean {
56110
return this.getPoint(UnitAction.Edit);
57111
}
58112
}
59113

114+
/**
115+
* Command-backed Edit permission for one stable Document object.
116+
* @hideconstructor
117+
*/
60118
export class FDocumentObjectPermission {
61119
constructor(
62120
private readonly _unitId: string,
@@ -66,7 +124,23 @@ export class FDocumentObjectPermission {
66124
private readonly _getParentObjectIds: () => string[] = () => []
67125
) {}
68126

69-
async setEditable(editable: boolean): Promise<void> {
127+
/**
128+
* Enables or disables editing for this stable Document object.
129+
*
130+
* This changes only the object's Edit point. `canEdit()` also applies the Document unit and
131+
* parent Section or Paragraph ceilings.
132+
*
133+
* @param {boolean} [editable] Whether object editing is allowed. Defaults to true.
134+
* @returns {Promise<void>} Resolves after the permission command finishes.
135+
* @example Restore editing for a paragraph
136+
* ```ts
137+
* const document = univerAPI.getActiveDocument();
138+
* const paragraph = document?.getParagraph('paragraph-1');
139+
* if (!paragraph) throw new Error('Paragraph not found.');
140+
* await paragraph.getPermission().setEditable();
141+
* ```
142+
*/
143+
async setEditable(editable = true): Promise<void> {
70144
await this._commandService.executeCommand(SetDocumentPermissionCommand.id, {
71145
unitId: this._unitId,
72146
objectId: this._objectId,
@@ -75,6 +149,25 @@ export class FDocumentObjectPermission {
75149
});
76150
}
77151

152+
/**
153+
* Makes this stable Document object read-only.
154+
* @returns {Promise<void>} Resolves after the permission command finishes.
155+
* @example
156+
* ```ts
157+
* const document = univerAPI.getActiveDocument();
158+
* const section = document?.getSection(0);
159+
* if (!section) throw new Error('Section not found.');
160+
* await section.getPermission().setReadOnly();
161+
* ```
162+
*/
163+
async setReadOnly(): Promise<void> {
164+
await this.setEditable(false);
165+
}
166+
167+
/**
168+
* Returns the effective Edit result after applying the Document, parent, and object permissions.
169+
* @returns {boolean} Whether the object is currently editable.
170+
*/
78171
canEdit(): boolean {
79172
return canEditDocumentTargets(
80173
this._permissionService,

packages/docs/src/facade/f-document-section.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,16 @@ export class FDocumentSection {
109109
return this._sectionId;
110110
}
111111

112-
/** Returns the effective edit permission facade for this section. */
112+
/**
113+
* Returns this Section's permission facade.
114+
* @returns {FDocumentObjectPermission} Permission facade combining Document and Section Edit points.
115+
* @example
116+
* ```ts
117+
* const section = univerAPI.getActiveDocument()?.getSection(0);
118+
* if (!section) throw new Error('Section not found.');
119+
* await section.getPermission().setReadOnly();
120+
* ```
121+
*/
113122
getPermission(): FDocumentObjectPermission {
114123
return new FDocumentObjectPermission(
115124
this._document.getId(),

packages/docs/src/facade/f-document.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,35 @@ export class FDocument extends FBaseInitialable {
185185
return this.id;
186186
}
187187

188-
/** Returns the effective permission facade for this document. */
188+
/**
189+
* Returns the Document unit permission facade.
190+
* @returns {FDocumentPermission} Permission facade for Edit, Copy, Print, Export, and Comment.
191+
* @example
192+
* ```ts
193+
* const document = univerAPI.getActiveDocument();
194+
* if (!document) throw new Error('No active Document.');
195+
* await document.getPermission().setReadOnly();
196+
* ```
197+
*/
189198
getPermission(): FDocumentPermission {
190199
return new FDocumentPermission(this.id, this._commandService, this._permissionService);
191200
}
192201

193-
/** Returns an edit permission facade for a stable document entity id. */
202+
/**
203+
* Returns the permission facade for an entity with a stable id, such as a Table, Drawing, or Custom Block.
204+
*
205+
* Parent Section and Paragraph permission ceilings are resolved from the current Document model.
206+
* @param {string} segmentId Segment id, or an empty string for the main body.
207+
* @param {string} entityType Stable entity type used by the owning Doc feature.
208+
* @param {string} entityId Stable entity id.
209+
* @returns {FDocumentObjectPermission} Effective permission facade for the entity.
210+
* @example Make one table read-only
211+
* ```ts
212+
* const document = univerAPI.getActiveDocument();
213+
* if (!document) throw new Error('No active Document.');
214+
* await document.getEntityPermission('', 'table', 'table-1').setReadOnly();
215+
* ```
216+
*/
194217
getEntityPermission(segmentId: string, entityType: string, entityId: string): FDocumentObjectPermission {
195218
return new FDocumentObjectPermission(
196219
this.id,

0 commit comments

Comments
 (0)