Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map component draft work #661

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

prushforth
Copy link

initial commit of directory for gcds-map component

Dynamically render layers based on passed props

Add gcds-map-layer, update gcds-map to use it. Update index.html with example map. Need to figure out how to get a storybook together.

Update gcds-map.stories.tsx

Add dependency on @maps4html/mapml

Updates for gcds-map

Remove dep on npm @maps4html/mapml

Summary | Résumé

1-3 sentence description of the changed you're proposing, including a link to
a GitHub Issue # or Trello card if applicable.


Description en 1 à 3 phrases de la modification proposée, avec un lien vers le
problème (« issue ») GitHub ou la fiche Trello, le cas échéant.

Test instructions | Instructions pour tester la modification

Sequential steps (1., 2., 3., ...) that describe how to test this change. This
will help a developer test things out without too much detective work. Also,
include any environmental setup steps that aren't in the normal README steps
and/or any time-based elements that this requires.


Étapes consécutives (1., 2., 3., …) qui décrivent la façon de tester la
modification. Elles aideront les développeurs à faire des tests sans avoir à
jouer au détective. Veuillez aussi inclure toutes les étapes de configuration
de l’environnement qui ne font pas partie des étapes normales dans le fichier
README et tout élément temporel requis.

@ethanWallace
Copy link
Collaborator

Hey @prushforth. Thanks for the PR to add more context to the email.

I took a look at the code this afternoon and there seems to be some other issues with the current code that are not mentioned. It appears with the current setup all the gcds packages won't build, mainly the @cdssnc/gcds-components-react-ssr package, so we will have to find an alternative for importing the needed files.

I made some modifications to the gcds-map.tsx file to try something different. Now I don't know if this is the best way forward, that is something we can discuss all together, but it will hopefully let you continue with setting up storybook. See code below:

import { Component, Element, h, Prop, Host } from '@stencil/core';
// import 'https://cdn.jsdelivr.net/npm/@maps4html/mapml/dist/mapml-viewer.js';  // Import mapml-viewer

@Component({
  tag: 'gcds-map',
  styleUrl: 'gcds-map.css',
  shadow: true,
})
export class GcdsMap {
  @Element() el: HTMLElement;

  // <mapml-viewer> attributes
  @Prop() lat: number;
  @Prop() lon: number;
  @Prop() zoom: number;
  @Prop() projection: string = 'OSMTILE'; // Default projection
  @Prop() controls: boolean = true;
  @Prop() controlslist: string;

  // Add width and height props to allow setting dimensions at design time
  @Prop() width: string = '100%'; // Allow setting width of the map
  @Prop() height: string = '400px'; // Allow setting height of the map

  componentDidLoad() {
    // Apply width and height as CSS variables
    this.el.style.setProperty('--map-width', this.width);
    this.el.style.setProperty('--map-height', this.height);

    // Handle <layer-> readiness once the map is rendered
    this.handleLayerReady();
  }

  handleLayerReady() {
    // Wait for the 'layer-' custom element to be defined
    customElements.whenDefined('layer-').then(() => {
      // Find all <layer-> elements inside the mapml-viewer
      const layers = Array.from(this.el.shadowRoot.querySelectorAll('layer-'));

      layers.forEach(layer => {
        // Now we know the <layer-> element is fully defined, call whenReady()
        (layer as any).whenReady().then(() => {
          // Check for <map-extent> in the layer's shadow DOM and add 'checked' attribute
          // this is necessary only for geogratis MapML resources, but harmless
          // otherwise (unless someone wanted to have an unchecked sublayer, for
          // some reason (it is possible, but maybe not useful).
          const mapExtent = layer.shadowRoot?.querySelector('map-extent');
          if (mapExtent && !mapExtent.hasAttribute('checked')) {
            mapExtent.setAttribute('checked', 'true');
          }
        });
      });
    });
  }

