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

More simple lightbox setup for SDK and compatible with custom History #749

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Jun 8, 2022

More simple lightbox setup for SDK and compatible with custom History

Changes to go along with matrix-org/matrix-viewer#12

Split out from #653

Todo

  • Test this out in the real Hydrogen to make sure everything still works

room,
eventId,
};
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing all of the previous boilerplate to setup the lightbox with this composable function that external consumers can also use in the SDK.

This makes setting up the lightbox in your project that uses the SDK as simple as a setupLightboxNavigation(...) function call. See the usage in matrix-org/matrix-viewer#12

@@ -92,6 +96,6 @@ export class LightboxViewModel extends ViewModel {
}

close() {
this.platform.history.pushUrl(this.closeUrl);
this.history.pushUrl(this.closeUrl);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're using a custom History implementation in matrix-org/matrix-viewer#12 to get hashes relative to the room, we need to use that custom History over the default one from platform.

The only other place we use platform.history is in src/platform/web/main.js#L38

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to inject your custom history through platform.history.


get history(): History {
return this._options.history;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #749 (comment) for why we're doing this now.

}));
this._loadEvent(room, eventObservable.get());
let event = this._eventEntry;
if (!this._eventEntry) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the option of passing in eventEntry directly since we have access to all of the events already in matrix-public-archive and we don't need or want to load more events.

Previously, this only allowed passing eventId which loaded in the full event.

return document.location.search;
}
return document.location.hash;
return document?.location?.hash;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to allow this function to run when server-side rendered in linkedom. It's ok for this function to return undefined. See shared/lib/archive-history.js for how this is integrated.

If this isn't desired, I can always wrap super.get() in my custom History instance and default that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better if you add your custom logic in your custom instance. It's going to be hard to not have regressions for some arcane DOM impl we never test for.

@MadLittleMods MadLittleMods changed the title Draft: More simple lightbox for SDK and compatible with custom History More simple lightbox for SDK and compatible with custom History Jun 8, 2022
@MadLittleMods MadLittleMods marked this pull request as ready for review June 8, 2022 04:55
MadLittleMods added a commit to matrix-org/matrix-viewer that referenced this pull request Jun 8, 2022
Add image lightbox support and Hydrogen URL hashes relative to the room

Related to element-hq/hydrogen-web#677

Requires the changes from element-hq/hydrogen-web#749 (split out from element-hq/hydrogen-web#653)

