Skip to content

Commit

Permalink
Simple README and prepare for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
itanka9 committed Sep 21, 2024
1 parent 83bcab8 commit 022fd55
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 107 deletions.
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright © 2024 itanka9 <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# @2gis/mapgl-share

A sharing control for the `@2gis/mapgl` library.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [ShareControlOptions](#sharecontroloptions)
- [Development](#development)
- [Build](#build)
- [Preview](#preview)
- [License](#license)

## Installation

To install the dependencies, run:

```bash
npm install
```

## Usage

Here's an example of how to use the sharing control with @2gis/mapgl:

```js
import { Map } from '@2gis/mapgl';
import { ShareControl } from '@2gis/mapgl-share';

const map = new Map('map-container', {
center: [55.751244, 37.618423],
zoom: 10,
});

const control = new ShareControl(map, {
locale: navigator.language,
urlMaker: (center, zoom, rotation, pitch) => {
const viewport = `${center.toString()}/${zoom}/p/${pitch}/r/${rotation}`;

return `${self.origin}${location.pathname}?m=${encodeURIComponent(viewport)}`;
},
})

```


## Control Options

| Option | Type | Description |
|--------------|------------|-----------------------------------------------------------------------------------------------|
| `position` | `ControlPosition` | Position of the control. |
| `locale` | `string` | Locale to use for UI elements. |
| `content` | `string` | Content of the control. Use this to customize the control button via providing custom HTML code. |
| `cssPrefix` | `string` | Prefix for CSS classes. Use this to customize the control appearance with your own styles. |
| `urlMaker` | `UrlMaker` | Function to construct sharing URL. |
| `mapCodeMaker` | `MapCode` | Function to construct map code. |

### ControlPosition

Possible values for `ControlPosition`:

- `topLeft`
- `topCenter`
- `topRight`
- `centerLeft`
- `centerRight`
- `bottomLeft`
- `bottomCenter`
- `bottomRight`

## Development

Install deps

```bash
npm i
```

To start the development server, run:

```
npm run dev
```

Build

```
npm run build
```

## License

This project is licensed under the MIT License.
11 changes: 11 additions & 0 deletions lib.vite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { resolve } from 'path'
import { defineConfig } from 'vite'

export default defineConfig({
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'mapglShare'
}
}
});
52 changes: 52 additions & 0 deletions locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const locales: Record<string, Record<string, string>> = {
buttonTitle: {
en: 'Share',
fr: 'Partager',
de: 'Teilen',
zh: '分享',
hy: 'Կիսվել',
ka: 'გაზიარება',
ru: 'Поделиться',
},
copyButton: {
en: 'Copy',
},
previewButton: {
en: 'Preview',
fr: 'Aperçu',
de: 'Vorschau',
zh: '预览',
hy: 'Նախադիտել',
ka: 'წინასწარი ნახვა',
ru: 'Предпросмотр',
},
popupTitle: {
en: 'Share the map',
fr: 'Partager la carte',
de: 'Karte teilen',
zh: '分享地图',
hy: 'Կիսվել քարտեզով',
ka: 'გაზიარება რუკის',
ru: 'Поделиться картой',
},
linkLabel: {
en: 'Get a link',
fr: 'Obtenir un lien',
de: 'Link erhalten',
zh: '获取链接',
hy: 'Ստանալ հղում',
ka: 'ბმულის მიღება',
ru: 'Получить ссылку',
},
embedLabel: {
en: 'Embed into page',
fr: 'Intégrer dans la page',
de: 'In die Seite einbetten',
zh: '嵌入页面',
hy: 'Ներդնել էջում',
ka: 'ჩასმა გვერდზე',
ru: 'Встроить в страницу',
},
};

export default locales;
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"name": "mkmap",
"private": true,
"version": "0.0.0",
"name": "@2gis/mapgl-share",
"version": "0.1.0",
"type": "module",
"main": "dist/mapgl-share.js",
"module": "dist/mapgl-share.umd.js",
"typings": "dist/src/index.d.ts",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
"build": "tsc && vite build -c ./lib.vite.js && npm run typings",
"preview": "vite preview",
"typings": "tsc -p tsconfig.typings.json --declaration --emitDeclarationOnly --outDir dist",
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@2gis/mapgl": "^1.50.1",
Expand Down
1 change: 1 addition & 0 deletions src/clipboard.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare function copyTextToClipboard(text: string): void;
72 changes: 72 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type * as MapGL from "@2gis/mapgl/types";
import { ControlOptions } from "@2gis/mapgl/types";
declare global {
interface Window {
mapgl: typeof MapGL;
}
}
export type UrlMaker = (center: number[], zoom: number, rotation: number, pitch: number) => string;
export type MapCodeMaker = (center: number[], zoom: number, rotation: number, pitch: number) => string;
export interface ShareControlOptions extends Partial<ControlOptions> {
/**
* Locale to use for UI elements.
*/
locale?: string;
/**
* Content of the control. Use this to customize the control button via providing custom HTML code.
*/
content?: string;
/**
* Prefix for CSS classes. Use this to customize the control appearance with your own styles.
*/
cssPrefix?: string;
/**
* Function to construct sharing URL.
*/
urlMaker?: UrlMaker;
/**
* Function to construct map code.
*/
mapCodeMaker?: MapCodeMaker;
}
/**
* Control for sharing map view for MapGL JS API.
*
* @param map - MapGL instance.
* @param options - Control options.
* @returns ShareControl instance.
*
* @example
*
* ```js
* import { ShareControl } from 'mapgl-share';
* import { load } from '@2gis/mapgl';
*
* const map = new mapgl.Map('map', {
* key: MAPGL_JS_API_KEY,
* ...
* });
*
* const control = new ShareControl(map, {
* locale: navigator.language,
* urlMaker: (center, zoom, rotation, pitch) => {
* const viewport = `${center.toString()}/${zoom}/p/${pitch}/r/${rotation}`;
*
* return `${self.origin}${location.pathname}?m=${encodeURIComponent(viewport)}`;
* },
* ...
* })
* ```
*/
export declare class ShareControl {
private map;
private options;
private popup?;
private locale?;
constructor(map: MapGL.Map, options?: ShareControlOptions);
private hidePopup;
private updateFields;
private showPopup;
private togglePreview;
private updatePreview;
}
66 changes: 59 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type * as MapGL from "@2gis/mapgl/types";

import { ControlOptions } from "@2gis/mapgl/types";
import { copyTextToClipboard } from "./clipboard";
import { l } from "./l10n";

declare global {
interface Window {
Expand All @@ -14,27 +15,78 @@ export type UrlMaker = (center: number[], zoom: number, rotation: number, pitch:
export type MapCodeMaker = (center: number[], zoom: number, rotation: number, pitch: number) => string;

export interface ShareControlOptions extends Partial<ControlOptions> {
/**
* Locale to use for UI elements.
*/
locale?: string,

/**
* Content of the control. Use this to customize the control button via providing custom HTML code.
*/
content?: string,

/**
* Prefix for CSS classes. Use this to customize the control appearance with your own styles.
*/
cssPrefix?: string,

/**
* Function to construct sharing URL.
*/
urlMaker?: UrlMaker,

/**
* Function to construct map code.
*/
mapCodeMaker?: MapCodeMaker
}

const iconUrl = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCg0KPHN2ZyB3aWR0aD0iMjRweCIgaGVpZ2h0PSIyNHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+DQo8Zz4NCjxwYXRoIGlkPSJWZWN0b3IiIGQ9Ik05IDZMMTIgM00xMiAzTDE1IDZNMTIgM1YxM003LjAwMDIzIDEwQzYuMDY4MzUgMTAgNS42MDI0MSAxMCA1LjIzNDg2IDEwLjE1MjJDNC43NDQ4MSAxMC4zNTUyIDQuMzU1MjMgMTAuNzQ0OCA0LjE1MjI0IDExLjIzNDlDNCAxMS42MDI0IDQgMTIuMDY4MSA0IDEzVjE3LjhDNCAxOC45MjAxIDQgMTkuNDc5OCA0LjIxNzk5IDE5LjkwNzZDNC40MDk3MyAyMC4yODM5IDQuNzE1NDcgMjAuNTkwNSA1LjA5MTggMjAuNzgyMkM1LjUxOTIgMjEgNi4wNzg5OSAyMSA3LjE5NjkxIDIxSDE2LjgwMzZDMTcuOTIxNSAyMSAxOC40ODA1IDIxIDE4LjkwNzkgMjAuNzgyMkMxOS4yODQyIDIwLjU5MDUgMTkuNTkwNSAyMC4yODM5IDE5Ljc4MjIgMTkuOTA3NkMyMCAxOS40ODAyIDIwIDE4LjkyMSAyMCAxNy44MDMxVjEzQzIwIDEyLjA2ODEgMTkuOTk5OSAxMS42MDI0IDE5Ljg0NzcgMTEuMjM0OUMxOS42NDQ3IDEwLjc0NDggMTkuMjU1NCAxMC4zNTUyIDE4Ljc2NTQgMTAuMTUyMkMxOC4zOTc4IDEwIDE3LjkzMTkgMTAgMTcgMTAiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4NCjwvZz4NCjwvc3ZnPg==';

/**
* Control for sharing map view for MapGL JS API.
*
* @param map - MapGL instance.
* @param options - Control options.
* @returns ShareControl instance.
*
* @example
*
* ```js
* import { ShareControl } from 'mapgl-share';
* import { load } from '@2gis/mapgl';
*
* const map = new mapgl.Map('map', {
* key: MAPGL_JS_API_KEY,
* ...
* });
*
* const control = new ShareControl(map, {
* locale: navigator.language,
* urlMaker: (center, zoom, rotation, pitch) => {
* const viewport = `${center.toString()}/${zoom}/p/${pitch}/r/${rotation}`;
*
* return `${self.origin}${location.pathname}?m=${encodeURIComponent(viewport)}`;
* },
* ...
* })
* ```
*/
export class ShareControl {
private popup?: HTMLElement;
private locale?: string;

constructor (private map: MapGL.Map, private options: ShareControlOptions = {}) {
options.cssPrefix ??= 'mapgl-share';
this.locale = options.locale

if (!options.urlMaker && !options.mapCodeMaker) {
throw new Error('Do not know how to construct sharing URL or map code.')
}

const control = new window.mapgl.Control(
map,
options.content ?? `<button id="${options.cssPrefix}-button" class="${options.cssPrefix}-border"><img src="${iconUrl}"></button>`,
options.content ?? `<button id="${options.cssPrefix}-button" title="${l('%buttonTitle%', this.locale)}" class="${options.cssPrefix}-border"><img src="${iconUrl}"></button>`,
{
position: options.position ?? 'topRight',
...options
Expand Down Expand Up @@ -123,19 +175,19 @@ export class ShareControl {
popup.innerHTML = `<div class="${cssPrefix}-veil" data-action="close">
<div class="${cssPrefix}-body ${cssPrefix}-border">
<button class="${cssPrefix}-closer" data-action="close">×</button>
<h3>Поделиться картой</h3>
<h3>${l('%popupTitle%', this.locale)}</h3>
<p style="display: ${urlMaker ? 'block' : 'none'}">
<label>Ссылка на карту</label>
<label>${l('%linkLabel%', this.locale)}</label>
<input id="${cssPrefix}-url-field" class="${cssPrefix}-input">
<button class="${cssPrefix}-copy" data-action="copy">copy</button>
<button class="${cssPrefix}-copy" data-action="copy">${l('%copyButton%', this.locale)}</button>
</p>
<p style="display: ${mapCodeMaker ? 'block' : 'none'}">
<label>Код для вставки на сайт</label>
<label>${l('%embedLabel%', this.locale)}</label>
<input id="${cssPrefix}-code-field" class="${cssPrefix}-input">
<button class="${cssPrefix}-copy" data-action="copy">copy</button>
<button class="${cssPrefix}-preview" data-action="preview">preview</button>
<button class="${cssPrefix}-copy" data-action="copy">${l('%copyButton%', this.locale)}</button>
<button class="${cssPrefix}-preview" data-action="preview">${l('%previewButton%', this.locale)}</button>
</p>
<iframe style="display: none;" width="560" height="360" id="${cssPrefix}-preview-placeholder"></iframe>
Expand Down
9 changes: 9 additions & 0 deletions src/l10n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

import locales from '../locales/index';


const defaultLocale = 'en';

export function l (s: string, locale = defaultLocale) {
return s.replace(/%(\S+)%/g, (_, v) => locales[v]?.[locale] ?? locales[v]?.[defaultLocale] ?? v);
}
Loading

0 comments on commit 022fd55

Please sign in to comment.