/** * Copyright 2023-present DreamNum Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { Nullable } from '@univerjs/core'; import type { ISheetNote } from '@univerjs/sheets-note'; import { generateRandomId } from '@univerjs/core'; import { SheetDeleteNoteCommand, SheetsNoteModel, SheetUpdateNoteCommand } from '@univerjs/sheets-note'; import { FRange } from '@univerjs/sheets/facade'; export interface ICreateOrUpdateNoteOptions { id?: string; width: number; height: number; note: string; show?: boolean; } /** * @ignore */ export interface IFRangeSheetsNoteMixin { /** * Get the annotation of the top-left cell in the range * @returns {Nullable} The annotation of the top-left cell in the range * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); * const fWorksheet = fWorkbook.getSheetByName('Sheet1'); * if (!fWorksheet) return; * const fRange = fWorksheet.getRange('A1:D10'); * const note = fRange.getNote(); * console.log(note); * ``` */ getNote(): Nullable; /** * Create or update the annotation of the top-left cell in the range * @param {ICreateOrUpdateNoteOptions} note The annotation to create or update * @returns {FRange} This range for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); * const fWorksheet = fWorkbook.getSheetByName('Sheet1'); * if (!fWorksheet) return; * const fRange = fWorksheet.getRange('A1'); * fRange.createOrUpdateNote({ * note: 'This is a note', * width: 160, * height: 100, * show: true, * }); * ``` */ createOrUpdateNote(note: ICreateOrUpdateNoteOptions): FRange; /** * Delete the annotation of the top-left cell in the range * @returns {FRange} This range for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); * const fWorksheet = fWorkbook.getSheetByName('Sheet1'); * if (!fWorksheet) return; * const notes = fWorksheet.getNotes(); * console.log(notes); * * if (notes.length > 0) { * // Delete the first note * const { row, col } = notes[0]; * fWorksheet.getRange(row, col).deleteNote(); * } * ``` */ deleteNote(): FRange; } export class FRangeSheetsNoteMixin extends FRange implements IFRangeSheetsNoteMixin { override createOrUpdateNote(note: ICreateOrUpdateNoteOptions): FRange { const unitId = this.getUnitId(); const sheetId = this.getSheetId(); const row = this.getRow(); const col = this.getColumn(); const oldNote = this._injector.get(SheetsNoteModel).getNote(unitId, sheetId, { row, col }); this._commandService.syncExecuteCommand( SheetUpdateNoteCommand.id, { unitId, sheetId, row, col, note: { id: oldNote?.id ?? note.id ?? generateRandomId(6), row, col, width: note.width, height: note.height, note: note.note, show: note.show, }, } ); return this; } override deleteNote(): FRange { this._commandService.syncExecuteCommand( SheetDeleteNoteCommand.id, { unitId: this.getUnitId(), sheetId: this.getSheetId(), row: this.getRow(), col: this.getColumn(), } ); return this; } override getNote(): Nullable { const model = this._injector.get(SheetsNoteModel); return model.getNote(this.getUnitId(), this.getSheetId(), { row: this.getRow(), col: this.getColumn() }); } } FRange.extend(FRangeSheetsNoteMixin); declare module '@univerjs/sheets/facade' { // eslint-disable-next-line ts/naming-convention interface FRange extends IFRangeSheetsNoteMixin { } }