Last active
April 2, 2024 09:26
-
-
Save xuruiyao-msft/3f2dbc062c71958dba098d7aa2b96a7d to your computer and use it in GitHub Desktop.
onAnnotationClicked null issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: onAnnotationClicked null issue | |
description: Creates a new snippet from a blank template. | |
host: WORD | |
api_set: {} | |
script: | |
content: | | |
$("#run").click(() => tryCatch(run)); | |
$("#insertAnnotations").click(() => tryCatch(insertAnnotations)); | |
async function run() { | |
await Word.run(async (context) => { | |
const callback = context.document.onAnnotationClicked.add(onAnnotationClicked); | |
await context.sync(); | |
console.log("Added event handler for when content controls are added."); | |
}); | |
async function onAnnotationClicked(event: Word.AnnotationClickedEventArgs) { | |
await Word.run(async (context) => { | |
console.log(event); | |
}); | |
} | |
} | |
async function insertAnnotations() { | |
await Word.run(async (context) => { | |
const paragraph = context.document.getSelection().paragraphs.getFirst(); | |
const critique1 = { | |
colorScheme: Word.CritiqueColorScheme.red, | |
start: 1, | |
length: 3 | |
}; | |
const critique2 = { | |
colorScheme: Word.CritiqueColorScheme.green, | |
start: 6, | |
length: 1 | |
}; | |
const critique3 = { | |
colorScheme: Word.CritiqueColorScheme.blue, | |
start: 10, | |
length: 3 | |
}; | |
const critique4 = { | |
colorScheme: Word.CritiqueColorScheme.lavender, | |
start: 14, | |
length: 3 | |
}; | |
const critique5 = { | |
colorScheme: Word.CritiqueColorScheme.berry, | |
start: 18, | |
length: 10 | |
}; | |
const annotationSet: Word.AnnotationSet = { | |
critiques: [critique1, critique2, critique3, critique4, critique5] | |
}; | |
const annotationIds = paragraph.insertAnnotations(annotationSet); | |
await context.sync(); | |
}); | |
} | |
/** Default helper for invoking an action and handling errors. */ | |
async function tryCatch(callback) { | |
try { | |
await callback(); | |
} catch (error) { | |
// Note: In a production add-in, you'd want to notify the user through your add-in's UI. | |
console.error(error); | |
} | |
} | |
language: typescript | |
template: | |
content: |- | |
<button id="run" class="ms-Button"> | |
<span class="ms-Button-label">Run</span> | |
</button> | |
<button id="insertAnnotations" class="ms-Button"> | |
<span class="ms-Button-label">insertAnnotations</span> | |
</button> | |
language: html | |
style: | |
content: |- | |
section.samples { | |
margin-top: 20px; | |
} | |
section.samples .ms-Button, section.setup .ms-Button { | |
display: block; | |
margin-bottom: 5px; | |
margin-left: 20px; | |
min-width: 80px; | |
} | |
language: css | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css | |
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css | |
core-js@2.4.1/client/core.min.js | |
@types/core-js | |
jquery@3.1.1 | |
@types/jquery@3.3.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment