@using Iceshrimp.Assets.PhosphorIcons @using Iceshrimp.Shared.Schemas.Web @inject IJSRuntime Js @if (Focused < _refs.Count - 1) { } @if (Focused > 0) { } @foreach (var element in Attachments) { @if (!string.IsNullOrWhiteSpace(element.AltText)) { @element.AltText } } @code { [Parameter] [EditorRequired] public required IList Attachments { get; set; } private ElementReference Dialog { get; set; } private IJSObjectReference Module { get; set; } = null!; private int Focused { get; set; } private List _refs = []; public ElementReference Ref { set => _refs.Add(value); } private async Task OnKeyDown(KeyboardEventArgs e) { switch (e.Key) { case "ArrowRight": await Next(); break; case "ArrowLeft": await Prev(); break; } } private ElementReference Scroller { get; set; } private int ScrollWidth { get; set; } private int ScrollLeft { get; set; } protected override void OnParametersSet() { Focused = 0; } private async Task Scroll() { // This should probably be moved to floating point. // So the state transition happens sooner, i.e. at 2.5 vs 3? ScrollLeft = await Module.InvokeAsync("getScrollLeft", Scroller); var fraction = ScrollWidth / _refs.Count; var section = ScrollLeft / fraction; Focused = section; } private async Task Next() { if (Focused >= _refs.Count -1) { return; } Focused += 1; await Module.InvokeVoidAsync("scrollTo", _refs[Focused]); } private async Task Prev() { if (Focused <= 0) { return; } Focused -= 1; await Module.InvokeVoidAsync("scrollTo", _refs[Focused]); } public async Task OpenDialog(NoteAttachment attachment) { await Module.InvokeVoidAsync("openDialog", Dialog); ScrollWidth = await Module.InvokeAsync("getScrollWidth", Scroller); var index = Attachments.IndexOf(attachment); await Module.InvokeVoidAsync("scrollTo", _refs[index]); } private async Task CloseDialog() { await Module.InvokeVoidAsync("closeDialog", Dialog); } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { Module = await Js.InvokeAsync("import", "./Components/AttachmentView.razor.js"); } } }