diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1cc9921 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2024 itanka9 + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcd5613 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/lib.vite.js b/lib.vite.js new file mode 100644 index 0000000..2618616 --- /dev/null +++ b/lib.vite.js @@ -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' + } + } +}); \ No newline at end of file diff --git a/locales/index.ts b/locales/index.ts new file mode 100644 index 0000000..61be471 --- /dev/null +++ b/locales/index.ts @@ -0,0 +1,52 @@ +const locales: Record> = { + 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; \ No newline at end of file diff --git a/package.json b/package.json index 32cb6fe..37fef7f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/clipboard.d.ts b/src/clipboard.d.ts new file mode 100644 index 0000000..1258c32 --- /dev/null +++ b/src/clipboard.d.ts @@ -0,0 +1 @@ +export declare function copyTextToClipboard(text: string): void; diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..f4a15fc --- /dev/null +++ b/src/index.d.ts @@ -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 { + /** + * 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; +} diff --git a/src/index.ts b/src/index.ts index 680dfc2..a6559e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -14,19 +15,70 @@ 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 { + /** + * 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,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCg0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+ * @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.') @@ -34,7 +86,7 @@ export class ShareControl { const control = new window.mapgl.Control( map, - options.content ?? ``, + options.content ?? ``, { position: options.position ?? 'topRight', ...options @@ -123,19 +175,19 @@ export class ShareControl { popup.innerHTML = `
-

Поделиться картой

+

${l('%popupTitle%', this.locale)}

- + - +

- + - - + +

diff --git a/src/l10n.ts b/src/l10n.ts new file mode 100644 index 0000000..0897247 --- /dev/null +++ b/src/l10n.ts @@ -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); +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 27b54da..ebe14b5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { ShareControl } from '.'; import './style.css'; import { load } from '@2gis/mapgl'; -const MAPGL_JS_API_KEY = 'a1893935-6834-4445-b97a-3405fb426c5b'; +const MAPGL_JS_API_KEY = 'cb20c5bf-34d3-4f0e-9b2b-33e9b8edb57f'; load().then(mapgl => { const url = new URL(location.href); @@ -16,12 +16,15 @@ load().then(mapgl => { zoom: params ? +params[1] : 2, pitch: params ? +params[3] : 0, rotation: params ? +params[5] : 0, + loopWorld: true, + enableTrackResize: true, styleState: { immersiveRoadsOn: true } }); new ShareControl(map, { + locale: navigator.language, urlMaker: (center, zoom, rotation, pitch) => `${self.origin}${location.pathname}?m=${encodeURIComponent(`${center.toString()}/${zoom}/p/${pitch}/r/${rotation}`)}`, mapCodeMaker: (center, zoom, rotation, pitch) => { const id =`map-${Math.trunc(Math.random() * 10**9)}`; diff --git a/src/style.css b/src/style.css index d139f02..b3d9258 100644 --- a/src/style.css +++ b/src/style.css @@ -1,18 +1,3 @@ -:root { - font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - line-height: 1.5; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - a { font-weight: 500; color: #646cff; @@ -31,69 +16,12 @@ body { display: flex; } -#app, #map, #popup-wrap { +#app, #map { display: flex; width: 100vw; height: 100vh; } -#popup-wrap { - position: absolute; - top: 0; - left: 0; - display: none; - flex-direction: row; - align-items: center; - justify-content: center; - background-color: #0008; - z-index: 200; -} - -#popup { - box-shadow: 0 1px 3px 0 rgba(38, 38, 38, 0.5); - border-radius: 4px; - padding: 4px; - display: flex; - flex-direction: column; - background: #fff; -} - -#popup .header { - flex-grow: 1; - display: flex; - align-items: center; - padding: 0 8px; - font-weight: bolder; -} - -#popup .row { - display: flex; - align-items: flex-start; -} - -#popup iframe { - flex-grow: 1; -} - -#popup textarea { - width: 200px; - height: 360px; - font-size: 10px; -} - -#src { - width: calc(100% - 16px); - height: calc(100% - 16px); - border: solid 1px #eee; - margin: 8px; -} - -#preview { - border: solid 1px #eee; - width: 560px; - height: 360px; -} - #mapgl-share-button { display: flex; align-items: center; @@ -111,16 +39,24 @@ body { } .mapgl-share-veil { - z-index: 10; + z-index: 100; position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; - background: #fff3; + background: #fff8; display: flex; align-items: center; justify-content: center; + + font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; + line-height: 1.5; + font-weight: 400; +} + +.mapgl-share-veil button:active { + transform: scale(1.1); } .mapgl-share-body { @@ -179,32 +115,19 @@ body { align-items: center; justify-content: center; background: transparent; - font-size: 14px; + color: #999; + font-size: 24px; top: 10px; right: 10px; width: 16px; - height: 16px; + height: 16px; } .spreader { flex-grow: 1; } + button { - border: 1px solid transparent; - cursor: pointer; - transition: border-color 0.25s; -} - -@media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; - } -} + border: 2px transparent; +} \ No newline at end of file diff --git a/tsconfig.typings.json b/tsconfig.typings.json new file mode 100644 index 0000000..c4d2fff --- /dev/null +++ b/tsconfig.typings.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "module": "ESNext", + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + "isolatedModules": true, + "moduleDetection": "force", + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": ["src/index.ts"] + } + \ No newline at end of file