  render() {
    // Find the <gcds-map-layer> children inside the light DOM
    const layers = Array.from(this.el.querySelectorAll('gcds-map-layer'));

    return (
      <Host>
        <mapml-viewer
          lat={this.lat}
          lon={this.lon}
          zoom={this.zoom}
          projection={this.projection}
          controls={this.controls ? true : undefined}
          controlslist={this.controlslist} // Pass the controlslist to mapml-viewer
        >
          {layers.map(layer => (
            <layer-
              label={layer.getAttribute('label')}
              src={layer.getAttribute('src')}
              checked={
                layer.getAttribute('checked') === 'true' ? 'checked' : undefined
              }
              opacity={layer.getAttribute('opacity')} // Pass the opacity to the <layer-> element
            ></layer->
          ))}
        </mapml-viewer>
        <script  type="module"  src="https://cdn.jsdelivr.net/npm/@maps4html/mapml/dist/mapml-viewer.js"></script>
      </Host>
    );
  }
}

I mainly just added a Host element around the rendered code and made the js files import through a <script> tag. Again this might not be the best way to do it but we can talk about it later.

To get storybook, it now should be just trying to match how we have it setup with our components. Matching the properties on Meta and using Canvas over Story.

Hopefully this helps!

@prushforth
Copy link
Author

hi @ethanWallace and thank you I'm certain this will unblock me. I will take a closer look tomorrow!

@prushforth prushforth changed the title draft PR to solicit advice - see email Map component draft work Oct 9, 2024
@prushforth prushforth force-pushed the mapml-viewer-gcds-map branch from 802dd3a to b4af354 Compare October 10, 2024 14:29
@prushforth prushforth force-pushed the mapml-viewer-gcds-map branch from 385e1b9 to faae7b2 Compare November 20, 2024 22:10
Dynamically render layers based on passed props

Add gcds-map-layer, update gcds-map to use it.  Update index.html with
example map.  Need to figure out how to get a storybook together.

Update gcds-map.stories.tsx

Add dependency on @maps4html/mapml

Updates for gcds-map

Remove dep on npm @maps4html/mapml

Add <Host>, load mapml-viewer via script tag, per Ethan Wallace suggestion

Basic functional story, needs work
Sync package.json with upstream

Update packages/web/src/components/gcds-map/gcds-map.tsx

Use suggested integrity SHA for mapml.js temporarily until it can be included in dist

Co-authored-by: Pierre Dubois <[email protected]>

Generated packages ?

Update default story - overview.mdx is the focus for examples of
gcds-map attributes effects.  To do: add examples for gcds-map-layer
properties, probably by renaming properties.mdx and using that as the
menu item for "Map Layer"

Add zoomTo method definition to ts class definition

Update component render method to get mapml.js from ./dist/gcds/gcds-map
Still need to create that or equivalent location during build process
Update map to have controls by default

Rename layer- to map-layer

Add @maps4html/mapml as dependency. Add copy script, update stencil
config to invoke copy of mapml artifacts to dist output directory.

Get rid of comment in component that is rendering
Change default controls from false to true

Make mapml a dependency, not a devDependency, since it must be present
at runtime / installed with product

Revert <layer-> to <map-layer> refactoring, because that code has not
been released yet

Make copyMapMLFiles.js into a stencil.config.js plugin thingy, in line
with other imported plugins.

non-idempotency in copy mapml plugin

Replace unreliable custom stencil plugin with stencil copy option

Make move <script> creation into componentWillLoad lifecycle method,
test for it's previous existence.

Add ts-loader to devDependencies (maybe this is not correct, but I couldn't
get storybook to compile otherwise).

Add dist/gcds/gcds-map as static resource directory for storybook.

Add caption attribute to gcds-map

Rename title to caption. Update @watches to handle one Prop each.

Add caption to index.html gcds-map instance

Fix a bunch of compile-time errors
Update title -> caption in story

Change how controls is implemented by stencil to make it consistent
with boolean attribute with default of false when not present

Attempt to mitigate problems caused by story rendering component many
times with each keystroke

Make controlslist a multi-select in storybook

Update mapml to 0.16.0
@prushforth prushforth force-pushed the mapml-viewer-gcds-map branch from 726caf9 to e98301f Compare January 7, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants