Skip to content

Commit bbb98d5

Browse files
committed
add debug logging utility
1 parent 0e8d5ca commit bbb98d5

File tree

9 files changed

+166
-27
lines changed

9 files changed

+166
-27
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"license": "MIT",
2020
"dependencies": {
2121
"@mdi/js": "^7.4.47",
22+
"debug": "^4.4.3",
2223
"home-assistant-js-websocket": "^9.5.0",
2324
"home-assistant-styles-manager": "^3.1.0",
2425
"lit": "^3.3.1",
@@ -34,7 +35,9 @@
3435
"@rollup/plugin-commonjs": "^28.0.6",
3536
"@rollup/plugin-json": "^6.1.0",
3637
"@rollup/plugin-node-resolve": "^16.0.2",
38+
"@rollup/plugin-replace": "^6.0.2",
3739
"@rollup/plugin-terser": "^0.4.4",
40+
"@types/debug": "^4.1.12",
3841
"@types/leaflet": "^1.9.20",
3942
"@types/leaflet-providers": "^1.2.5",
4043
"@types/lodash": "^4.17.20",
@@ -66,6 +69,7 @@
6669
"scripts": {
6770
"start": "rollup -c rollup.config.js --bundleConfigAsCjs --watch",
6871
"dev": "pnpm run start",
72+
"debug": "cross-env DEBUG=true pnpm run dev",
6973
"build": "pnpm run lint && pnpm run rollup",
7074
"lint": "eslint src/**/*.ts",
7175
"rollup": "rollup -c rollup.config.js --bundleConfigAsCjs",

pnpm-lock.yaml

Lines changed: 43 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.helper.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function logCardInfo(version) {
3131
}
3232

3333
export const defaultPlugins = [
34-
nodeResolve({ preferBuiltins: false }),
34+
nodeResolve(),
3535
commonjs(),
3636
babel({
3737
babelHelpers: 'bundled',

rollup.config.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import typescript from 'rollup-plugin-typescript2';
22
import terser from '@rollup/plugin-terser';
3+
import replace from '@rollup/plugin-replace';
34
import serve from 'rollup-plugin-serve';
5+
46
import { logCardInfo, defaultPlugins } from './rollup.config.helper.mjs';
57

68
import { version } from './package.json';
79

810
const dev = process.env.ROLLUP_WATCH;
911
const port = process.env.PORT || 8235;
12+
const debug = process.env.DEBUG;
13+
1014
const currentVersion = dev ? 'DEVELOPMENT' : `v${version}`;
1115
const custombanner = logCardInfo(currentVersion);
1216

@@ -29,12 +33,17 @@ const terserOpt = {
2933
},
3034
};
3135

36+
const replaceOpts = {
37+
preventAssignment: true,
38+
'process.env.DEBUG': JSON.stringify(debug),
39+
};
40+
3241
const plugins = [
3342
dev && serve(serveopts),
3443
!dev && terser(terserOpt),
3544
typescript({
3645
sourceMap: dev,
37-
outputToFilesystem: true,
46+
outputToFilesystem: false,
3847
}),
3948
];
4049

@@ -47,13 +56,13 @@ export default [
4756
format: 'es',
4857
inlineDynamicImports: true,
4958
sourcemap: dev,
50-
banner: !dev ? custombanner : undefined,
59+
banner: custombanner,
5160
},
5261
],
5362
watch: {
5463
exclude: 'node_modules/**',
5564
},
56-
plugins: [...defaultPlugins, ...plugins],
65+
plugins: [replace(replaceOpts), ...defaultPlugins, ...plugins],
5766
moduleContext: (id) => {
5867
const thisAsWindowForModules = [
5968
'node_modules/@formatjs/intl-utils/lib/src/diff.js',

src/components/vsc-mini-map.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
1+
// debugger
2+
import { debug } from '../utils/debuglog';
3+
4+
const debuglog = debug.extend('mini-map');
5+
16
import L from 'leaflet';
27
import 'leaflet-providers/leaflet-providers.js';
38
import mapstyle from 'leaflet/dist/leaflet.css';
49
import { css, CSSResultGroup, html, PropertyValues, TemplateResult, unsafeCSS } from 'lit';
510
import { customElement, property, state } from 'lit/decorators.js';
11+
import { styleMap } from 'lit/directives/style-map.js';
612

713
import { COMPONENT } from '../constants/const';
814
import { Address, MapData, MiniMapConfig } from '../types/config';
915
import { SECTION } from '../types/section';
16+
import { isSafari } from '../utils';
1017
import { BaseElement } from '../utils/base-element';
1118
import { _getMapAddress } from '../utils/lovelace/create-map-card';
1219
import { showHaMapDialog } from '../utils/lovelace/show-map-dialog';
1320

1421
export const DEFAULT_HOURS_TO_SHOW = 0;
1522
export const DEFAULT_ZOOM = 14;
1623

24+
export const CARD_MAP_POSITION = ['default', 'top', 'bottom', 'single'] as const;
25+
export type CardMapPosition = (typeof CARD_MAP_POSITION)[number];
26+
27+
const MAP_FILTER: Record<CardMapPosition, string> = {
28+
default: 'linear-gradient(to bottom, transparent 0%, black 15%, black 90%, transparent 100%)',
29+
top: 'linear-gradient(to bottom, black 90%, transparent 100%)',
30+
bottom: 'linear-gradient(to bottom, transparent 0%, black 10%)',
31+
single: 'linear-gradient(to bottom, transparent 0%, black 0%, black 100%, transparent 100%)',
32+
};
33+
34+
const DEBUG_NO_FETCH = process.env.DEBUG === 'true' && true;
35+
const DEV_ADDRESS = {
36+
streetName: '123 Example St',
37+
sublocality: 'Example Suburb',
38+
city: 'Example City',
39+
};
40+
1741
@customElement(COMPONENT.MINI_MAP)
1842
export class MiniMapBox extends BaseElement {
1943
constructor() {
2044
super(SECTION.MINI_MAP);
2145
}
2246

23-
@property({ attribute: false }) mapConfig!: MiniMapConfig;
47+
@property({ attribute: false }) public mapConfig!: MiniMapConfig;
2448
@property({ attribute: 'is-dark', type: Boolean, reflect: true })
2549
isDark!: boolean;
50+
@property({ type: String, reflect: true, attribute: 'map-position' }) public mapPosition: CardMapPosition = 'default';
2651
@state() private mapData?: MapData;
2752

2853
@state() private map: L.Map | null = null;
@@ -144,9 +169,20 @@ export class MiniMapBox extends BaseElement {
144169
return true;
145170
}
146171

172+
protected firstUpdated(): void {
173+
this.mapPosition = this._computeMapPosition();
174+
}
175+
147176
private async _getAddress(): Promise<void> {
177+
// Avoid fetching address in debug mode
178+
if (DEBUG_NO_FETCH) {
179+
this._addressReady = true;
180+
this._address = DEV_ADDRESS;
181+
return;
182+
}
183+
148184
const { lat, lon } = this.mapData!;
149-
// console.log('Getting adress...');
185+
debuglog('Getting adress...');
150186
const address = await _getMapAddress(this.mapConfig, lat, lon);
151187
if (address) {
152188
this._address = address;
@@ -237,9 +273,13 @@ export class MiniMapBox extends BaseElement {
237273
}
238274

239275
protected render(): TemplateResult {
240-
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
276+
const mapStyle = {
277+
'--vic-map-mask-image': MAP_FILTER[this.mapPosition] || MAP_FILTER['default'],
278+
height: `${this.mapConfig?.map_height || 150}px`,
279+
};
280+
241281
return html`
242-
<div class="map-wrapper" ?safari=${isSafari} style=${`height: ${this.mapConfig?.map_height || 150}px;`}>
282+
<div class="map-wrapper" ?safari=${isSafari} style=${styleMap(mapStyle)}>
243283
<div id="overlay-container">
244284
<div class="reset-button" @click=${this.resetMap} .hidden=${this._locateIconVisible}>
245285
<ha-icon icon="mdi:compass"></ha-icon>
@@ -294,6 +334,24 @@ export class MiniMapBox extends BaseElement {
294334
showHaMapDialog(this, params);
295335
}
296336

337+
private _computeMapPosition = (): CardMapPosition => {
338+
const mapSec = this.parentNode as HTMLElement | null;
339+
if (!mapSec) return 'default';
340+
const { previousElementSibling, nextElementSibling } = mapSec;
341+
342+
let position: CardMapPosition = 'default';
343+
if (!previousElementSibling && !nextElementSibling) {
344+
position = 'single';
345+
} else if (!previousElementSibling && nextElementSibling) {
346+
position = 'top';
347+
} else if (previousElementSibling && !nextElementSibling) {
348+
position = 'bottom';
349+
} else {
350+
position = 'default';
351+
}
352+
return position;
353+
};
354+
297355
static get styles(): CSSResultGroup {
298356
return [
299357
super.styles,

src/types/declarations.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ declare module '*.css';
33
declare module '*.png';
44
declare module '*.jpg';
55
declare module 'tinycolor2';
6-
declare module 'debug-es/src/browser.js';
6+
declare module 'debug/src/browser.js';
7+
declare module 'object-hash';
8+
declare module 'object-hash/dist/object_hash.js';

src/utils/debuglog.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Debug from 'debug/src/browser.js';
2+
3+
const DEBUG_NAME = 'vsc';
4+
export const debug = Debug(DEBUG_NAME);

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export * from './editor/process-config-entities';
1111
export * from './loader_extra_map';
1212
export * from './mdi-icons';
1313
export * from './lovelace/create-custom-card';
14-
1514
export * from './base-button';
15+
export * from './debuglog';

0 commit comments

Comments
 (0)