如何在ckeditor插件同步中读取JSON文件

发布于 2025-01-21 01:49:45 字数 1083 浏览 4 评论 0原文

我需要为 CKEditor 5 创建一个自定义插件,它将读取 JSON 并在工具栏的下拉列表中显示数据。 我面临的问题是异步获取数据,在将列表设置到 Init 方法中的配置之前,该数据尚未完成。

export default class PlaceholderEditing extends Plugin {
static get requires() {
    return [Widget];
}

init() {
    
    this._defineSchema();
    this._defineConverters();

    this.editor.commands.add('placeholder', new PlaceholderCommand(this.editor));

    this.editor.editing.mapper.on(
        'viewToModelPosition',
        viewToModelPositionOutsideModelElement(this.editor.model, viewElement => viewElement.hasClass('placeholder'))
    );

    var items=new Array();

    fetch('/GetTerms').then(response => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);       
        }
        return response.json();
    }).then(data => {
        items = data;
    });


    this.editor.config.define('placeholderConfig', {
        types: items
    });



}

在此代码中,items 始终为 null,因为配置定义是在获取完成之前运行的。 我无法将 config.define 放在 then 子句中,因为 init 方法将在它之前完成。

那么,如何强制 fetch 等待结果,或者更好地说如何同步而不是异步获取数据?

I need to create a customs plugin for CKEditor 5 that will read a JSON and will show data in a dropdown list in the toolbar.
the problem I faced is async fetching data which is not finished before setting the list into the configuration in the Init method.

export default class PlaceholderEditing extends Plugin {
static get requires() {
    return [Widget];
}

init() {
    
    this._defineSchema();
    this._defineConverters();

    this.editor.commands.add('placeholder', new PlaceholderCommand(this.editor));

    this.editor.editing.mapper.on(
        'viewToModelPosition',
        viewToModelPositionOutsideModelElement(this.editor.model, viewElement => viewElement.hasClass('placeholder'))
    );

    var items=new Array();

    fetch('/GetTerms').then(response => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);       
        }
        return response.json();
    }).then(data => {
        items = data;
    });


    this.editor.config.define('placeholderConfig', {
        types: items
    });



}

in this code items always is null because config define is run before the fetch is finished.
I couldn't put config.define in the then clause because init method will be finished before it.

So, how can I force fetch to wait for the result or better to say how can I fetch data sync not async?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2025-01-28 01:49:45

最后,我通过在构造函数中添加异步函数来解决我的问题

init() {
    return (async () => {

        this._defineSchema();
        this._defineConverters();

        this.editor.commands.add('placeholder', new PlaceholderCommand(this.editor));

        this.editor.editing.mapper.on(
            'viewToModelPosition',
            viewToModelPositionOutsideModelElement(this.editor.model, viewElement => viewElement.hasClass('placeholder'))
        );

        var items = new Array();

        await fetch('contractTerms.json').then(response => {
            if (!response.ok) {
                throw new Error("HTTP error " + response.status);
            }
            return response.json();
        }).then(data => {
            console.log(data.items);
            items = data.items;
        });

        console.log('pass');
        this.editor.config.define('placeholderConfig', {
            types: items
        });
      })();

Finally, I solve my problem by adding async function into my constructor

init() {
    return (async () => {

        this._defineSchema();
        this._defineConverters();

        this.editor.commands.add('placeholder', new PlaceholderCommand(this.editor));

        this.editor.editing.mapper.on(
            'viewToModelPosition',
            viewToModelPositionOutsideModelElement(this.editor.model, viewElement => viewElement.hasClass('placeholder'))
        );

        var items = new Array();

        await fetch('contractTerms.json').then(response => {
            if (!response.ok) {
                throw new Error("HTTP error " + response.status);
            }
            return response.json();
        }).then(data => {
            console.log(data.items);
            items = data.items;
        });

        console.log('pass');
        this.editor.config.define('placeholderConfig', {
            types: items
        });
      })();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文