npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsdkit/bridge

v1.0.2

Published

即时设计开发辅助工具套件,方便宿主与iframe通信

Downloads

8

Readme

@jsdkit/bridge

本项目使您可以像调用本地同步函数一样,进行宿主与iframe间双向同步调用。

同时本项目,也可做为使用 React 进行即时设计插件开发的基础模板。

安装

ES6(推荐)

npm i @jsdkit/bridge

UMD(不推荐)

npm 包中包含 umd 文件,你可以下载下来后,添加到工程中:


<script src="./node_modules/@jsdkit/bridge/dist/index.umd.js"></script>

使用

ui 调用 host 中的方法

首先在 host 中提供一个 createRectangle 方法:

import {createUiBridge} from '@/lib';

const ui = createUiBridge();

// 定义一个方法,供 ui 环境使用
ui.createRectangle = async (count) => {
    console.log('ui 传递的参数:', count);  // 123
    // 使用 return 将值直接传递给 ui
    return true;
};

jsDesign.showUI(__html__);

在 ui 环境中,直接调用宿主中的 createRectangle 方法:

import {createHostBridge} from '@jsdkit/bridge';

const host = createHostBridge('你的插件id');
const res = await host.createRectangle(123);
console.log(res)  // res 即为 host 中 createRectangle 方法的返回值 (true)

host 调用 ui 中的方法

首先在 ui 中提供一个 changeButtonColor 方法:

import {createHostBridge} from '@jsdkit/bridge';

const host = createHostBridge('你的插件id');

// 定义一个方法供 host 调用
host.changeButtonColor = (color: string) => {
    // 收到 color 参数
    document.getElementById('btn').style.backgroundColor = color;
};

在 host 环境中,直接调用 ui 中的 changeButtonColor 方法:

// 调用 ui 方法
import {createUiBridge} from '@/lib';

const ui = createUiBridge();
ui.changeButtonColor('#f00'); // 如果有返回值,也可以使用 await 来接收
jsDesign.showUI(__html__);

TypeScript 支持

您只需要对提供的方法进行声明,并在 createBridge 时传入泛型声明即可:

示例:

// interfaces.ts ,此文件仅为 ts 定义文件,会被 host 和 ui 环境共用类型提示
export interface Bridge {
    /** 创建矩形 */
    createRectangle(count: number): Promise<boolean>;

    /** 改变按钮颜色 */
    changeButtonColor(color: string): void;
}

在 ui 中使用:

const host = createHostBridge<Bridge>('你的插件id');

之后输入 host.,您的编辑器即可自动提示。

在 host 中使用:

const ui = createUiBridge<Bridge>();

之后输入 ui.,您的编辑器即可自动提示。