next-box 1.0.14 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,19 +4,62 @@
4
4
  A library for developing extensions, components and working with data in the NextBox platform.
5
5
 
6
6
  ```javascript
7
- import { initFileApi } from 'next-box';
8
-
9
- initFileApi().then((fileApi) => {
10
- // Get file url
11
- console.log(fileApi.fileUrl)
12
-
13
- // Download file
14
- fileApi.download().then(response => response.text()).then(data => {
15
- document.body.innerHTML = data;
7
+ import { init, ExtensionEvents } from 'next-box';
8
+
9
+ const app = init();
10
+
11
+ app.on(ExtensionEvents.Ready, () => {
12
+ /**
13
+ * Get file url
14
+ */
15
+ console.log(app.fileUrl);
16
+
17
+ /**
18
+ * Download file
19
+ */
20
+ app.download()
21
+ .then((response) => response.text())
22
+ .then((data) => {
23
+ document.body.innerHTML = data;
24
+ });
25
+
26
+ /**
27
+ * Replace file content
28
+ */
29
+ app.replace('new content');
30
+
31
+ /**
32
+ * Display change event
33
+ */
34
+ app.on(ExtensionEvents.ChangeView, (state) => {
35
+ console.log(state);
16
36
  });
17
37
 
18
- // Replace file content
19
- fileApi.replace('new content')
38
+ /**
39
+ * Display change event
40
+ */
41
+ app.on(ExtensionEvents.ChangeView, (state) => {
42
+ console.log(state);
43
+ });
44
+
45
+ /**
46
+ * Request to save changes and close the editor
47
+ */
48
+ app.on(ExtensionEvents.SaveAndClose, (state) => {
49
+ console.log(state);
50
+ app.close();
51
+ });
52
+
53
+ /**
54
+ * Submit a content change message
55
+ * In the case when content is written before exiting the application or when writing large files
56
+ */
57
+ app.changeContent(true);
58
+
59
+ /**
60
+ * Send an event to close the editor
61
+ */
62
+ app.close();
20
63
  });
21
64
  ```
22
65
 
package/app.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { EventEmitter, EventHandler } from './event-emitter';
2
+ import { AppState, ExtensionEvents } from './interfaces';
3
+ export declare class NextBox extends EventEmitter {
4
+ state?: AppState;
5
+ debug: boolean;
6
+ constructor();
7
+ on(event: ExtensionEvents, handler: EventHandler): void;
8
+ off(event: ExtensionEvents, handler: EventHandler): void;
9
+ /**
10
+ * Returns the full path to download the file
11
+ */
12
+ get fileUrl(): string;
13
+ /**
14
+ * Subscribe postMessage and send an initialization event
15
+ */
16
+ init(): void;
17
+ /**
18
+ * Submit a content change message
19
+ * In the case when content is written before exiting the application or when writing large files
20
+ */
21
+ changeContent(state: boolean): void;
22
+ /**
23
+ * Send an event to close the editor
24
+ */
25
+ close(state: boolean): void;
26
+ /**
27
+ * @deprecated
28
+ */
29
+ download(): Promise<Response | Pick<Response, "text">>;
30
+ /**
31
+ * Submit a file download request
32
+ */
33
+ fileDownload(): Promise<Response | Pick<Response, 'text'>>;
34
+ /**
35
+ * @deprecated
36
+ */
37
+ replace(body: any): Promise<Response | null>;
38
+ /**
39
+ * Submit a file change request
40
+ */
41
+ fileReplace(body: any): Promise<Response | null>;
42
+ private subscribeEvent;
43
+ }
@@ -0,0 +1,7 @@
1
+ export type EventHandler = (...args: any) => void;
2
+ export declare class EventEmitter {
3
+ _handlers: Map<string, Set<EventHandler>>;
4
+ on(event: string, handler: EventHandler): void;
5
+ off(event: string, handler: EventHandler): void;
6
+ emit(event: string, ...args: any): void;
7
+ }
package/helpers.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { TransportEventPayload, AppEvents } from './interfaces';
2
+ export declare function sendEvent(event: AppEvents, payload?: TransportEventPayload): void;
package/index.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- export * from './file-api';
2
1
  export * from './interfaces';
2
+ export * from './init';
3
+ export * from './event-emitter';
4
+ export * from './helpers';
5
+ export * from './app';
package/index.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.PolymaticaApi=t():e.PolymaticaApi=t()}(globalThis,(()=>(()=>{"use strict";var e={437:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.initFileApi=void 0;var o=n(679);t.initFileApi=function(){return new Promise((function(e){parent!==window?(parent.postMessage({event:o.TransportEventType.Init},window.location.origin),window.addEventListener("message",(function(t){t.data.event===o.TransportEventType.Ready&&e(new r(t.data.payload))}))):e(new r)}))};var r=function(){function e(e){this.payload=e}return Object.defineProperty(e.prototype,"fileUrl",{get:function(){var e;return(null===(e=this.payload)||void 0===e?void 0:e.file_download_uri)||""},enumerable:!1,configurable:!0}),e.prototype.download=function(){return this.payload?fetch(this.payload.file_download_uri,{method:this.payload.file_download_method}):new Promise((function(e){return{text:function(){return""}}}))},e.prototype.replace=function(e){return this.payload?fetch(this.payload.file_replace_uri,{method:this.payload.file_replace_method,body:e}):new Promise((function(e){e(null)}))},e}()},607:function(e,t,n){var o=this&&this.__createBinding||(Object.create?function(e,t,n,o){void 0===o&&(o=n);var r=Object.getOwnPropertyDescriptor(t,n);r&&!("get"in r?!t.__esModule:r.writable||r.configurable)||(r={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,o,r)}:function(e,t,n,o){void 0===o&&(o=n),e[o]=t[n]}),r=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||o(t,e,n)};Object.defineProperty(t,"__esModule",{value:!0}),r(n(437),t),r(n(679),t)},679:(e,t)=>{var n,o;Object.defineProperty(t,"__esModule",{value:!0}),t.State=t.TransportEventType=void 0,(o=t.TransportEventType||(t.TransportEventType={})).Init="init",o.Ready="ready",o.ChangeState="change-state",o.ChangeContent="change-content",(n=t.State||(t.State={})).view="view",n.edit="edit"}},t={};return function n(o){var r=t[o];if(void 0!==r)return r.exports;var i=t[o]={exports:{}};return e[o].call(i.exports,i,i.exports,n),i.exports}(607)})()));
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.PolymaticaApi=t():e.PolymaticaApi=t()}(globalThis,(()=>(()=>{"use strict";var e={752:function(e,t,n){var o,i=this&&this.__extends||(o=function(e,t){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},o(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(t,"__esModule",{value:!0}),t.NextBox=void 0;var r=n(139),s=n(382),a=n(679),u=function(e){function t(){var t=e.call(this)||this;return t.debug=!1,t}return i(t,e),t.prototype.on=function(t,n){return e.prototype.on.call(this,t,n)},t.prototype.off=function(t,n){return e.prototype.off.call(this,t,n)},Object.defineProperty(t.prototype,"fileUrl",{get:function(){var e;return(null===(e=this.state)||void 0===e?void 0:e.file_download_uri)||""},enumerable:!1,configurable:!0}),t.prototype.init=function(){this.subscribeEvent(),(0,s.sendEvent)(a.AppEvents.Init)},t.prototype.changeContent=function(e){(0,s.sendEvent)(a.AppEvents.ChangeContent,e)},t.prototype.close=function(e){(0,s.sendEvent)(a.AppEvents.Close,e)},t.prototype.download=function(){return this.fileDownload()},t.prototype.fileDownload=function(){return this.state?fetch(this.state.file_download_uri,{method:this.state.file_download_method}):new Promise((function(e){return{text:function(){return""}}}))},t.prototype.replace=function(e){return this.fileReplace(e)},t.prototype.fileReplace=function(e){return this.state?fetch(this.state.file_replace_uri,{method:this.state.file_replace_method,body:e}):new Promise((function(e){e(null)}))},t.prototype.subscribeEvent=function(){var e=this;window.addEventListener("message",(function(t){var n=t.data.event,o=t.data.payload;switch(e.debug&&console.log("frame.message",t.data),n){case a.ExtensionEvents.Ready:e.debug=o.debug,e.emit(a.ExtensionEvents.Ready);break;case a.ExtensionEvents.ChangeView:e.state&&(e.state.view=o)}}))},t}(r.EventEmitter);t.NextBox=u},139:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.EventEmitter=void 0;var n=function(){function e(){this._handlers=new Map}return e.prototype.on=function(e,t){var n;this._handlers.has(e)||this._handlers.set(e,new Set),null===(n=this._handlers.get(e))||void 0===n||n.add(t)},e.prototype.off=function(e,t){var n;null===(n=this._handlers.get(e))||void 0===n||n.delete(t)},e.prototype.emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];this._handlers.get(e)},e}();t.EventEmitter=n},382:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.sendEvent=void 0,t.sendEvent=function(e,t){parent!==window&&parent.postMessage({event:e,payload:t},window.location.origin)}},607:function(e,t,n){var o=this&&this.__createBinding||(Object.create?function(e,t,n,o){void 0===o&&(o=n);var i=Object.getOwnPropertyDescriptor(t,n);i&&!("get"in i?!t.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,o,i)}:function(e,t,n,o){void 0===o&&(o=n),e[o]=t[n]}),i=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||o(t,e,n)};Object.defineProperty(t,"__esModule",{value:!0}),i(n(679),t),i(n(178),t),i(n(139),t),i(n(382),t),i(n(752),t)},178:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.initFileApi=t.init=void 0;var o=n(752),i=n(679);function r(){var e=new o.NextBox;return e.init(),e}t.init=r,t.initFileApi=function(){return new Promise((function(e){var t=r();t.on(i.ExtensionEvents.Ready,(function(){e(t)}))}))}},679:(e,t)=>{var n,o,i;Object.defineProperty(t,"__esModule",{value:!0}),t.ViewState=t.ExtensionEvents=t.AppEvents=void 0,(i=t.AppEvents||(t.AppEvents={})).Init="init",i.ChangeContent="change-content",i.Close="close",(o=t.ExtensionEvents||(t.ExtensionEvents={})).Ready="ready",o.ChangeView="change-view",o.SaveAndClose="save-and-close",(n=t.ViewState||(t.ViewState={})).view="view",n.edit="edit"}},t={};return function n(o){var i=t[o];if(void 0!==i)return i.exports;var r=t[o]={exports:{}};return e[o].call(r.exports,r,r.exports,n),r.exports}(607)})()));
package/init.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { NextBox } from './app';
2
+ export declare function init(): NextBox;
3
+ /**
4
+ * @deprecated
5
+ */
6
+ export declare function initFileApi(): Promise<NextBox>;
package/interfaces.d.ts CHANGED
@@ -1,21 +1,26 @@
1
- export declare enum TransportEventType {
1
+ export declare enum AppEvents {
2
2
  Init = "init",
3
+ ChangeContent = "change-content",
4
+ Close = "close"
5
+ }
6
+ export declare enum ExtensionEvents {
3
7
  Ready = "ready",
4
- ChangeState = "change-state",
5
- ChangeContent = "change-content"
8
+ ChangeView = "change-view",
9
+ SaveAndClose = "save-and-close"
6
10
  }
7
11
  export interface TransportEvent {
8
12
  uid?: string;
9
- event: TransportEventType;
13
+ event: AppEvents | ExtensionEvents;
10
14
  payload?: TransportEventPayload;
11
15
  }
12
- export type TransportEventPayload = PayloadReady | State | any;
13
- export declare enum State {
16
+ export declare enum ViewState {
14
17
  view = "view",
15
18
  edit = "edit"
16
19
  }
17
- export interface PayloadReady {
18
- state: State;
20
+ export type TransportEventPayload = AppState | ViewState | any;
21
+ export interface AppState {
22
+ debug: boolean;
23
+ view: ViewState;
19
24
  file_download_uri: string;
20
25
  file_download_method: string;
21
26
  file_replace_uri: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-box",
3
- "version": "1.0.14",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/file-api.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { PayloadReady } from './interfaces';
2
- export declare function initFileApi(): Promise<FileApi>;
3
- declare class FileApi {
4
- private payload?;
5
- constructor(payload?: PayloadReady);
6
- get fileUrl(): string;
7
- download(): Promise<Response | downloadResponse>;
8
- replace(body: any): Promise<Response | null>;
9
- }
10
- interface downloadResponse {
11
- text: () => string;
12
- }
13
- export {};