@@ -19,13 +19,35 @@ import type { DocumentUnitPermissionAction } from '@univerjs/docs';
1919import { canEditDocumentTargets , getDocumentPermissionValue , SetDocumentPermissionCommand } from '@univerjs/docs' ;
2020import { UnitAction } from '@univerjs/protocol' ;
2121
22+ /**
23+ * Command-backed permissions for one Document unit.
24+ * @hideconstructor
25+ */
2226export 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+ */
60118export 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 ,
0 commit comments