File size: 1,960 Bytes
d6e7555
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(function (global) {
  "use strict";

  const API_VERSION = 1;
  const RENDER_EVENT = "streamlit:render";
  const listeners = new Set();
  let ready = false;
  let lastFrameHeight = null;

  function send(type, data) {
    global.parent.postMessage(
      Object.assign(
        {
          isStreamlitMessage: true,
          type: type,
        },
        data || {},
      ),
      "*",
    );
  }

  function onMessage(event) {
    if (!event || !event.data || event.data.type !== RENDER_EVENT) {
      return;
    }
    const detail = {
      args: event.data.args || {},
      disabled: Boolean(event.data.disabled),
      theme: event.data.theme || null,
    };
    listeners.forEach(function (listener) {
      listener(detail);
    });
  }

  function setComponentReady() {
    if (!ready) {
      global.addEventListener("message", onMessage);
      ready = true;
    }
    send("streamlit:componentReady", { apiVersion: API_VERSION });
  }

  function setFrameHeight(height) {
    const nextHeight = Number.isFinite(height)
      ? Math.max(0, Math.round(height))
      : Math.max(0, Math.round(document.body.scrollHeight));
    if (nextHeight === lastFrameHeight) {
      return;
    }
    lastFrameHeight = nextHeight;
    send("streamlit:setFrameHeight", { height: nextHeight });
  }

  function setComponentValue(value) {
    send("streamlit:setComponentValue", {
      value: value,
      dataType: "json",
    });
  }

  function onRender(listener) {
    if (typeof listener !== "function") {
      throw new TypeError("render listener must be a function");
    }
    listeners.add(listener);
    return function () {
      listeners.delete(listener);
    };
  }

  global.SolidPrivacyStreamlitBridge = Object.freeze({
    API_VERSION: API_VERSION,
    RENDER_EVENT: RENDER_EVENT,
    onRender: onRender,
    setComponentReady: setComponentReady,
    setFrameHeight: setFrameHeight,
    setComponentValue: setComponentValue,
  });
})(window);