Skip to content

Commit

Permalink
Close #466 : Add typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Aug 29, 2021
1 parent 4cd59e1 commit 8b560f0
Show file tree
Hide file tree
Showing 71 changed files with 2,157 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ jobs:
npm install
npm run compile
npm run test
- name: tests
working-directory: ./tests
run: |
npm install
npm run test
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/node_modules
node_modules
/.idea
/*.iml
/dist
/public
/package-lock.json
/yarn.lock
/yarn-error.log
package-lock.json
yarn.lock
yarn-error.log
/three-examples
2 changes: 1 addition & 1 deletion docs/guide/adapters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ new PhotoSphereViewer.Viewer({

::: tab ES import
```js
import CubemapAdapter from 'photo-sphere-viewer/dist/adapters/cubemap';
import { CubemapAdapter } from 'photo-sphere-viewer/dist/adapters/cubemap';

new Viewer({
adapter: [CubemapAdapter, {
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Official plugins (listed on the left menu) are available in the the main `photo-
Import `photo-sphere-viewer/dist/plugins/markers.css` with the prefered way depending on your tooling.

```js
import MarkersPlugins from 'photo-sphere-viewer/dist/plugins/markers';
import { MarkersPlugins } from 'photo-sphere-viewer/dist/plugins/markers';
```
:::

Expand Down
24 changes: 21 additions & 3 deletions docs/plugins/writing-a-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Your plugin is also an [`EventEmitter`](https://github.com/mistic100/uEvent) wit
```js
import { AbstractPlugin } from 'photo-sphere-viewer';

export default class PhotoSphereViewerCustomPlugin extends AbstractPlugin {
export class PhotoSphereViewerCustomPlugin extends AbstractPlugin {

static id = 'custom-plugin';

Expand Down Expand Up @@ -68,7 +68,8 @@ export default {
],
plugins : [
require('rollup-plugin-babel')({
exclude: 'node_modules/**',
exclude : 'node_modules/**',
babelHelpers: 'bundled',
}),
],
};
Expand Down Expand Up @@ -192,7 +193,7 @@ A plugin can expose one or more settings to the viewer by using the [Settings pl
This is done by requiring the settings plugin and calling the `addSetting` method. Consult the [Settings plugin](./plugin-settings.md) page for more information.

```js
import SettingsPlugin from 'photo-sphere-viewer/plugins/settings';
import { SettingsPlugin } from 'photo-sphere-viewer/dist/plugins/settings';

export default class PhotoSphereViewerCustomPlugin extends AbstractPlugin {

Expand Down Expand Up @@ -227,6 +228,23 @@ export default class PhotoSphereViewerCustomPlugin extends AbstractPlugin {
}
```

For this two work you will need two modifications in your rollup configuration:

```js
// rollup.config.js

export default {
output : {
globals : {
'photo-sphere-viewer/dist/plugins/settings': 'PhotoSphereViewer.SettingsPlugin',
},
},
external: [
'photo-sphere-viewer/dist/plugins/settings',
],
};
```


## Naming and publishing

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"description": "A JavaScript library to display Photo Sphere panoramas",
"homepage": "https://photo-sphere-viewer.js.org",
"main": "dist/photo-sphere-viewer.js",
"types": "dist/photo-sphere-viewer.d.ts",
"files": [
"src/",
"dist/"
"dist/",
"types/"
],
"authors": [
{
Expand All @@ -32,7 +34,7 @@
},
"dependencies": {
"three": "^0.131.2",
"uevent": "~2.0.0"
"uevent": "~2.0.1"
},
"devDependencies": {
"@babel/core": "^7.7.4",
Expand All @@ -42,7 +44,6 @@
"@babel/register": "^7.7.4",
"@csstools/postcss-sass": "https://github.com/sinankeskin/postcss-sass#master",
"@pixi/jsdoc-template": "^2.5.1",
"@rollup/plugin-alias": "^3.0.1",
"@rollup/plugin-babel": "^5.2.2",
"@rollup/plugin-replace": "^3.0.0",
"@vuepress/plugin-active-header-links": "^1.2.0",
Expand All @@ -68,6 +69,7 @@
"promise-polyfill": "^8.1.3",
"raw-loader": "^4.0.0",
"rollup": "^2.36.2",
"rollup-plugin-dts": "^3.0.2",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-string": "^3.0.0",
Expand Down
90 changes: 69 additions & 21 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import alias from '@rollup/plugin-alias';
import babel from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import fs from 'fs';
import path from 'path';
import localResolve from 'rollup-plugin-local-resolve';
import postcss from 'rollup-plugin-postcss'
import dts from 'rollup-plugin-dts';
import { string } from 'rollup-plugin-string';

import pkg from './package.json';

const plugins = fs.readdirSync(path.join(__dirname, 'src/plugins'))
.filter(p => p !== 'AbstractPlugin.js');
.filter(p => fs.lstatSync(`src/plugins/${p}`).isDirectory());

const adapters = fs.readdirSync(path.join(__dirname, 'src/adapters'))
.filter(p => p !== 'AbstractAdapter.js')
.filter(p => fs.lstatSync(`src/adapters/${p}`).isDirectory())
.filter(p => p !== 'equirectangular');

const banner = `/*!
Expand Down Expand Up @@ -49,8 +49,6 @@ const baseConfig = {
'three',
'uevent',
],
// wrapped in a function to ensure unique plugin instances for each entry-point
// https://github.com/egoist/rollup-plugin-postcss/issues/158
plugins : () => [
localResolve(),
babel({
Expand Down Expand Up @@ -95,21 +93,46 @@ const secondaryConfig = {
'photo-sphere-viewer',
...plugins.map(p => `photo-sphere-viewer/dist/plugins/${p}`),
],

plugins: () => [
...baseConfig.plugins(),
alias({
'photo-sphere-viewer': './src',
...plugins.reduce((alias, p) => {
alias[`photo-sphere-viewer/dist/plugins/${p}`] = `./src/plugins/${p}`;
return alias;
}, {}),
}),
replace({
// configuration to embed the examples files in PSV source
delimiters : ['', ''],
preventAssignment : true,
[`from 'three/examples/jsm`]: `from '../../../three-examples`,
[`from '../..'`] : `from 'photo-sphere-viewer'`,
...plugins.reduce((replace, p) => {
replace[`from '../${p}'`] = `from 'photo-sphere-viewer/dist/plugins/${p}'`;
return replace;
}, {}),
}),
...baseConfig.plugins(),
],
};

const baseConfigDTS = {
output : {
format: 'es',
},
plugins: () => [
dts(),
],
};

const secondaryConfigDTS = {
...baseConfigDTS,
external: [
...secondaryConfig.external,
],
plugins : () => [
replace({
delimiters : ['', ''],
preventAssignment: true,
[`from '../..'`] : `from 'photo-sphere-viewer'`,
...plugins.reduce((replace, p) => {
replace[`from '../${p}'`] = `from 'photo-sphere-viewer/dist/plugins/${p}'`;
return replace;
}, {}),
}),
...baseConfigDTS.plugins(),
],
};

Expand All @@ -134,8 +157,16 @@ export default [
},
plugins: secondaryConfig.plugins(),
},
].concat(
plugins.map(p => ({
{
...baseConfigDTS,
input : 'types/index.d.ts',
output : {
...baseConfigDTS.output,
file: 'dist/photo-sphere-viewer.d.ts',
},
plugins: baseConfigDTS.plugins(),
},
...plugins.map(p => ({
...secondaryConfig,
input : `src/plugins/${p}/index.js`,
output : {
Expand All @@ -144,9 +175,8 @@ export default [
name: `PhotoSphereViewer.${camelize(p)}Plugin`,
},
plugins: secondaryConfig.plugins(),
}))
).concat(
adapters.map(p => ({
})),
...adapters.map(p => ({
...secondaryConfig,
input : `src/adapters/${p}/index.js`,
output : {
Expand All @@ -155,5 +185,23 @@ export default [
name: `PhotoSphereViewer.${camelize(p)}Adapter`,
},
plugins: secondaryConfig.plugins(),
})),
...plugins.map(p => ({
...secondaryConfigDTS,
input : `types/plugins/${p}/index.d.ts`,
output : {
...secondaryConfigDTS.output,
file: `dist/plugins/${p}.d.ts`,
},
plugins: secondaryConfigDTS.plugins(),
})),
...adapters.map(p => ({
...secondaryConfigDTS,
input : `types/adapters/${p}/index.d.ts`,
output : {
...secondaryConfigDTS.output,
file: `dist/adapters/${p}.d.ts`,
},
plugins: secondaryConfigDTS.plugins(),
}))
);
];
10 changes: 9 additions & 1 deletion src/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CONFIG_PARSERS, DEFAULTS, DEPRECATED_OPTIONS, getConfig, READONLY_OPTIO
import { CHANGE_EVENTS, EVENTS, IDS, SPHERE_RADIUS, VIEWER_DATA } from './data/constants';
import { SYSTEM } from './data/system';
import errorIcon from './icons/error.svg';
import { AbstractPlugin } from './plugins/AbstractPlugin';
import { PSVError } from './PSVError';
import { DataHelper } from './services/DataHelper';
import { EventsHandler } from './services/EventsHandler';
Expand All @@ -24,6 +25,7 @@ import {
isExtendedPosition,
isFullscreenEnabled,
logWarn,
pluginInterop,
requestFullscreen,
throttle,
toggleClass
Expand Down Expand Up @@ -359,7 +361,13 @@ export class Viewer extends EventEmitter {
* @returns {PSV.plugins.AbstractPlugin}
*/
getPlugin(pluginId) {
return pluginId ? this.plugins[typeof pluginId === 'function' ? pluginId.id : pluginId] : null;
if (typeof pluginId === 'string') {
return this.plugins[pluginId];
}
else {
const pluginCtor = pluginInterop(pluginId, AbstractPlugin);
return pluginCtor ? this.plugins[pluginCtor.id] : undefined;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class AbstractAdapter {
}

/**
* @summary Destroys the service
* @summary Destroys the adapter
*/
destroy() {
delete this.psv;
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/cubemap/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractAdapter, CONSTANTS, PSVError, SYSTEM, utils } from 'photo-sphere-viewer';
import * as THREE from 'three';
import { AbstractAdapter, CONSTANTS, PSVError, SYSTEM, utils } from '../..';

/**
* @typedef {Object} PSV.adapters.CubemapAdapter.Cubemap
Expand All @@ -20,7 +20,7 @@ const CUBE_HASHMAP = ['left', 'right', 'top', 'bottom', 'back', 'front'];
* @summary Adapter for cubemaps
* @memberof PSV.adapters
*/
export default class CubemapAdapter extends AbstractAdapter {
export class CubemapAdapter extends AbstractAdapter {

static id = 'cubemap';
static supportsTransition = true;
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/equirectangular-tiles/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AbstractAdapter, CONSTANTS, PSVError, SYSTEM, utils } from 'photo-sphere-viewer';
import * as THREE from 'three';
import { AbstractAdapter, CONSTANTS, PSVError, SYSTEM, utils } from '../..';
import { Queue } from './Queue';
import { Task } from './Task';

Expand Down Expand Up @@ -54,7 +54,7 @@ function powerOfTwo(x) {
* @summary Adapter for tiled panoramas
* @memberof PSV.adapters
*/
export default class EquirectangularTilesAdapter extends AbstractAdapter {
export class EquirectangularTilesAdapter extends AbstractAdapter {

static id = 'equirectangular-tiles';
static supportsTransition = false;
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/equirectangular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const SPHERE_SEGMENTS = 64;
* @summary Adapter for equirectangular panoramas
* @memberof PSV.adapters
*/
export default class EquirectangularAdapter extends AbstractAdapter {
export class EquirectangularAdapter extends AbstractAdapter {

static id = 'equirectangular';
static supportsTransition = true;
Expand Down
14 changes: 8 additions & 6 deletions src/data/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AbstractAdapter } from '../adapters/AbstractAdapter';
import { EquirectangularAdapter } from '../adapters/equirectangular';
import { AbstractPlugin } from '../plugins/AbstractPlugin';
import { PSVError } from '../PSVError';
import EquirectangularAdapter from '../adapters/equirectangular';
import { bound, clone, deepmerge, each, logWarn, parseAngle, parseSpeed } from '../utils';
import { bound, clone, deepmerge, each, logWarn, parseAngle, parseSpeed, pluginInterop } from '../utils';
import { ACTIONS } from './constants';

/**
Expand Down Expand Up @@ -112,10 +114,10 @@ export const CONFIG_PARSERS = {
return [EquirectangularAdapter];
}
else if (Array.isArray(adapter)) {
return adapter;
return [pluginInterop(adapter[0], AbstractAdapter), adapter[1]];
}
else {
return [adapter];
return [pluginInterop(adapter, AbstractAdapter)];
}
},
defaultLong : (defaultLong) => {
Expand Down Expand Up @@ -187,10 +189,10 @@ export const CONFIG_PARSERS = {
return plugins
.map((plugin) => {
if (Array.isArray(plugin)) {
return plugin;
return [pluginInterop(plugin[0], AbstractPlugin), plugin[1]];
}
else {
return [plugin];
return [pluginInterop(plugin, AbstractPlugin)];
}
})
.filter(plugin => !!plugin[0]);
Expand Down
Loading

0 comments on commit 8b560f0

Please sign in to comment.