Last active
August 19, 2021 10:08
-
-
Save richie5um/8edf779e93f6757729c753a76762fd42 to your computer and use it in GitHub Desktop.
Shows a bug with getSelection content controls
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: Content control basics | |
description: Shows a bug with getSelection content controls | |
host: WORD | |
api_set: {} | |
script: | |
content: > | |
$("#output-paragraph-controls").click(() => | |
tryCatch(outputParagraphContentControls)); | |
$("#output-selection-controls").click(() => | |
tryCatch(outputSelectionContentControls)); | |
$("#output-selection-control").click(() => | |
tryCatch(outputSelectionContentControl)); | |
$("#output-controls").click(() => tryCatch(outputContentControls)); | |
async function outputContentControls() { | |
await Word.run(async (context) => { | |
let contentControls = context.document.contentControls; | |
await loadContentControls(context, contentControls); | |
await context.sync(); | |
logContentControls(contentControls); | |
}); | |
} | |
async function outputParagraphContentControls() { | |
await Word.run(async (context) => { | |
let selection = context.document.getSelection(); | |
context.load(selection.paragraphs, "*"); | |
await context.sync(); | |
let contentControls = selection.paragraphs.getFirst().contentControls; | |
await loadContentControls(context, contentControls); | |
logContentControls(contentControls); | |
}); | |
} | |
async function outputSelectionContentControls() { | |
await Word.run(async (context) => { | |
let contentControls = context.document.getSelection().contentControls; | |
await loadContentControls(context, contentControls); | |
logContentControls(contentControls); | |
}); | |
} | |
async function outputSelectionContentControl() { | |
await Word.run(async (context) => { | |
// let contentControls = context.document.getSelection().contentControls; | |
let contentControl = context.document.getSelection().parentContentControlOrNullObject; | |
await loadContentControls(context, contentControl); | |
logContentControl(contentControl); | |
}); | |
} | |
async function loadContentControls(ctx, contentControls) { | |
ctx.load( | |
contentControls, | |
"appearance," + | |
"cannotDelete," + | |
"cannotEdit," + | |
"color," + | |
"id," + | |
"placeHolderText," + | |
"removeWhenEdited," + | |
"title," + | |
"text," + | |
"type," + | |
"style," + | |
"tag," + | |
"font/size," + | |
"font/name," + | |
"font/color" | |
); | |
await ctx.sync(); | |
} | |
function logContentControls(contentControls) { | |
for (let i = 0; i < contentControls.items.length; i++) { | |
logContentControl(contentControls.items[0]); | |
} | |
} | |
function logContentControl(contentControl){ | |
console.log( | |
"Property values of the first content control:" + | |
" ----- appearance: " + | |
contentControl.appearance + | |
" ----- cannotDelete: " + | |
contentControl.cannotDelete + | |
" ----- cannotEdit: " + | |
contentControl.cannotEdit + | |
" ----- color: " + | |
contentControl.color + | |
" ----- id: " + | |
contentControl.id + | |
" ----- placeHolderText: " + | |
contentControl.placeholderText + | |
" ----- removeWhenEdited: " + | |
contentControl.removeWhenEdited + | |
" ----- title: " + | |
contentControl.title + | |
" ----- text: " + | |
contentControl.text + | |
" ----- type: " + | |
contentControl.type + | |
" ----- style: " + | |
contentControl.style + | |
" ----- tag: " + | |
contentControl.tag + | |
" ----- font size: " + | |
contentControl.font.size + | |
" ----- font name: " + | |
contentControl.font.name + | |
" ----- font color: " + | |
contentControl.font.color | |
); | |
} | |
/** 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: "<section class=\"ms-font-m\">\n\tThis sample demonstrates an issue with getSelection().contentControls.\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"output-controls\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Output content controls</span>\n </button>\n\t<button id=\"output-paragraph-controls\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Output Selected Paragraph content controls</span>\n </button>\n\t<button id=\"output-selection-controls\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Output Selection content controls (BUG?! fails on Desktop Word, works on Online Word)</span>\n </button>\n\t<button id=\"output-selection-control\" class=\"ms-Button\">\n\t <span class=\"ms-Button-label\">Output Selection content control</span>\n\t </button>\n</section>" | |
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 | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment