Exploring word add-ins world. Want to create a list of paragraphs and navigate to each by click on it(some kind of table of content)
I have created code(on reactjs, but it does not matter which framework to use)
When I click on any button the callback is invoked, no errors in console, but the navigation does not happen.
Question: How can I pass some context data between Word.run
calls to achieve this kind of behavior?
import * as React from 'react'
import { useEffect, useState } from 'react'
const findAllParagraphs = async () => {
return Word.run(context => {
const contentControls = context.document.body.paragraphs.load('text')
return context
.sync()
.then(() => contentControls.items)
})
}
export const App = ({ isOfficeInitialized }) => {
const [paras, setParas] = useState<any[]>([])
useEffect(() => {
const loadParagraphs = async () => {
setParas(await findAllParagraphs())
}
if (isOfficeInitialized) {
loadParagraphs()
}
}, [isOfficeInitialized])
const navToPar = async (par: Word.Paragraph) => {
return await Word.run(async context => {
par.select()
return await context.sync()
})
}
const renderList = () => {
return paras.filter(({ text }) => !!text).map((par: Word.Paragraph, i) => {
return (
<div>
<button onClick={() => navToPar(par)} key={i}>
{par.text.substr(0, 30)}...
</button>
</div>
)
})
}
return (<div>{renderList()}</div>)
}