![](https://user-images.githubusercontent.com/558581/172526457-38c108e8-8c46-4e0c-9979-734348ec67fc.gif)


### Hydrogen routing relative to the room (remove session and room from the URL hash)

Before:
Page URL: doesn't work
```html
<div class="Timeline_messageBody">
   <div class="media" style="max-width: 400px">
      <div class="spacer" style="padding-top: 48.75%;"></div>
      <a href="undefined">
          <img src="http://192.168.1.182:8008//_matrix/media/r0/thumbnail/my.synapse.server/RxfuMxEgYcXHKYWERkKVUkqO?width=400&amp;height=195&amp;method=scale" alt="Friction_between_surfaces.jpeg" title="Friction_between_surfaces.jpeg" style="max-width: 400px; max-height: 195px;">
      </a>
      <time>2/24 6:20 PM</time>
   </div>
   <!--node binding placeholder-->
</div>
```

Before (not relative):
Page URL: `http://localhost:3050/!HBehERstyQBxyJDLfR:my.synapse.server/date/2022/02/24#/session/123/room/!HBehERstyQBxyJDLfR:my.synapse.server/lightbox/$17cgP6YBP9ny9xuU1vBmpOYFhRG4zpOe9SOgWi2Wxsk`
```html
<div class="Timeline_messageBody">
   <div class="media" style="max-width: 400px">
      <div class="spacer" style="padding-top: 48.75%;"></div>
      <a href="#/session/123/room/!HBehERstyQBxyJDLfR:my.synapse.server/lightbox/$17cgP6YBP9ny9xuU1vBmpOYFhRG4zpOe9SOgWi2Wxsk">
          <img src="http://192.168.1.182:8008//_matrix/media/r0/thumbnail/my.synapse.server/RxfuMxEgYcXHKYWERkKVUkqO?width=400&amp;height=195&amp;method=scale" alt="Friction_between_surfaces.jpeg" title="Friction_between_surfaces.jpeg" style="max-width: 400px; max-height: 195px;">
      </a>
      <time>2/24 6:20 PM</time>
   </div>
   <!--node binding placeholder-->
</div>
```

After (nice relative links):
Page URL: `http://localhost:3050/!HBehERstyQBxyJDLfR:my.synapse.server/date/2022/02/24#/lightbox/$17cgP6YBP9ny9xuU1vBmpOYFhRG4zpOe9SOgWi2Wxsk`
```html
<div class="Timeline_messageBody">
   <div class="media" style="max-width: 400px">
      <div class="spacer" style="padding-top: 48.75%;"></div>
      <a href="#/lightbox/$17cgP6YBP9ny9xuU1vBmpOYFhRG4zpOe9SOgWi2Wxsk">
          <img src="http://192.168.1.182:8008//_matrix/media/r0/thumbnail/my.synapse.server/RxfuMxEgYcXHKYWERkKVUkqO?width=400&amp;height=195&amp;method=scale" alt="Friction_between_surfaces.jpeg" title="Friction_between_surfaces.jpeg" style="max-width: 400px; max-height: 195px;">
      </a>
      <time>2/24 6:20 PM</time>
   </div>
   <!--node binding placeholder-->
</div>
```
@MadLittleMods MadLittleMods changed the title More simple lightbox for SDK and compatible with custom History More simple lightbox setup for SDK and compatible with custom History Jun 8, 2022
@@ -92,6 +96,6 @@ export class LightboxViewModel extends ViewModel {
}

close() {
this.platform.history.pushUrl(this.closeUrl);
this.history.pushUrl(this.closeUrl);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to inject your custom history through platform.history.

src/domain/session/room/LightboxViewModel.js Outdated Show resolved Hide resolved
return document.location.search;
}
return document.location.hash;
return document?.location?.hash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better if you add your custom logic in your custom instance. It's going to be hard to not have regressions for some arcane DOM impl we never test for.

* `lightboxChildOptions.eventEntry` or `lightboxChildOptions.eventId` are
* provided.
*/
function updateLightboxViewModel(vm, fieldName, lightboxChildOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I think this kind of dynamic mixin programming makes the code as a whole a lot less readable. Also, this will prevent us to make the track and disposeTracked method protected once we're fully converted to typescript.

I can see a case for having a generic helper method on ViewModel to setup a child view model based on a navigation observable, but I'd do it differently, something like this:

class ViewModel {
	bindChildToNavigationSegment(segmentName, setter, getter, vmMapper, publicPropName) {
		const update = value => {
			if (getter()) {
	            setter(this.disposeTracked(getter()));
	        }
	        if (value) {
	            setter(this.track(vmMapper(value)));
	        }
	        this.emitChange(publicPropName);
		}

		const observable = this.navigation.observe(segmentName);
        this.track(observable.subscribe(update));
        update(observable.get());
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it make sense it my head, here would be the usage for lightbox (adapted to using object parameters since there is so many flying around here):

this.bindChildToNavigationSegment({
  segmentName: 'lightbox',
  setter: (newValue) => {
    this._lightboxViewModel = newValue;
  },
  getter: () = {
    return this._lightboxViewModel;
  },
  vmMapper: (eventId) = {
    return new LightboxViewModel(this.childOptions({eventId, room}));
  },
  publicPropName: 'lightboxViewModel'
});

bindChildToNavigationSegment does make a lot of sense to capture this abstraction because it's so common 👍

The usage seems like too much boilerplate for what a SDK consumer cares about though.


I think we could boil it down to:

this.bindChildToNavigationSegment({
  segmentName: 'lightbox',
  vmMapper: (eventId) = {
    return new LightboxViewModel(this.childOptions({eventId, room}));
  },
  publicPropName: 'lightboxViewModel'
});
class ViewModel {
  bindChildToNavigationSegment({ segmentName, vmMapper, publicPropName }) {
        // Store the `ViewModel` under a symbol so no one else can tamper with
        // it. This acts like a private field on the class since no one else has the
        // symbol to look it up.
      let viewModelSymbol = Symbol(publicPropName);

      // On the given `vm`, create a getter at `publicPropName` that the
        // `ViewModel` is exposed at for usage in the view.
        Object.defineProperty(vm, publicPropName, {
            get: function() {
                return vm[viewModelSymbol];
            }
        });
        
    const update = value => {
      if (vm[lightboxViewModelSymbol]) {
              vm[lightboxViewModelSymbol] = this.disposeTracked(getter());
          }
          if (value) {
              vm[lightboxViewModelSymbol] = this.track(vmMapper(value));
          }
          this.emitChange(publicPropName);
    }

    const observable = this.navigation.observe(segmentName);
        this.track(observable.subscribe(update));
        update(observable.get());
  }
}

If some use case needs getter/setter we could have those as extra options and store it for them by default as above ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bwindels Does the boiled down version look reasonable?

Better context

Co-authored-by: Bruno Windels <274386+bwindels@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants