mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 08:13:45 +08:00
Merge branch 'next' into next-reply
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Generated
+6
@@ -24532,6 +24532,12 @@
|
||||
"integrity": "sha512-kk80vLW9iGtjMnIv11qyxLqZm20UklzuR2tL0QAnDIygIUIemcZMxlMWudl9OOt76H3ntVzcTiddQ1/pAAJMYg==",
|
||||
"dev": true
|
||||
},
|
||||
"typescript-snapshots-plugin": {
|
||||
"version": "1.2.0",
|
||||
"resolved": "https://registry.npmjs.org/typescript-snapshots-plugin/-/typescript-snapshots-plugin-1.2.0.tgz",
|
||||
"integrity": "sha1-4rp5y0C3Vc4tUp6h5fYlMyd5T5g=",
|
||||
"dev": true
|
||||
},
|
||||
"ua-parser-js": {
|
||||
"version": "0.7.18",
|
||||
"resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.18.tgz",
|
||||
|
||||
@@ -239,6 +239,7 @@
|
||||
"typeface-manuale": "0.0.54",
|
||||
"typeface-source-sans-pro": "0.0.54",
|
||||
"typescript": "^3.0.3",
|
||||
"typescript-snapshots-plugin": "^1.2.0",
|
||||
"uglifyjs-webpack-plugin": "^1.2.5",
|
||||
"webpack": "4.12.0",
|
||||
"webpack-cli": "^3.0.2",
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import pym from "pym.js";
|
||||
|
||||
export type CleanupCallback = () => void;
|
||||
export type Decorator = (pym: pym.Parent) => CleanupCallback | void;
|
||||
export { Decorator, CleanupCallback } from "./types";
|
||||
export { default as withAutoHeight } from "./withAutoHeight";
|
||||
export { default as withClickEvent } from "./withClickEvent";
|
||||
export { default as withSetCommentID } from "./withSetCommentID";
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import pym from "pym.js";
|
||||
|
||||
export type CleanupCallback = () => void;
|
||||
export type Decorator = (pym: pym.Parent) => CleanupCallback | void;
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
const withAutoHeight: Decorator = pym => {
|
||||
// Resize parent iframe height when child height changes
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
const withClickEvent: Decorator = pym => {
|
||||
const handleClick = () => pym.sendMessage("click", "");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { EventEmitter2 } from "eventemitter2";
|
||||
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
const withEventEmitter = (eventEmitter: EventEmitter2): Decorator => pym => {
|
||||
// Pass events from iframe to the event emitter.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
const withIOSSafariWidthWorkaround: Decorator = pym => {
|
||||
// Workaround: IOS Safari ignores `width` but respects `min-width` value.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
const withPymStorage = (
|
||||
storage: Storage,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import qs from "query-string";
|
||||
|
||||
import { buildURL } from "../utils";
|
||||
import { Decorator } from "./";
|
||||
import { Decorator } from "./types";
|
||||
|
||||
function getCurrentCommentID() {
|
||||
return qs.parse(location.search).commentID;
|
||||
|
||||
@@ -23,7 +23,7 @@ export interface TalkContext {
|
||||
/** formatter for timeago. */
|
||||
timeagoFormatter?: Formatter;
|
||||
|
||||
/** Session Storage */
|
||||
/** Local Storage */
|
||||
localStorage: Storage;
|
||||
|
||||
/** Session storage */
|
||||
|
||||
@@ -1,10 +1,71 @@
|
||||
import { Child, Parent } from "pym.js";
|
||||
import uuid from "uuid/v4";
|
||||
|
||||
import { PromisifiedStorage } from "./PromisifiedStorage";
|
||||
|
||||
type Pym = Child | Parent;
|
||||
|
||||
class PymStorage implements PromisifiedStorage {
|
||||
/** Instance to pym */
|
||||
private pym: Pym;
|
||||
|
||||
/** Requested storage type */
|
||||
private type: string;
|
||||
|
||||
/** A Map of requestID => {resolve, reject} */
|
||||
private requests: Record<
|
||||
string,
|
||||
{ resolve: ((v: any) => void); reject: ((v: any) => void) }
|
||||
> = {};
|
||||
|
||||
/** Requests method with parameters over pym. */
|
||||
private call<T>(
|
||||
method: string,
|
||||
parameters: { key: string; value?: string }
|
||||
): Promise<T> {
|
||||
const id = uuid();
|
||||
return new Promise((resolve, reject) => {
|
||||
this.requests[id] = { resolve, reject };
|
||||
this.pym.sendMessage(
|
||||
`pymStorage.${this.type}.request`,
|
||||
JSON.stringify({ id, method, parameters })
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/** Listen to pym responses */
|
||||
private listen() {
|
||||
// Receive successful responses.
|
||||
this.pym.onMessage(`pymStorage.${this.type}.response`, (msg: string) => {
|
||||
const { id, result } = JSON.parse(msg);
|
||||
this.requests[id].resolve(result);
|
||||
delete this.requests[id];
|
||||
});
|
||||
|
||||
// Receive error responses.
|
||||
this.pym.onMessage(`pymStorage.${this.type}.error`, (msg: string) => {
|
||||
const { id, error } = JSON.parse(msg);
|
||||
this.requests[id].reject(new Error(error));
|
||||
delete this.requests[id];
|
||||
});
|
||||
}
|
||||
|
||||
constructor(pym: Pym, type: string) {
|
||||
this.pym = pym;
|
||||
this.type = type;
|
||||
this.listen();
|
||||
}
|
||||
|
||||
public setItem(key: string, value: string) {
|
||||
return this.call<void>("setItem", { key, value });
|
||||
}
|
||||
public getItem(key: string) {
|
||||
return this.call<string | null>("getItem", { key });
|
||||
}
|
||||
public removeItem(key: string) {
|
||||
return this.call<void>("removeItem", { key });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a storage that put requests onto pym.
|
||||
* This is the counterpart of `connectStorageToPym`.
|
||||
@@ -14,45 +75,6 @@ type Pym = Child | Parent;
|
||||
export default function createPymStorage(
|
||||
pym: Pym,
|
||||
type: "localStorage" | "sessionStorage"
|
||||
): PromisifiedStorage {
|
||||
// A Map of requestID => {resolve, reject}
|
||||
const requests: Record<
|
||||
string,
|
||||
{ resolve: ((v: any) => void); reject: ((v: any) => void) }
|
||||
> = {};
|
||||
|
||||
// Requests method with parameters over pym.
|
||||
const call = <T>(
|
||||
method: string,
|
||||
parameters: { key: string; value?: string }
|
||||
): Promise<T> => {
|
||||
const id = uuid();
|
||||
return new Promise((resolve, reject) => {
|
||||
requests[id] = { resolve, reject };
|
||||
pym.sendMessage(
|
||||
`pymStorage.${type}.request`,
|
||||
JSON.stringify({ id, method, parameters })
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// Receive successful responses.
|
||||
pym.onMessage(`pymStorage.${type}.response`, (msg: string) => {
|
||||
const { id, result } = JSON.parse(msg);
|
||||
requests[id].resolve(result);
|
||||
delete requests[id];
|
||||
});
|
||||
|
||||
// Receive error responses.
|
||||
pym.onMessage(`pymStorage.${type}.error`, (msg: string) => {
|
||||
const { id, error } = JSON.parse(msg);
|
||||
requests[id].reject(new Error(error));
|
||||
delete requests[id];
|
||||
});
|
||||
|
||||
return {
|
||||
setItem: (key: string, value: string) => call("setItem", { key, value }),
|
||||
getItem: (key: string) => call("getItem", { key }),
|
||||
removeItem: (key: string) => call("removeItem", { key }),
|
||||
};
|
||||
): PymStorage {
|
||||
return new PymStorage(pym, type);
|
||||
}
|
||||
|
||||
+6
-1
@@ -13,7 +13,12 @@
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"noErrorTruncation": true,
|
||||
"lib": ["dom", "es6", "esnext.asynciterable"]
|
||||
"lib": ["dom", "es6", "esnext.asynciterable"],
|
||||
"plugins": [
|
||||
{
|
||||
"name": "typescript-snapshots-plugin"
|
||||
}
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"./src/**/.*.js",
|
||||
|
||||
Reference in New Issue
Block a user