Skip to content

Commit

Permalink
fix(CiceroMarkToOOXML): use recursion to attach listeners
Browse files Browse the repository at this point in the history
Signed-off-by: Aman Sharma <mannu.poski10@gmail.com>
  • Loading branch information
algomaster99 committed Jun 26, 2020
1 parent a6b4834 commit d682b0c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/utils/CiceroMarkToOOXML/index.js
Expand Up @@ -37,39 +37,42 @@ const insertText = async (context, value) => {
await context.sync();
};

const insertVariable = async (context, title, tag, value) => {
let variableText = context.document.body.insertText(value, Word.InsertLocation.end);
let contentControl = variableText.insertContentControl();
contentControl.title = title;
contentControl.tag = tag;
contentControl.font.set({
color: 'black',
bold: true,
size: 14,
});
await context.sync();

const attachEventListener = (context, title) => {
Office.context.document.bindings.addFromNamedItemAsync(title, 'text', { id: title }, res => {
if (res.status === Office.AsyncResultStatus.Succeeded) {
res.value.addHandlerAsync(Office.EventType.BindingDataChanged, variableChangeListener, res => {
console.log(res);
if (res.status === Office.AsyncResultStatus.Succeeded) {
// ToDo: show the success to user in Production environment
console.info(`Listener attached to ${title}`);
return;
}
else {
// ToDo: show the error to user in Production environment
console.error(`Listener failed to attach to ${title}`);
attachEventListener(context, title);
}
});
}
else {
// ToDo: show the error to user in Production environment
console.error(title, res);
attachEventListener(context, title);
}
});
};

const insertVariable = async (context, title, tag, value) => {
let variableText = context.document.body.insertText(value, Word.InsertLocation.end);
let contentControl = variableText.insertContentControl();
contentControl.title = title;
contentControl.tag = tag;
contentControl.font.set({
color: 'black',
bold: true,
size: 14,
});
await context.sync();

// If the app ever goes into an infinite loop, it is probably because of this function call.
attachEventListener(context, title);
};

const definedNodes = {
computedVariable: 'org.accordproject.ciceromark.ComputedVariable',
heading: 'org.accordproject.commonmark.Heading',
Expand Down

0 comments on commit d682b0c

Please sign in to comment.