Skip to content

Commit

Permalink
feat: location plugin DRAFT
Browse files Browse the repository at this point in the history
  • Loading branch information
phoebus-84 committed Sep 19, 2024
1 parent 678acb3 commit ee8d9f4
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/location/.npmignore
41 changes: 41 additions & 0 deletions pkg/location/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@slangroom/location",
"dependencies": {
"@slangroom/core": "workspace:*"
},
"version": "1.40",
"repository": "https://github.com/dyne/slangroom",
"license": "AGPL-3.0-only",
"type": "module",
"main": "./build/esm/src/index.js",
"browser": "./build/slangroom.js",
"types": "./build/esm/src/index.d.ts",
"files": [
"./build/**/*.js"
],
"exports": {
".": {
"import": {
"types": "./build/esm/src/index.d.ts",
"default": "./build/esm/src/index.js"
}
},
"./*": {
"import": {
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/node": "^20.10.4",
"esbuild": "^0.21.4"
},
"engines": {
"node": "^18.20.0 || ^20.10.0 || ^22"
}
}
3 changes: 3 additions & 0 deletions pkg/location/package.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2024 Dyne.org foundation

SPDX-License-Identifier: AGPL-3.0-or-later
1 change: 1 addition & 0 deletions pkg/location/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@slangroom/location/plugin'
207 changes: 207 additions & 0 deletions pkg/location/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// read the version from the package.json
import packageJson from '@slangroom/location/package.json' with { type: 'json' };
import { Plugin, PluginContext } from '@slangroom/core';

export const version = packageJson.version;

export class LocationBaseError extends Error {
constructor(message: string) {
super(message);
this.name = 'Slangroom @slangroom/location@' + packageJson.version + ' Error';
}
}

const p = new Plugin();

const isBrowser = (ctx:PluginContext)=>{
if (window === undefined) {
return ctx.fail(new LocationBaseError('You must be in a browser environment to use this plugin'));
}
return undefined
}

const isUrlAString = (ctx:PluginContext, url:unknown)=>{
if (typeof url !== 'string') {
return ctx.fail(new LocationBaseError('url must be a string'));
}
return undefined
}


/**
* @internal
*/
export const getLocation = p.new('connect', 'get the current location', (ctx) => {
isBrowser(ctx)
const location = window.location
return ctx.pass({
href: location.href,
protocol: location.protocol,
host: location.host,
hostname: location.hostname,
port: location.port,
pathname: location.pathname,
search: location.search,
hash: location.hash
})
});

/**
* @internal
*/
export const getHref = p.new('connect', 'get the current location href', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.href)
});

/**
* @internal
*/
export const getProtocol = p.new('connect', 'get the current location protocol', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.protocol)
});

/**
* @internal
*/
export const getHost = p.new('connect', 'get the current location host', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.host)
});

/**
* @internal
*/
export const getHostname = p.new('connect', 'get the current location hostname', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.hostname)
});

/**
* @internal
*/
export const getPort = p.new('connect', 'get the current location port', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.port)
});

/**
* @internal
*/
export const getPathname = p.new('connect', 'get the current location pathname', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.pathname)
});

/**
* @internal
*/
export const getSearch = p.new('connect', 'get the current location search params', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.search)
});

/**
* @internal
*/
export const getHash = p.new('connect', 'get the current location hash', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.location.hash)
});

/**
* @internal
*/
export const reload = p.new('connect', 'reload the current page', (ctx) => {
isBrowser(ctx)
window.location.reload()
return ctx.pass('reload');
});

/**
* @internal
*/
export const replace = p.new(['url'], 'replace the current location', (ctx) => {
isBrowser(ctx)
const url = ctx.get('url') as string;
isUrlAString(ctx,url)
window.location.replace(url)
return ctx.pass('replace');
});

/**
* @internal
*/
export const assign = p.new(['url'], 'assign the current location', (ctx) => {
isBrowser(ctx)
const url = ctx.get('url') as string;
isUrlAString(ctx,url)
window.location.assign(url)
return ctx.pass('assign');
});

/**
* @internal
*/
export const redirectToUrl = p.new(['url'], 'redirect to the url', (ctx) => {
isBrowser(ctx)
const url = ctx.get('url') as string;
isUrlAString(ctx,url)
window.location.href = url
return ctx.pass('redirect');
});

/**
* @internal
*/
export const goBack = p.new(['url'], 'go back in history', (ctx) => {
isBrowser(ctx)
window.history.back()
return ctx.pass('back');
});

/**
* @internal
*/
export const goForward = p.new(['url'], 'go forward in history', (ctx) => {
isBrowser(ctx)
window.history.forward()
return ctx.pass('forward');
});

/**
* @internal
*/
export const go = p.new(['index'], 'go to a specific page in history', (ctx) => {
isBrowser(ctx)
const index = ctx.get('index')
if (typeof index !== 'number') {
return ctx.fail(new LocationBaseError('index must be a number'))
}
window.history.go(index)
return ctx.pass('go');
});

/**
* @internal
*/
export const length = p.new('connect', 'get the history length', (ctx) => {
isBrowser(ctx)
return ctx.pass(window.history.length)
});



/**
* @internal
*/
export const openWindow = p.new(['url'], 'open the url in a new window', (ctx) => {
isBrowser(ctx)
const url = ctx.get('url') as string;
isUrlAString(ctx,url)
window.open(url, '_blank')
return ctx.pass('open');
});

export const location = p
1 change: 1 addition & 0 deletions pkg/location/tsconfig.json
3 changes: 3 additions & 0 deletions pkg/location/tsconfig.json.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2024 Dyne.org foundation

SPDX-License-Identifier: AGPL-3.0-or-later

0 comments on commit ee8d9f4

Please sign in to comment.