1414 * limitations under the License.
1515 */
1616
17- import type { IAccessor } from '@univerjs/core' ;
18- import type { IMenuButtonItem } from '@univerjs/ui' ;
19- import type { LocaleKey } from '../locale/types' ;
20- import { IUniverInstanceService , SHEET_EDITOR_UNITS , UniverInstanceType } from '@univerjs/core' ;
21- import { DocSelectionManagerService , DocSkeletonManagerService } from '@univerjs/docs' ;
17+ import { IPermissionService , IUniverInstanceService , SHEET_EDITOR_UNITS , UniverInstanceType } from '@univerjs/core' ;
18+ import { DocSelectionManagerService , DocSkeletonManagerService , getDocumentPermissionValue } from '@univerjs/docs' ;
2219import { DocumentEditArea , IRenderManagerService , withCurrentTypeOfRenderer } from '@univerjs/engine-render' ;
20+ import { UnitAction } from '@univerjs/protocol' ;
2321import { getMenuHiddenObservable , MenuItemType } from '@univerjs/ui' ;
24- import { debounceTime , Observable } from 'rxjs' ;
22+ import { combineLatest , debounceTime , map , startWith } from 'rxjs' ;
2523import {
2624 AddDocDrawingCommentOperation ,
2725 StartAddCommentOperation ,
2826 ToggleCommentPanelOperation ,
2927} from '../commands/operations/show-comment-panel.operation' ;
3028
31- export function AddDocDrawingCommentMenuItemFactory ( accessor : IAccessor ) : IMenuButtonItem < LocaleKey > {
29+ type MenuAccessor = Parameters < typeof getMenuHiddenObservable > [ 0 ] ;
30+
31+ function getCommentPermissionDisabled$ ( accessor : MenuAccessor ) {
32+ const instanceService = accessor . get ( IUniverInstanceService ) ;
33+ const permissionService = accessor . get ( IPermissionService ) ;
34+ return combineLatest ( [
35+ instanceService . getCurrentTypeOfUnit$ ( UniverInstanceType . UNIVER_DOC ) ,
36+ permissionService . permissionPointUpdate$ . pipe ( startWith ( undefined ) ) ,
37+ ] ) . pipe ( map ( ( [ document ] ) => ! document || ! getDocumentPermissionValue (
38+ permissionService ,
39+ document . getUnitId ( ) ,
40+ document . getUnitId ( ) ,
41+ UnitAction . Comment
42+ ) ) ) ;
43+ }
44+
45+ export function AddDocDrawingCommentMenuItemFactory ( accessor : MenuAccessor ) {
3246 return {
3347 id : AddDocDrawingCommentOperation . id ,
3448 type : MenuItemType . BUTTON ,
3549 icon : 'InsertCommentDoubleIcon' ,
3650 title : 'docs-thread-comment-ui.panel.addComment' ,
3751 tooltip : 'docs-thread-comment-ui.panel.addComment' ,
3852 hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_DOC ) ,
53+ disabled$ : getCommentPermissionDisabled$ ( accessor ) ,
3954 } ;
4055}
4156
42- export const shouldDisableAddComment = ( accessor : IAccessor ) => {
57+ export const shouldDisableAddComment = ( accessor : MenuAccessor ) => {
4358 const renderManagerService = accessor . get ( IRenderManagerService ) ;
4459 const docSelectionManagerService = accessor . get ( DocSelectionManagerService ) ;
60+ const instanceService = accessor . get ( IUniverInstanceService ) ;
61+ const permissionService = accessor . get ( IPermissionService ) ;
62+ const document = instanceService . getCurrentUnitOfType ( UniverInstanceType . UNIVER_DOC ) ;
63+ if ( ! document || ! getDocumentPermissionValue (
64+ permissionService ,
65+ document . getUnitId ( ) ,
66+ document . getUnitId ( ) ,
67+ UnitAction . Comment
68+ ) ) {
69+ return true ;
70+ }
4571 const skeleton = withCurrentTypeOfRenderer (
4672 UniverInstanceType . UNIVER_DOC ,
4773 DocSkeletonManagerService ,
48- accessor . get ( IUniverInstanceService ) ,
74+ instanceService ,
4975 renderManagerService
5076 ) ?. getSkeleton ( ) ;
5177
@@ -63,28 +89,27 @@ export const shouldDisableAddComment = (accessor: IAccessor) => {
6389 return false ;
6490} ;
6591
66- export function AddDocCommentMenuItemFactory ( accessor : IAccessor ) : IMenuButtonItem < LocaleKey > {
92+ export function AddDocCommentMenuItemFactory ( accessor : MenuAccessor ) {
6793 return {
6894 id : StartAddCommentOperation . id ,
6995 type : MenuItemType . BUTTON ,
7096 icon : 'InsertCommentDoubleIcon' ,
7197 title : 'docs-thread-comment-ui.panel.addComment' ,
7298 tooltip : 'docs-thread-comment-ui.panel.addComment' ,
7399 hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_DOC , undefined , SHEET_EDITOR_UNITS ) ,
74- disabled$ : new Observable ( function ( subscribe ) {
75- const textSelectionService = accessor . get ( DocSelectionManagerService ) ;
76- const observer = textSelectionService . textSelection$ . pipe ( debounceTime ( 16 ) ) . subscribe ( ( ) => {
77- subscribe . next ( shouldDisableAddComment ( accessor ) ) ;
78- } ) ;
79-
80- return ( ) => {
81- observer . unsubscribe ( ) ;
82- } ;
83- } ) ,
100+ disabled$ : combineLatest ( [
101+ accessor . get ( DocSelectionManagerService ) . textSelection$ . pipe (
102+ debounceTime ( 16 ) ,
103+ map ( ( ) => shouldDisableAddComment ( accessor ) ) ,
104+ startWith ( shouldDisableAddComment ( accessor ) )
105+ ) ,
106+ getCommentPermissionDisabled$ ( accessor ) ,
107+ ] ) . pipe ( map ( ( [ selectionDisabled , permissionDisabled ] ) =>
108+ selectionDisabled || permissionDisabled ) ) ,
84109 } ;
85110}
86111
87- export function ToolbarDocCommentMenuItemFactory ( accessor : IAccessor ) : IMenuButtonItem < LocaleKey > {
112+ export function ToolbarDocCommentMenuItemFactory ( accessor : MenuAccessor ) {
88113 return {
89114 id : ToggleCommentPanelOperation . id ,
90115 type : MenuItemType . BUTTON ,
0 commit comments