本文整理匯總了TypeScript中@wordpress/data.dispatch函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript dispatch函數的具體用法?TypeScript dispatch怎麽用?TypeScript dispatch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了dispatch函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: setFootnotes
function* setFootnotes() {
const items = select('abt/data').getFootnotes();
const blocksList = select<Block[]>('core/block-editor').getBlocks();
const footnoteBlockIndex = blocksList.findIndex(
({ name }) => name === 'abt/footnotes',
);
const bibliographyBlockIndex = blocksList.findIndex(
({ name }) => name === 'abt/bibliography',
);
if (items.length > 0 && footnoteBlockIndex >= 0) {
yield dispatch('core/block-editor').updateBlockAttributes(
blocksList[footnoteBlockIndex].clientId,
{ items },
);
} else if (items.length > 0 && footnoteBlockIndex === -1) {
yield dispatch('core/block-editor').insertBlock(
createBlock('abt/footnotes', {
items,
}),
bibliographyBlockIndex > 0
? bibliographyBlockIndex
: blocksList.length,
undefined,
false,
);
} else if (items.length === 0 && footnoteBlockIndex >= 0) {
yield dispatch('core/block-editor').removeBlock(
blocksList[footnoteBlockIndex].clientId,
);
}
}
示例2: setBibliography
function* setBibliography({ items, meta }: Processor.Bibliography) {
const blocksList = select<Block[]>('core/block-editor').getBlocks();
const bibliographyBlock = blocksList.find(
block => block.name === 'abt/bibliography',
);
if (items.length > 0 && bibliographyBlock) {
yield dispatch('core/block-editor').updateBlockAttributes(
bibliographyBlock.clientId,
{ ...meta, items },
);
} else if (items.length > 0 && !bibliographyBlock) {
yield dispatch('core/block-editor').insertBlock(
createBlock('abt/bibliography', {
...meta,
items,
}),
blocksList.length,
undefined,
false,
);
} else if (items.length === 0 && bibliographyBlock) {
yield dispatch('core/block-editor').removeBlock(
bibliographyBlock.clientId,
);
}
}
示例3: removeReferences
export function* removeReferences(itemIds: string[]) {
const doc = getEditorDOM();
const toDelete = [
...doc.querySelectorAll<HTMLSpanElement>(CitationElement.selector),
].reduce(
(idsToDelete, citation) => {
const existingIds = CitationElement.getItems(citation);
const filteredItemIds = existingIds.filter(
id => !itemIds.includes(id),
);
if (filteredItemIds.length === 0 && citation.parentNode) {
citation.parentNode.removeChild(citation);
} else {
citation.dataset.items = JSON.stringify(filteredItemIds);
}
return idsToDelete.filter(id => !existingIds.includes(id));
},
[...itemIds],
);
yield dispatch('core/block-editor').resetBlocks(parse(doc.innerHTML));
if (toDelete.length > 0) {
yield {
type: Actions.REMOVE_REFERENCES,
itemIds: toDelete,
};
yield save();
}
}
示例4: updateEditorCitations
function* updateEditorCitations(citations: Processor.CitationMeta[]) {
const doc = getEditorDOM();
for (const { html, id, sortedItems } of citations) {
const node = doc.querySelector<HTMLElement>(
createSelector(
...CitationElement.legacyClassNames.map(cls => ({
classNames: [cls],
attributes: { id },
})),
),
);
if (node) {
node.innerHTML = html;
node.dataset.items = sortedItems;
if (node.firstElementChild) {
node.dataset.hasChildren = 'true';
node.firstElementChild.innerHTML =
ZERO_WIDTH_SPACE +
node.firstElementChild.innerHTML +
ZERO_WIDTH_SPACE;
} else {
delete node.dataset.hasChildren;
node.innerHTML = ZERO_WIDTH_SPACE + html + ZERO_WIDTH_SPACE;
}
}
}
yield dispatch('core/block-editor').resetBlocks(parse(doc.innerHTML));
}
示例5: parseFootnotes
export function* parseFootnotes() {
const doc = getEditorDOM();
doc.querySelectorAll(FootnoteElement.selector).forEach((footnote, i) => {
footnote.innerHTML = FootnoteElement.createMarker(i);
});
yield dispatch('core/block-editor').resetBlocks(parse(doc.innerHTML));
yield setFootnotes();
}
示例6: removeAllCitations
export function* removeAllCitations() {
const doc = getEditorDOM();
for (const el of doc.querySelectorAll(CitationElement.selector)) {
if (el.parentNode) {
el.parentNode.removeChild(el);
}
}
yield dispatch('core/block-editor').resetBlocks(parse(doc.innerHTML));
}
示例7: save
function* save() {
yield saveState();
yield dispatch('core/editor').autosave();
}