Commentaires
Les utilisateurs peuvent laisser des commentaires et des réponses sur un document, afin qu'une équipe puisse discuter du contenu en un seul endroit.
Mode plugin
Installation
pnpm add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uinpm install @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uiyarn add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uibun add @univerjs/thread-comment @univerjs/thread-comment-ui @univerjs/sheets-thread-comment @univerjs/sheets-thread-comment-uiUtilisation
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment'import { UniverSheetsThreadCommentUIPlugin } from '@univerjs/sheets-thread-comment-ui'import SheetsThreadCommentEnUS from '@univerjs/sheets-thread-comment/locale/en-US'import SheetsThreadCommentUIEnUS from '@univerjs/sheets-thread-comment-ui/locale/en-US'import { UniverThreadCommentPlugin } from '@univerjs/thread-comment'import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui'import ThreadCommentUIEnUS from '@univerjs/thread-comment-ui/locale/en-US'import '@univerjs/sheets-thread-comment-ui/lib/index.css'import '@univerjs/thread-comment-ui/lib/index.css'import '@univerjs/sheets-thread-comment/facade'const univer = new Univer({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( ThreadCommentUIEnUS, SheetsThreadCommentEnUS, SheetsThreadCommentUIEnUS, ), },})univer.registerPlugin(UniverThreadCommentPlugin)univer.registerPlugin(UniverThreadCommentUIPlugin)univer.registerPlugin(UniverSheetsThreadCommentPlugin)univer.registerPlugin(UniverSheetsThreadCommentUIPlugin)Utilisation avec la fonctionnalité de collaboration
Si Collaboration est également utilisé, enregistrez également le plugin source de données:
pnpm add @univerjs-pro/thread-comment-datasourcenpm install @univerjs-pro/thread-comment-datasourceyarn add @univerjs-pro/thread-comment-datasourcebun add @univerjs-pro/thread-comment-datasourceimport { UniverThreadCommentDataSourcePlugin } from '@univerjs-pro/thread-comment-datasource'univer.registerPlugin(UniverThreadCommentDataSourcePlugin)Prise en charge mobile
Configurez d'abord le éditeur mobile. Dans la configuration en mode plugin sur cette page, remplacez les enregistrements de bureau ci-dessous par les exportations mobiles des mêmes paquets, et faites-le avant d'ouvrir un document . Gardez les plugins modèles, styles, locaux et importations Facade, et ne enregistrez pas les deux variantes.
| Paquet | Inscription de bureau | Inscription mobile |
|---|---|---|
@univerjs/sheets-thread-comment-ui | UniverSheetsThreadCommentUIPlugin | UniverSheetsThreadCommentMobileUIPlugin |
@univerjs/thread-comment-ui | UniverThreadCommentUIPlugin | UniverThreadCommentMobileUIPlugin |
Mode preset
Installation
pnpm add @univerjs/preset-sheets-thread-commentnpm install @univerjs/preset-sheets-thread-commentyarn add @univerjs/preset-sheets-thread-commentbun add @univerjs/preset-sheets-thread-commentUtilisation
import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { UniverSheetsThreadCommentPreset } from '@univerjs/preset-sheets-thread-comment'import UniverPresetSheetsThreadCommentEnUS from '@univerjs/preset-sheets-thread-comment/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'import '@univerjs/preset-sheets-thread-comment/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, UniverPresetSheetsThreadCommentEnUS, ), }, presets: [ UniverSheetsCorePreset(), UniverSheetsThreadCommentPreset(), ],})API Facade
Les définitions de type Facade API complètes sont disponibles dans le FacadeAPI.
Importation
Note du mode greffon
Le paquet Facade est importé manuellement en mode plugin seulement. Le mode Preset l'inclut déjà, donc aucune importation supplémentaire n'est nécessaire.
import '@univerjs/sheets-thread-comment/facade'Créer un commentaire de cellule
univerAPI.newTheadComment() retourne un FTheadCommentBuilder pour un nouveau commentaire. Méthodes de chaîne sur le constructeur pour définir les propriétés du commentaire.
Voici quelques méthodes membres de FTheadCommentBuilder :
| Méthode | Description |
|---|---|
| setContent | Définit le contenu du commentaire |
| setPersonId | Définit l'identifiant de la personne du commentaire |
| setDateTime | Définit la date et l'heure du commentaire |
| setId | Définit l'identifiant du commentaire |
| setThreadId | Définit l'ID du thread du commentaire |
// Create a new commentconst richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment() .setContent(richText)console.log(commentBuilder.content.toPlainText())// Add the comment to cell A1const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()const cell = fWorksheet.getRange('A1')const result = await cell.addCommentAsync(commentBuilder)Obtenir les commentaires des cellules
Ces méthodes renvoient un FThreadComment, qui peut ensuite être mis à jour, supprimé ou résolu.
FWorkbook.getComments(): obtient tous les commentaires dans le cahierFfWorksheet.getComments(): obtient tous les commentaires dans la feuille de travailFRange.getComment(): obtient le commentaire de la cellule supérieure gauche dans la plageFRange.getComments(): obtient tous les commentaires dans la gamme
Voici quelques méthodes membres de FThreadComment :
| Méthode | Description |
|---|---|
| getIsRoot | Indique si le commentaire est un commentaire racine |
| getCommentData | Obtient les données de commentaires |
| getReplies | Obtient la liste des réponses au commentaire |
| getRange | Obtient la gamme du commentaire |
| getRichText | Obtient le contenu texte riche du commentaire |
| deleteAsync | Supprime le commentaire et ses réponses |
| updateAsync | Mettre à jour le contenu des commentaires |
| resolveAsync | Résoudre le commentaire |
| replyAsync | Réponses au commentaire |
// Get all comments in the workbookconst fWorkbook = univerAPI.getActiveWorkbook()fWorkbook.getComments()// Get all comments in the active worksheetconst fWorksheet = fWorkbook.getActiveSheet()fWorksheet.getComments()const fRange = fWorksheet.getRange('A1:B2')// Get the comment of cell A1fRange.getComment()// Get all comments in the range A1:B2fRange.getComments()Effacer les commentaires des cellules
FWorkbook.clearComments(): efface tous les commentaires dans le cahierFWorksheet.clearComments(): efface tous les commentaires dans la feuille de travailFRange.clearCommentAsync(): efface le commentaire de la cellule supérieure gauche dans la plageFRange.clearCommentsAsync(): efface tous les commentaires dans la gamme
// Clear all comments in the workbookconst fWorkbook = univerAPI.getActiveWorkbook()await fWorkbook.clearComments()// Clear all comments in the active worksheetconst fWorksheet = fWorkbook.getActiveSheet()await fWorksheet.clearComments()const fRange = fWorksheet.getRange('A1:B2')// Clear the comment of cell A1await fRange.clearCommentAsync()// Clear all comments in the range A1:B2await fRange.clearCommentsAsync()Obtenir la liste des réponses à un commentaire
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Get all comments in the worksheetconst comments = fWorksheet.getComments()comments.forEach((comment) => { // If the comment is a root comment, get its replies if (comment.getIsRoot()) { const replies = comment.getReplies() replies.forEach((reply) => { console.log(reply.getCommentData()) }) }})Mettre à jour, supprimer, résoudre des commentaires ou y répondre
const fWorkbook = univerAPI.getActiveWorkbook()const fWorksheet = fWorkbook.getActiveSheet()// Create a new comment and add it to cell A1const richText = univerAPI.newRichText().insertText('hello univer')const commentBuilder = univerAPI.newTheadComment() .setContent(richText) .setId('mock-comment-id')const cell = fWorksheet.getRange('A1')await cell.addCommentAsync(commentBuilder)// Update the comment and reply after 3 secondssetTimeout(async () => { // Get the comment by ID and update its content const comment = fWorksheet.getCommentById('mock-comment-id') const newRichText = univerAPI.newRichText().insertText('Hello Univer AI') await comment.updateAsync(newRichText) // Create a new reply comment and set its content const replyText = univerAPI.newRichText().insertText('Hello Univer AI! GO! GO! GO!') const reply = univerAPI.newTheadComment().setContent(replyText) await comment.replyAsync(reply)}, 3000)// Resolve the comment and delete it after 6 secondssetTimeout(async () => { const comment = fWorksheet.getCommentById('mock-comment-id') await comment.resolveAsync() await comment.deleteAsync()}, 6000)Écouteurs d’événements
Consultez les définitions complètes des types d’événements dans Events.
Le module de commentaires émet des événements pour les ajouts de commentaires, les mises à jour, les suppressions et les changements d'état de résolution. univerAPI.addEvent().
Événements d’ajout de commentaire
univerAPI.Event.CommentAdded allume après l'ajout d'un commentaire.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentAdded, (params) => { const { comment, workbook, worksheet, row, col } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentAdd allume avant qu'un commentaire ne soit ajouté.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentAdd, (params) => { const { comment, workbook, worksheet, row, col } = params // Cancel the comment addition operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Événements de mise à jour de commentaire
univerAPI.Event.CommentUpdated allume après la mise à jour d'un commentaire.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentUpdated, (params) => { const { comment, workbook, worksheet, row, col } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentUpdate allume avant qu'un commentaire ne soit mis à jour.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentUpdate, (params) => { const { comment, workbook, worksheet, row, col, newContent } = params // Cancel the comment update operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Événements de suppression de commentaire
univerAPI.Event.CommentDeleted allume après la suppression d'un commentaire.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentDeleted, (params) => { const { commentId, workbook, worksheet } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentDelete allume avant qu'un commentaire ne soit supprimé.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentDelete, (params) => { const { comment, workbook, worksheet, row, col } = params // Cancel the comment deletion operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Événements de changement de l’état de résolution
univerAPI.Event.CommentResolved s'allume après que l'état de résolution d'un commentaire change.
const disposable = univerAPI.addEvent(univerAPI.Event.CommentResolved, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params})// Remove the event listener, use `disposable.dispose()`univerAPI.Event.BeforeCommentResolve s'allume avant que l'état de résolution d'un commentaire ne change.
const disposable = univerAPI.addEvent(univerAPI.Event.BeforeCommentResolve, (params) => { const { comment, row, col, resolved, workbook, worksheet } = params // Cancel the comment resolution operation params.cancel = true})// Remove the event listener, use `disposable.dispose()`Tous les événements comportent les paramètres communs suivants:
workbook: le cahier de travail actuelworksheet: la feuille de travail actuellerow: l'index des lignes du commentairecol: l'index de colonne du commentairecomment: l'objet commentaire (commentIduniquement dans les événements de suppression)
Certains événements ajoutent leurs propres paramètres :
BeforeCommentUpdatetransporte égalementnewContent, le nouveau contenu de commentaire (Type RichTextValue)CommentResolvedetBeforeCommentResolveportent égalementresolved, le statut de résolution du commentaire
Chaque callback Before peut définir params.cancel = true pour arrêter l'opération.
Opérations communes
Importez à la fois le commentaire partagé Facade et le produit Facade avant d'appeler ces méthodes. Les helpers de produit créent des ancres stables, et l'API partagée gère les réponses, les mises à jour, la résolution et la suppression. getComments() lit seulement l'état local. listCommentsAsync() synchronise les threads qu'il connaît déjà à travers la source de données configurée; il ne découvre pas chaque thread sur le serveur.
import '@univerjs/thread-comment/facade'const [thread] = univerAPI.getComments({ resolved: false })if (thread) { await univerAPI.replyCommentAsync({ unitId: thread.unitId, subUnitId: thread.subUnitId, threadId: thread.threadId, content: 'Reviewed.', })}Discussions partagées
Les discussions partagées nécessitent le service de commentaires et l'Endpoint, intégrés par Server SDK. Après la collaboration et la licence sont configurées, enregistrez UniverThreadCommentDataSourcePlugin depuis @univerjs-pro/thread-comment-datasource . La source de données fournie utilise /universer-api/comment/unit/{unitId}/... et /universer-api/user/list, de sorte que l'application doit exposer ces routes.
Commentaires sur les images et graphiques
Après avoir configuré le plugin host dessin ou graphique, ajoutez l'intégration correspondante ci-dessous avec son entrée Facade avant d'ouvrir l'unité. Le mode Plugin et le mode préréglé exigent tous deux que ces intégrations soient enregistrées explicitement. Ils n'ajoutent pas de styles ou de localisations supplémentaires.
pnpm add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentnpm install @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentyarn add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentbun add @univerjs-pro/sheets-drawing-thread-comment @univerjs-pro/sheets-chart-thread-commentimport { UniverSheetsDrawingThreadCommentPlugin } from '@univerjs-pro/sheets-drawing-thread-comment'import { UniverSheetsChartThreadCommentPlugin } from '@univerjs-pro/sheets-chart-thread-comment'import '@univerjs-pro/sheets-drawing-thread-comment/facade'import '@univerjs-pro/sheets-chart-thread-comment/facade'univer.registerPlugin(UniverSheetsDrawingThreadCommentPlugin)univer.registerPlugin(UniverSheetsChartThreadCommentPlugin)Avec l'unité ouverte, des commentaires peuvent être ajoutés aux images ou aux graphiques existants. getComments() retourne les threads chargés localement; listCommentsAsync() synchronise les threades qu'il connaît avant de les renvoyer.
const host = univerAPI.getActiveWorkbook()?.getActiveSheet()const image = host?.getImages()[0]const chart = host?.getCharts()[0]await image?.createCommentAsync('Review this image.')await chart?.createCommentAsync('Verify the chart source.')@univerjs-pro/sheets-drawing-thread-comment · @univerjs-pro/sheets-chart-thread-comment
Que pensez-vous de ce guide ?