当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript data.dispatch函数代码示例

本文整理汇总了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,
        );
    }
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:31,代码来源:actions.ts

示例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,
        );
    }
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:26,代码来源:actions.ts

示例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();
    }
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:29,代码来源:actions.ts

示例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));
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:28,代码来源:actions.ts

示例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();
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:8,代码来源:actions.ts

示例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));
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:9,代码来源:actions.ts

示例7: save

function* save() {
    yield saveState();
    yield dispatch('core/editor').autosave();
}
开发者ID:dsifford,项目名称:academic-bloggers-toolkit,代码行数:4,代码来源:actions.ts


注:本文中的@wordpress/data.dispatch函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。