| | | 1 | | @using Chronicis.Client.Components.Shared |
| | | 2 | | @using Chronicis.Client.Components.Characters |
| | | 3 | | @using Chronicis.Client.Utilities |
| | | 4 | | @using Chronicis.Shared.DTOs |
| | | 5 | | @using Chronicis.Shared.Enums |
| | | 6 | | @using Chronicis.Shared.Utilities |
| | | 7 | | @using Blazored.LocalStorage |
| | | 8 | | @using Microsoft.JSInterop |
| | | 9 | | @inject IArticleApiService ArticleApi |
| | | 10 | | @inject ILinkApiService LinkApiService |
| | | 11 | | @inject IExternalLinkApiService ExternalLinkApiService |
| | | 12 | | @inject IWikiLinkService WikiLinkService |
| | | 13 | | @inject IAISummaryApiService SummaryApi |
| | | 14 | | @inject IMarkdownService MarkdownService |
| | | 15 | | @inject ITreeStateService TreeState |
| | | 16 | | @inject IAppContextService AppContext |
| | | 17 | | @inject IBreadcrumbService BreadcrumbService |
| | | 18 | | @inject ISnackbar Snackbar |
| | | 19 | | @inject IJSRuntime JSRuntime |
| | | 20 | | @inject NavigationManager Navigation |
| | | 21 | | @inject ILocalStorageService LocalStorage |
| | | 22 | | @inject ILogger<ArticleDetail> Logger |
| | | 23 | | @inject IArticleCacheService ArticleCache |
| | | 24 | | @inject IMetadataDrawerService MetadataDrawerService |
| | | 25 | | @inject IKeyboardShortcutService KeyboardShortcutService |
| | | 26 | | @inject IWorldApiService WorldApi |
| | | 27 | | |
| | | 28 | | @implements IDisposable |
| | | 29 | | |
| | 0 | 30 | | @if (_isLoading) |
| | | 31 | | { |
| | | 32 | | <LoadingSkeleton /> |
| | | 33 | | } |
| | 0 | 34 | | else if (_article == null) |
| | | 35 | | { |
| | | 36 | | <EmptyState |
| | | 37 | | Icon="📝" |
| | | 38 | | Title="No Article Selected" |
| | | 39 | | Message="Select an article from the tree to view and edit its content." |
| | | 40 | | ActionText="Create New Article" |
| | | 41 | | OnActionClick="CreateRootArticle" /> |
| | | 42 | | } |
| | | 43 | | else |
| | | 44 | | { |
| | | 45 | | <style> |
| | | 46 | | .mud-input.mud-input-underline:before, |
| | | 47 | | .mud-input.mud-input-underline:after, |
| | | 48 | | .mud-input.mud-input-underline:hover:not(.mud-disabled):before, |
| | | 49 | | .mud-input.mud-input-underline:hover:not(.mud-disabled):after { |
| | | 50 | | border-bottom: none !important; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | .mud-input, |
| | | 54 | | .chronicis-editor-content { |
| | | 55 | | border: solid 1px #c7c7c4; |
| | | 56 | | border-radius: 4px; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | .chronicis-article-title .mud-input { |
| | | 60 | | padding-left:8px; |
| | | 61 | | } |
| | | 62 | | </style> |
| | | 63 | | |
| | | 64 | | <MudPaper Elevation="2" Class="chronicis-article-card chronicis-fade-in"> |
| | | 65 | | |
| | | 66 | | <!-- Article Header (Breadcrumbs, Icon, Title, Divider) --> |
| | | 67 | | <ArticleHeader Breadcrumbs="_breadcrumbs" |
| | | 68 | | @bind-Title="_editTitle" |
| | | 69 | | IconEmoji="@_article.IconEmoji" |
| | | 70 | | OnIconChanged="HandleIconChanged" |
| | | 71 | | OnTitleEdited="HandleTitleEdited" |
| | | 72 | | OnEnterPressed="SaveArticle" |
| | | 73 | | OnMetadataToggle="ToggleMetadata" |
| | | 74 | | ShouldFocusTitle="_shouldFocusTitle" |
| | | 75 | | ShouldFocusTitleChanged="HandleFocusTitleChanged" /> |
| | | 76 | | |
| | | 77 | | @* Character Claim Button - only show for Character type articles *@ |
| | 0 | 78 | | @if (_article.Type == ArticleType.Character) |
| | | 79 | | { |
| | | 80 | | <div class="mb-3"> |
| | | 81 | | <CharacterClaimButton CharacterId="@_article.Id" /> |
| | | 82 | | </div> |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | <!-- Editor Toolbar --> |
| | | 86 | | <div class="chronicis-editor-toolbar mb-2"> |
| | | 87 | | <MudTooltip Text="Insert Image" Placement="Placement.Top"> |
| | | 88 | | <MudIconButton Icon="@Icons.Material.Filled.Image" |
| | | 89 | | Size="Size.Small" |
| | | 90 | | Color="Color.Default" |
| | | 91 | | OnClick="InsertImageFromToolbar" |
| | | 92 | | Disabled="!_editorInitialized" /> |
| | | 93 | | </MudTooltip> |
| | | 94 | | </div> |
| | | 95 | | |
| | | 96 | | <!-- TipTap Editor Container --> |
| | | 97 | | <div id="tiptap-editor-@_article.Id" class="chronicis-editor-container mb-4"></div> |
| | | 98 | | |
| | | 99 | | <!-- AI Summary Section --> |
| | | 100 | | <AISummarySection EntityId="@_article.Id" |
| | | 101 | | EntityType="Article" |
| | | 102 | | IsExpanded="@_isSummaryExpanded" |
| | 0 | 103 | | IsExpandedChanged="@((expanded) => _isSummaryExpanded = expanded)" /> |
| | | 104 | | |
| | | 105 | | <!-- Action Bar (Save Status + Buttons) --> |
| | | 106 | | <ArticleActionBar IsSaving="_isSaving" |
| | | 107 | | IsAutoLinking="_isAutoLinking" |
| | | 108 | | IsCreatingChild="_isCreatingChild" |
| | | 109 | | HasUnsavedChanges="_hasUnsavedChanges" |
| | | 110 | | LastSaveTime="@_lastSaveTime" |
| | | 111 | | OnSave="SaveArticle" |
| | | 112 | | OnDelete="DeleteArticle" |
| | | 113 | | OnAutoLink="AutoLinkArticle" |
| | | 114 | | OnCreateChild="CreateChildArticle" /> |
| | | 115 | | </MudPaper> |
| | | 116 | | |
| | | 117 | | <!-- Metadata Drawer --> |
| | 0 | 118 | | <ArticleMetadataDrawer @ref="_metadataDrawer" |
| | 0 | 119 | | Article="_article" |
| | 0 | 120 | | @bind-IsOpen="_openMetadata" /> |
| | 0 | 121 | | |
| | 0 | 122 | | <!-- External Link Preview Drawer --> |
| | 0 | 123 | | <MudDrawer @bind-Open="_externalPreviewOpen" |
| | | 124 | | Anchor="Anchor.End" |
| | | 125 | | Variant="@DrawerVariant.Temporary" |
| | | 126 | | Elevation="9999" |
| | | 127 | | Class="external-link-preview-drawer"> |
| | | 128 | | <MudDrawerHeader> |
| | | 129 | | <MudSpacer /> |
| | | 130 | | <MudIconButton Icon="@Icons.Material.Filled.Close" OnClick="CloseExternalPreview" /> |
| | | 131 | | |
| | | 132 | | </MudDrawerHeader> |
| | | 133 | | <MudDrawerContainer> |
| | | 134 | | <div class="external-link-preview-header"> |
| | 0 | 135 | | @if (!string.IsNullOrWhiteSpace(_externalPreviewSource)) |
| | | 136 | | { |
| | 0 | 137 | | <span class="external-link-preview-source">@_externalPreviewSource.ToUpperInvariant()</span> |
| | | 138 | | } |
| | 0 | 139 | | <MudText Typo="Typo.h6">@(_externalPreviewTitle ?? "External Link")</MudText> |
| | | 140 | | </div> |
| | 0 | 141 | | @if (!string.IsNullOrWhiteSpace(_externalPreviewContent?.Kind)) |
| | | 142 | | { |
| | | 143 | | <MudText Typo="Typo.caption" Style="color: var(--mud-palette-text-secondary);"> |
| | 0 | 144 | | @_externalPreviewContent.Kind |
| | | 145 | | </MudText> |
| | | 146 | | } |
| | 0 | 147 | | @if (_externalPreviewLoading) |
| | | 148 | | { |
| | | 149 | | <div class="d-flex justify-center align-center" style="padding: 24px;"> |
| | | 150 | | <MudProgressCircular Indeterminate="true" /> |
| | | 151 | | </div> |
| | | 152 | | } |
| | 0 | 153 | | else if (!string.IsNullOrWhiteSpace(_externalPreviewError)) |
| | | 154 | | { |
| | 0 | 155 | | <MudAlert Severity="Severity.Error">@_externalPreviewError</MudAlert> |
| | | 156 | | } |
| | 0 | 157 | | else if (_externalPreviewContent != null) |
| | | 158 | | { |
| | | 159 | | <ExternalLinkDetailPanel Content="_externalPreviewContent" /> |
| | | 160 | | } |
| | | 161 | | </MudDrawerContainer> |
| | | 162 | | </MudDrawer> |
| | | 163 | | |
| | | 164 | | <!-- Wiki Link Autocomplete --> |
| | 0 | 165 | | @if (_showAutocomplete && _article != null) |
| | | 166 | | { |
| | | 167 | | <ArticleDetailWikiLinkAutocomplete Suggestions="@_autocompleteSuggestions" |
| | | 168 | | Loading="@_autocompleteLoading" |
| | | 169 | | SelectedIndex="@_autocompleteSelectedIndex" |
| | | 170 | | SelectedIndexChanged="OnAutocompleteIndexChanged" |
| | | 171 | | OnSelect="OnAutocompleteSelect" |
| | | 172 | | OnCreate="OnAutocompleteCreate" |
| | | 173 | | Position="@_autocompletePosition" |
| | | 174 | | Query="@_autocompleteQuery" |
| | | 175 | | IsExternalQuery="@_autocompleteIsExternalQuery" /> |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | @code { |
| | | 180 | | // Article state |
| | | 181 | | private ArticleDto? _article; |
| | | 182 | | private List<BreadcrumbItem>? _breadcrumbs; |
| | 0 | 183 | | private string _editTitle = string.Empty; |
| | 0 | 184 | | private string _editBody = string.Empty; |
| | | 185 | | |
| | | 186 | | // UI state |
| | | 187 | | private bool _isLoading = false; |
| | | 188 | | private bool _isSaving = false; |
| | | 189 | | private bool _isAutoLinking = false; |
| | | 190 | | private bool _isCreatingChild = false; |
| | | 191 | | private bool _hasUnsavedChanges = false; |
| | | 192 | | private bool _editorInitialized = false; |
| | 0 | 193 | | private string _lastSaveTime = "just now"; |
| | | 194 | | private bool _openMetadata = false; |
| | | 195 | | private bool _isSummaryExpanded = false; |
| | | 196 | | private bool _shouldFocusTitle = false; |
| | | 197 | | |
| | | 198 | | // Timer and references |
| | | 199 | | private Timer? _autoSaveTimer; |
| | | 200 | | private DotNetObjectReference<ArticleDetail>? _dotNetHelper; |
| | | 201 | | private ArticleMetadataDrawer? _metadataDrawer; |
| | | 202 | | private bool _disposed = false; |
| | | 203 | | |
| | | 204 | | // Autocomplete state |
| | | 205 | | private bool _showAutocomplete = false; |
| | | 206 | | private bool _autocompleteLoading = false; |
| | 0 | 207 | | private List<WikiLinkAutocompleteItem> _autocompleteSuggestions = new(); |
| | | 208 | | private int _autocompleteSelectedIndex = 0; |
| | 0 | 209 | | private (double X, double Y) _autocompletePosition = (0, 0); |
| | 0 | 210 | | private string _autocompleteQuery = string.Empty; |
| | | 211 | | private bool _autocompleteIsExternalQuery = false; |
| | | 212 | | private string? _autocompleteExternalSourceKey; |
| | | 213 | | |
| | | 214 | | // External link preview |
| | | 215 | | private bool _externalPreviewOpen = false; |
| | | 216 | | private bool _externalPreviewLoading = false; |
| | | 217 | | private string? _externalPreviewError; |
| | | 218 | | private ExternalLinkContentDto? _externalPreviewContent; |
| | | 219 | | private string? _externalPreviewSource; |
| | | 220 | | private string? _externalPreviewId; |
| | | 221 | | private string? _externalPreviewTitle; |
| | 0 | 222 | | private readonly Dictionary<string, ExternalLinkContentDto> _externalLinkCache = new(StringComparer.OrdinalIgnoreCas |
| | | 223 | | |
| | | 224 | | private const string LastArticlePathKey = "chronicis_last_article_path"; |
| | | 225 | | |
| | | 226 | | #region Lifecycle |
| | | 227 | | |
| | | 228 | | protected override async Task OnInitializedAsync() |
| | | 229 | | { |
| | 0 | 230 | | TreeState.OnStateChanged += OnTreeStateChanged; |
| | 0 | 231 | | MetadataDrawerService.OnToggle += OnMetadataDrawerToggle; |
| | 0 | 232 | | KeyboardShortcutService.OnSaveRequested += HandleSaveShortcut; |
| | 0 | 233 | | _dotNetHelper = DotNetObjectReference.Create(this); |
| | 0 | 234 | | await Task.CompletedTask; |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | protected override void OnInitialized() |
| | | 238 | | { |
| | 0 | 239 | | OnCtrlNPressed += HandleCtrlNInstance; |
| | 0 | 240 | | base.OnInitialized(); |
| | 0 | 241 | | } |
| | | 242 | | |
| | | 243 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | | 244 | | { |
| | 0 | 245 | | if (firstRender && TreeState.SelectedArticleId.HasValue) |
| | | 246 | | { |
| | 0 | 247 | | await LoadArticleAsync(TreeState.SelectedArticleId.Value); |
| | 0 | 248 | | StateHasChanged(); |
| | 0 | 249 | | await Task.Delay(100); |
| | 0 | 250 | | await InitializeEditor(); |
| | | 251 | | } |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | public void Dispose() |
| | | 255 | | { |
| | 0 | 256 | | _disposed = true; |
| | 0 | 257 | | TreeState.OnStateChanged -= OnTreeStateChanged; |
| | 0 | 258 | | MetadataDrawerService.OnToggle -= OnMetadataDrawerToggle; |
| | 0 | 259 | | KeyboardShortcutService.OnSaveRequested -= HandleSaveShortcut; |
| | 0 | 260 | | OnCtrlNPressed -= HandleCtrlNInstance; |
| | 0 | 261 | | _autoSaveTimer?.Dispose(); |
| | | 262 | | |
| | | 263 | | // Clear and dispose the DotNetObjectReference |
| | 0 | 264 | | var helper = _dotNetHelper; |
| | 0 | 265 | | _dotNetHelper = null; |
| | 0 | 266 | | helper?.Dispose(); |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | #endregion |
| | | 270 | | |
| | | 271 | | #region Header Event Handlers |
| | | 272 | | |
| | 0 | 273 | | private void HandleTitleEdited() => _hasUnsavedChanges = true; |
| | | 274 | | |
| | | 275 | | private async Task HandleIconChanged(string? newIcon) |
| | | 276 | | { |
| | 0 | 277 | | if (_article == null || _isSaving) return; |
| | | 278 | | |
| | 0 | 279 | | _article.IconEmoji = newIcon; |
| | 0 | 280 | | _isSaving = true; |
| | 0 | 281 | | StateHasChanged(); |
| | | 282 | | |
| | | 283 | | try |
| | | 284 | | { |
| | 0 | 285 | | var updateDto = new ArticleUpdateDto |
| | 0 | 286 | | { |
| | 0 | 287 | | Title = _editTitle?.Trim() ?? string.Empty, |
| | 0 | 288 | | Body = _editBody, |
| | 0 | 289 | | EffectiveDate = _article.EffectiveDate, |
| | 0 | 290 | | IconEmoji = newIcon |
| | 0 | 291 | | }; |
| | | 292 | | |
| | 0 | 293 | | await ArticleApi.UpdateArticleAsync(_article.Id, updateDto); |
| | 0 | 294 | | _article.ModifiedAt = DateTime.Now; |
| | 0 | 295 | | _lastSaveTime = "just now"; |
| | | 296 | | |
| | 0 | 297 | | await TreeState.RefreshAsync(); |
| | 0 | 298 | | TreeState.UpdateNodeDisplay(_article.Id, _editTitle ?? string.Empty, newIcon); |
| | | 299 | | |
| | 0 | 300 | | Snackbar.Add($"Icon {(string.IsNullOrEmpty(newIcon) ? "removed" : "updated")}", Severity.Success); |
| | 0 | 301 | | } |
| | 0 | 302 | | catch (Exception ex) |
| | | 303 | | { |
| | 0 | 304 | | Snackbar.Add($"Failed to save icon: {ex.Message}", Severity.Error); |
| | 0 | 305 | | } |
| | | 306 | | finally |
| | | 307 | | { |
| | 0 | 308 | | _isSaving = false; |
| | 0 | 309 | | StateHasChanged(); |
| | | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | private void HandleFocusTitleChanged(bool value) => _shouldFocusTitle = value; |
| | | 314 | | |
| | 0 | 315 | | private void ToggleMetadata() => _openMetadata = !_openMetadata; |
| | | 316 | | |
| | | 317 | | #endregion |
| | | 318 | | |
| | | 319 | | #region Article Loading |
| | | 320 | | |
| | | 321 | | private async void OnTreeStateChanged() |
| | | 322 | | { |
| | 0 | 323 | | if (_disposed) return; |
| | | 324 | | |
| | 0 | 325 | | await InvokeAsync(async () => |
| | 0 | 326 | | { |
| | 0 | 327 | | if (_disposed) return; |
| | 0 | 328 | | |
| | 0 | 329 | | try |
| | 0 | 330 | | { |
| | 0 | 331 | | if (_editorInitialized && _article != null) |
| | 0 | 332 | | { |
| | 0 | 333 | | await JSRuntime.InvokeVoidAsync("destroyTipTapEditor", $"tiptap-editor-{_article.Id}"); |
| | 0 | 334 | | } |
| | 0 | 335 | | |
| | 0 | 336 | | _editorInitialized = false; |
| | 0 | 337 | | |
| | 0 | 338 | | if (_disposed) return; |
| | 0 | 339 | | |
| | 0 | 340 | | if (TreeState.SelectedArticleId.HasValue && TreeState.SelectedArticleId.Value != Guid.Empty) |
| | 0 | 341 | | { |
| | 0 | 342 | | await LoadArticleAsync(TreeState.SelectedArticleId.Value); |
| | 0 | 343 | | } |
| | 0 | 344 | | else |
| | 0 | 345 | | { |
| | 0 | 346 | | _article = null; |
| | 0 | 347 | | if (!_disposed) |
| | 0 | 348 | | { |
| | 0 | 349 | | await JSRuntime.InvokeVoidAsync("eval", "document.title = 'Chronicis'"); |
| | 0 | 350 | | } |
| | 0 | 351 | | } |
| | 0 | 352 | | |
| | 0 | 353 | | if (_disposed) return; |
| | 0 | 354 | | StateHasChanged(); |
| | 0 | 355 | | |
| | 0 | 356 | | if (_article != null && !_disposed) |
| | 0 | 357 | | { |
| | 0 | 358 | | await Task.Delay(100); |
| | 0 | 359 | | if (!_disposed) |
| | 0 | 360 | | { |
| | 0 | 361 | | await InitializeEditor(); |
| | 0 | 362 | | } |
| | 0 | 363 | | } |
| | 0 | 364 | | } |
| | 0 | 365 | | catch (ObjectDisposedException) |
| | 0 | 366 | | { |
| | 0 | 367 | | // Component was disposed during async operation - this is expected during navigation |
| | 0 | 368 | | } |
| | 0 | 369 | | catch (JSDisconnectedException) |
| | 0 | 370 | | { |
| | 0 | 371 | | // JS runtime disconnected - this is expected during navigation |
| | 0 | 372 | | } |
| | 0 | 373 | | }); |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | private async Task LoadArticleAsync(Guid articleId) |
| | | 377 | | { |
| | 0 | 378 | | _isLoading = true; |
| | 0 | 379 | | _externalPreviewOpen = false; |
| | 0 | 380 | | StateHasChanged(); |
| | | 381 | | |
| | | 382 | | try |
| | | 383 | | { |
| | 0 | 384 | | _article = await ArticleApi.GetArticleAsync(articleId); |
| | 0 | 385 | | _editTitle = _article?.Title ?? string.Empty; |
| | 0 | 386 | | _editBody = _article?.Body ?? string.Empty; |
| | 0 | 387 | | _hasUnsavedChanges = false; |
| | | 388 | | |
| | 0 | 389 | | if (_article != null) |
| | | 390 | | { |
| | 0 | 391 | | ArticleCache.CacheArticle(_article); |
| | | 392 | | } |
| | | 393 | | |
| | 0 | 394 | | LoadBreadcrumbs(); |
| | 0 | 395 | | await UpdatePageTitle(); |
| | 0 | 396 | | await SaveLastArticlePath(); |
| | | 397 | | |
| | 0 | 398 | | if (string.IsNullOrEmpty(_editTitle) || TreeState.ShouldFocusTitle) |
| | | 399 | | { |
| | 0 | 400 | | _shouldFocusTitle = true; |
| | 0 | 401 | | TreeState.ShouldFocusTitle = false; |
| | 0 | 402 | | StateHasChanged(); |
| | | 403 | | } |
| | 0 | 404 | | } |
| | 0 | 405 | | catch (Exception ex) |
| | | 406 | | { |
| | 0 | 407 | | Snackbar.Add($"Failed to load article: {ex.Message}", Severity.Error); |
| | 0 | 408 | | } |
| | | 409 | | finally |
| | | 410 | | { |
| | 0 | 411 | | _isLoading = false; |
| | | 412 | | } |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | private void LoadBreadcrumbs() |
| | | 416 | | { |
| | 0 | 417 | | if (_article?.Breadcrumbs != null && _article.Breadcrumbs.Any()) |
| | | 418 | | { |
| | 0 | 419 | | _breadcrumbs = BreadcrumbService.ForArticle(_article.Breadcrumbs); |
| | | 420 | | } |
| | | 421 | | else |
| | | 422 | | { |
| | 0 | 423 | | _breadcrumbs = new List<BreadcrumbItem> { new("Dashboard", href: "/dashboard") }; |
| | | 424 | | } |
| | 0 | 425 | | } |
| | | 426 | | |
| | | 427 | | private async Task UpdatePageTitle() |
| | | 428 | | { |
| | 0 | 429 | | var pageTitle = string.IsNullOrEmpty(_article?.Title) ? "Untitled - Chronicis" : $"{_article.Title} - Chronicis" |
| | 0 | 430 | | await JSRuntime.InvokeVoidAsync("eval", $"document.title = '{JsUtilities.EscapeForJs(pageTitle)}'"); |
| | 0 | 431 | | } |
| | | 432 | | |
| | | 433 | | private async Task SaveLastArticlePath() |
| | | 434 | | { |
| | 0 | 435 | | if (_article?.Breadcrumbs != null && _article.Breadcrumbs.Any()) |
| | | 436 | | { |
| | 0 | 437 | | var path = BreadcrumbService.BuildArticleUrl(_article.Breadcrumbs); |
| | 0 | 438 | | await LocalStorage.SetItemAsStringAsync(LastArticlePathKey, path); |
| | | 439 | | } |
| | 0 | 440 | | } |
| | | 441 | | |
| | | 442 | | #endregion |
| | | 443 | | |
| | | 444 | | #region Editor |
| | | 445 | | |
| | | 446 | | private async Task InitializeEditor() |
| | | 447 | | { |
| | 0 | 448 | | if (_article != null && !_editorInitialized && !_disposed && _dotNetHelper != null) |
| | | 449 | | { |
| | | 450 | | try |
| | | 451 | | { |
| | 0 | 452 | | await JSRuntime.InvokeVoidAsync("initializeTipTapEditor", $"tiptap-editor-{_article.Id}", _editBody, _do |
| | | 453 | | |
| | 0 | 454 | | if (_disposed) return; |
| | | 455 | | |
| | 0 | 456 | | await JSRuntime.InvokeVoidAsync("initializeWikiLinkAutocomplete", $"tiptap-editor-{_article.Id}", _dotNe |
| | | 457 | | |
| | | 458 | | // Initialize image upload (drag-drop, paste) |
| | 0 | 459 | | await JSRuntime.InvokeVoidAsync("initializeImageUpload", $"tiptap-editor-{_article.Id}", _dotNetHelper); |
| | | 460 | | |
| | | 461 | | // Resolve any chronicis-image: references to real SAS URLs |
| | 0 | 462 | | await JSRuntime.InvokeVoidAsync("resolveEditorImages", $"tiptap-editor-{_article.Id}", _dotNetHelper); |
| | | 463 | | |
| | 0 | 464 | | _editorInitialized = true; |
| | 0 | 465 | | } |
| | 0 | 466 | | catch (ObjectDisposedException) |
| | | 467 | | { |
| | | 468 | | // Component was disposed during async operation - this is expected during navigation |
| | 0 | 469 | | } |
| | 0 | 470 | | catch (JSDisconnectedException) |
| | | 471 | | { |
| | | 472 | | // JS runtime disconnected - this is expected during navigation |
| | 0 | 473 | | } |
| | 0 | 474 | | catch (Exception ex) |
| | | 475 | | { |
| | 0 | 476 | | if (!_disposed) |
| | | 477 | | { |
| | 0 | 478 | | Snackbar.Add($"Failed to initialize editor: {ex.Message}", Severity.Warning); |
| | | 479 | | } |
| | 0 | 480 | | } |
| | | 481 | | } |
| | 0 | 482 | | } |
| | | 483 | | |
| | | 484 | | [JSInvokable] |
| | | 485 | | public void OnEditorUpdate(string markdown) |
| | | 486 | | { |
| | 0 | 487 | | _editBody = markdown; |
| | 0 | 488 | | _hasUnsavedChanges = true; |
| | 0 | 489 | | _autoSaveTimer?.Dispose(); |
| | 0 | 490 | | _autoSaveTimer = new Timer(async _ => await AutoSave(), null, 500, Timeout.Infinite); |
| | 0 | 491 | | } |
| | | 492 | | |
| | | 493 | | private async Task AutoSave() |
| | | 494 | | { |
| | 0 | 495 | | await InvokeAsync(async () => |
| | 0 | 496 | | { |
| | 0 | 497 | | if (_hasUnsavedChanges && !_isSaving) |
| | 0 | 498 | | await SaveArticle(); |
| | 0 | 499 | | }); |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | #endregion |
| | | 503 | | |
| | | 504 | | #region Save & Delete |
| | | 505 | | |
| | | 506 | | private async Task SaveArticle() |
| | | 507 | | { |
| | 0 | 508 | | if (_article == null || _isSaving) return; |
| | | 509 | | |
| | 0 | 510 | | _isSaving = true; |
| | 0 | 511 | | StateHasChanged(); |
| | | 512 | | |
| | | 513 | | try |
| | | 514 | | { |
| | 0 | 515 | | var originalTitle = _article.Title; |
| | 0 | 516 | | var newTitle = _editTitle?.Trim() ?? string.Empty; |
| | 0 | 517 | | var titleChanged = originalTitle != newTitle; |
| | 0 | 518 | | string? newSlug = null; |
| | | 519 | | |
| | 0 | 520 | | if (titleChanged) |
| | | 521 | | { |
| | 0 | 522 | | var suggestedSlug = SlugGenerator.GenerateSlug(newTitle); |
| | 0 | 523 | | if (suggestedSlug != _article.Slug) |
| | 0 | 524 | | newSlug = suggestedSlug; |
| | | 525 | | } |
| | | 526 | | |
| | 0 | 527 | | var updateDto = new ArticleUpdateDto |
| | 0 | 528 | | { |
| | 0 | 529 | | Title = newTitle, |
| | 0 | 530 | | Slug = newSlug, |
| | 0 | 531 | | Body = _editBody, |
| | 0 | 532 | | EffectiveDate = _article.EffectiveDate, |
| | 0 | 533 | | IconEmoji = _article.IconEmoji |
| | 0 | 534 | | }; |
| | | 535 | | |
| | 0 | 536 | | await ArticleApi.UpdateArticleAsync(_article.Id, updateDto); |
| | 0 | 537 | | ArticleCache.InvalidateCache(); |
| | | 538 | | |
| | 0 | 539 | | _article.Title = newTitle; |
| | 0 | 540 | | _article.Body = _editBody; |
| | 0 | 541 | | _article.ModifiedAt = DateTime.Now; |
| | 0 | 542 | | _hasUnsavedChanges = false; |
| | 0 | 543 | | _lastSaveTime = "just now"; |
| | | 544 | | |
| | 0 | 545 | | if (newSlug != null || titleChanged) |
| | | 546 | | { |
| | 0 | 547 | | await UpdatePageTitle(); |
| | 0 | 548 | | TreeState.UpdateNodeDisplay(_article.Id, newTitle, _article.IconEmoji); |
| | | 549 | | } |
| | | 550 | | |
| | 0 | 551 | | if (newSlug != null) |
| | | 552 | | { |
| | 0 | 553 | | TreeState.ExpandPathToAndSelect(_article.Id); |
| | 0 | 554 | | _article = await ArticleApi.GetArticleAsync(_article.Id); |
| | | 555 | | |
| | 0 | 556 | | if (_article?.Breadcrumbs != null && _article.Breadcrumbs.Any()) |
| | | 557 | | { |
| | 0 | 558 | | var path = BreadcrumbService.BuildArticleUrl(_article.Breadcrumbs); |
| | 0 | 559 | | Navigation.NavigateTo(path, replace: true); |
| | | 560 | | } |
| | | 561 | | } |
| | 0 | 562 | | } |
| | 0 | 563 | | catch (Exception ex) |
| | | 564 | | { |
| | 0 | 565 | | Snackbar.Add($"Failed to save: {ex.Message}", Severity.Error); |
| | 0 | 566 | | } |
| | | 567 | | finally |
| | | 568 | | { |
| | 0 | 569 | | _isSaving = false; |
| | 0 | 570 | | StateHasChanged(); |
| | | 571 | | // Only refresh panels if the metadata drawer is open |
| | 0 | 572 | | if (_metadataDrawer != null && _openMetadata) |
| | 0 | 573 | | await _metadataDrawer.RefreshPanelsAsync(); |
| | | 574 | | } |
| | 0 | 575 | | } |
| | | 576 | | |
| | | 577 | | private async Task DeleteArticle() |
| | | 578 | | { |
| | 0 | 579 | | if (_article == null) return; |
| | | 580 | | |
| | 0 | 581 | | var message = $"Are you sure you want to delete '{_article.Title}'?"; |
| | 0 | 582 | | if (_article.ChildCount > 0) |
| | | 583 | | { |
| | 0 | 584 | | var childText = _article.ChildCount == 1 ? "1 child article" : $"{_article.ChildCount} child articles"; |
| | 0 | 585 | | message = $"Are you sure you want to delete '{_article.Title}'?\n\n⚠️ WARNING: This will also delete {childT |
| | | 586 | | } |
| | 0 | 587 | | message += "\n\nThis action cannot be undone."; |
| | | 588 | | |
| | 0 | 589 | | if (!await JSRuntime.InvokeAsync<bool>("confirm", message)) return; |
| | | 590 | | |
| | | 591 | | try |
| | | 592 | | { |
| | 0 | 593 | | var success = await ArticleApi.DeleteArticleAsync(_article.Id); |
| | 0 | 594 | | if (success) |
| | | 595 | | { |
| | 0 | 596 | | ArticleCache.InvalidateCache(); |
| | 0 | 597 | | Snackbar.Add("Article deleted successfully", Severity.Success); |
| | 0 | 598 | | await TreeState.RefreshAsync(); |
| | 0 | 599 | | _article = null; |
| | | 600 | | } |
| | | 601 | | else |
| | | 602 | | { |
| | 0 | 603 | | Snackbar.Add("Failed to delete article. You may not have permission.", Severity.Error); |
| | | 604 | | } |
| | 0 | 605 | | } |
| | 0 | 606 | | catch (Exception ex) |
| | | 607 | | { |
| | 0 | 608 | | Snackbar.Add($"Failed to delete: {ex.Message}", Severity.Error); |
| | 0 | 609 | | } |
| | 0 | 610 | | } |
| | | 611 | | |
| | | 612 | | private async Task AutoLinkArticle() |
| | | 613 | | { |
| | 0 | 614 | | if (_article == null || _isAutoLinking) return; |
| | | 615 | | |
| | 0 | 616 | | _isAutoLinking = true; |
| | 0 | 617 | | StateHasChanged(); |
| | | 618 | | |
| | | 619 | | try |
| | | 620 | | { |
| | 0 | 621 | | var result = await LinkApiService.AutoLinkAsync(_article.Id, _editBody); |
| | | 622 | | |
| | 0 | 623 | | if (result == null) |
| | | 624 | | { |
| | 0 | 625 | | Snackbar.Add("Failed to scan for links", Severity.Error); |
| | 0 | 626 | | return; |
| | | 627 | | } |
| | | 628 | | |
| | 0 | 629 | | if (result.LinksFound == 0) |
| | | 630 | | { |
| | 0 | 631 | | Snackbar.Add("No linkable content found", Severity.Info); |
| | 0 | 632 | | return; |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | var matchList = string.Join("\n", result.Matches.Select(m => $"• \"{m.MatchedText}\" → {m.ArticleTitle}")); |
| | 0 | 636 | | if (!await JSRuntime.InvokeAsync<bool>("confirm", $"Found {result.LinksFound} potential link(s):\n\n{matchLi |
| | 0 | 637 | | return; |
| | | 638 | | |
| | | 639 | | // Convert matches to JS-friendly format and insert via TipTap |
| | | 640 | | // For alias matches, format as "Alias → Title", otherwise just use matched text |
| | 0 | 641 | | var jsMatches = result.Matches.Select(m => new |
| | 0 | 642 | | { |
| | 0 | 643 | | articleId = m.ArticleId.ToString(), |
| | 0 | 644 | | displayText = m.IsAliasMatch ? $"{m.MatchedText} → {m.ArticleTitle}" : m.MatchedText, |
| | 0 | 645 | | startIndex = m.StartIndex, |
| | 0 | 646 | | endIndex = m.EndIndex |
| | 0 | 647 | | }).ToArray(); |
| | | 648 | | |
| | 0 | 649 | | await JSRuntime.InvokeVoidAsync("insertWikiLinksAtPositions", $"tiptap-editor-{_article.Id}", jsMatches); |
| | | 650 | | |
| | | 651 | | // Get the updated content from the editor |
| | 0 | 652 | | _editBody = await JSRuntime.InvokeAsync<string>("getTipTapContent", $"tiptap-editor-{_article.Id}"); |
| | 0 | 653 | | _hasUnsavedChanges = true; |
| | 0 | 654 | | await SaveArticle(); |
| | 0 | 655 | | Snackbar.Add($"Added {result.LinksFound} link(s)", Severity.Success); |
| | 0 | 656 | | } |
| | 0 | 657 | | catch (Exception ex) |
| | | 658 | | { |
| | 0 | 659 | | Logger.LogError(ex, "Error auto-linking article {ArticleId}", _article.Id); |
| | 0 | 660 | | Snackbar.Add($"Failed to auto-link: {ex.Message}", Severity.Error); |
| | 0 | 661 | | } |
| | | 662 | | finally |
| | | 663 | | { |
| | 0 | 664 | | _isAutoLinking = false; |
| | 0 | 665 | | StateHasChanged(); |
| | | 666 | | } |
| | 0 | 667 | | } |
| | | 668 | | |
| | | 669 | | #endregion |
| | | 670 | | |
| | | 671 | | #region Article Creation |
| | | 672 | | |
| | | 673 | | private async Task CreateRootArticle() |
| | | 674 | | { |
| | 0 | 675 | | var worldId = AppContext.CurrentWorldId; |
| | 0 | 676 | | if (!worldId.HasValue || worldId == Guid.Empty) |
| | | 677 | | { |
| | 0 | 678 | | Snackbar.Add("Please select a World first", Severity.Warning); |
| | 0 | 679 | | return; |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | try |
| | | 683 | | { |
| | 0 | 684 | | var createDto = new ArticleCreateDto |
| | 0 | 685 | | { |
| | 0 | 686 | | Title = "Untitled", |
| | 0 | 687 | | Body = string.Empty, |
| | 0 | 688 | | ParentId = null, |
| | 0 | 689 | | EffectiveDate = DateTime.Now, |
| | 0 | 690 | | WorldId = worldId.Value |
| | 0 | 691 | | }; |
| | | 692 | | |
| | 0 | 693 | | await ArticleApi.CreateArticleAsync(createDto); |
| | 0 | 694 | | await TreeState.RefreshAsync(); |
| | 0 | 695 | | Snackbar.Add("Article created", Severity.Success); |
| | 0 | 696 | | } |
| | 0 | 697 | | catch (Exception ex) |
| | | 698 | | { |
| | 0 | 699 | | Snackbar.Add($"Failed to create article: {ex.Message}", Severity.Error); |
| | 0 | 700 | | } |
| | 0 | 701 | | } |
| | | 702 | | |
| | | 703 | | private async Task CreateSiblingArticle() |
| | | 704 | | { |
| | 0 | 705 | | if (_article == null) return; |
| | | 706 | | |
| | | 707 | | try |
| | | 708 | | { |
| | 0 | 709 | | var createDto = new ArticleCreateDto |
| | 0 | 710 | | { |
| | 0 | 711 | | Title = string.Empty, |
| | 0 | 712 | | Body = string.Empty, |
| | 0 | 713 | | ParentId = _article.ParentId, |
| | 0 | 714 | | EffectiveDate = DateTime.Now, |
| | 0 | 715 | | WorldId = _article.WorldId, |
| | 0 | 716 | | CampaignId = _article.CampaignId |
| | 0 | 717 | | }; |
| | | 718 | | |
| | 0 | 719 | | var created = await ArticleApi.CreateArticleAsync(createDto); |
| | 0 | 720 | | if (created == null) |
| | | 721 | | { |
| | 0 | 722 | | Snackbar.Add("Failed to create article", Severity.Error); |
| | 0 | 723 | | return; |
| | | 724 | | } |
| | | 725 | | |
| | 0 | 726 | | await TreeState.RefreshAsync(); |
| | 0 | 727 | | TreeState.ExpandPathToAndSelect(created.Id); |
| | | 728 | | |
| | | 729 | | // Fetch the full article to get breadcrumbs for navigation |
| | 0 | 730 | | var articleDetail = await ArticleApi.GetArticleDetailAsync(created.Id); |
| | 0 | 731 | | if (articleDetail != null && articleDetail.Breadcrumbs.Any()) |
| | | 732 | | { |
| | 0 | 733 | | var path = BreadcrumbService.BuildArticleUrl(articleDetail.Breadcrumbs); |
| | 0 | 734 | | Navigation.NavigateTo(path); |
| | | 735 | | } |
| | | 736 | | else |
| | | 737 | | { |
| | | 738 | | // Fallback to slug-only path |
| | 0 | 739 | | Navigation.NavigateTo($"/article/{created.Slug}"); |
| | | 740 | | } |
| | | 741 | | |
| | 0 | 742 | | Snackbar.Add("New article created", Severity.Success); |
| | 0 | 743 | | } |
| | 0 | 744 | | catch (Exception ex) |
| | | 745 | | { |
| | 0 | 746 | | Snackbar.Add($"Failed to create article: {ex.Message}", Severity.Error); |
| | 0 | 747 | | } |
| | 0 | 748 | | } |
| | | 749 | | |
| | | 750 | | private async Task CreateChildArticle() |
| | | 751 | | { |
| | 0 | 752 | | if (_article == null || _isCreatingChild) return; |
| | | 753 | | |
| | 0 | 754 | | _isCreatingChild = true; |
| | 0 | 755 | | StateHasChanged(); |
| | | 756 | | |
| | | 757 | | try |
| | | 758 | | { |
| | 0 | 759 | | var createDto = new ArticleCreateDto |
| | 0 | 760 | | { |
| | 0 | 761 | | Title = string.Empty, |
| | 0 | 762 | | Body = string.Empty, |
| | 0 | 763 | | ParentId = _article.Id, // Current article becomes the parent |
| | 0 | 764 | | EffectiveDate = DateTime.Now, |
| | 0 | 765 | | WorldId = _article.WorldId, |
| | 0 | 766 | | CampaignId = _article.CampaignId |
| | 0 | 767 | | }; |
| | | 768 | | |
| | 0 | 769 | | var created = await ArticleApi.CreateArticleAsync(createDto); |
| | 0 | 770 | | if (created == null) |
| | | 771 | | { |
| | 0 | 772 | | Snackbar.Add("Failed to create child article", Severity.Error); |
| | 0 | 773 | | return; |
| | | 774 | | } |
| | | 775 | | |
| | 0 | 776 | | await TreeState.RefreshAsync(); |
| | 0 | 777 | | TreeState.ExpandPathToAndSelect(created.Id); |
| | 0 | 778 | | TreeState.ShouldFocusTitle = true; |
| | | 779 | | |
| | | 780 | | // Fetch the full article to get breadcrumbs for navigation |
| | 0 | 781 | | var articleDetail = await ArticleApi.GetArticleDetailAsync(created.Id); |
| | 0 | 782 | | if (articleDetail != null && articleDetail.Breadcrumbs.Any()) |
| | | 783 | | { |
| | 0 | 784 | | var path = BreadcrumbService.BuildArticleUrl(articleDetail.Breadcrumbs); |
| | 0 | 785 | | Navigation.NavigateTo(path); |
| | | 786 | | } |
| | | 787 | | else |
| | | 788 | | { |
| | | 789 | | // Fallback to slug-only path |
| | 0 | 790 | | Navigation.NavigateTo($"/article/{created.Slug}"); |
| | | 791 | | } |
| | | 792 | | |
| | 0 | 793 | | Snackbar.Add("Child article created", Severity.Success); |
| | 0 | 794 | | } |
| | 0 | 795 | | catch (Exception ex) |
| | | 796 | | { |
| | 0 | 797 | | Logger.LogError(ex, "Error creating child article under {ParentId}", _article.Id); |
| | 0 | 798 | | Snackbar.Add($"Failed to create child article: {ex.Message}", Severity.Error); |
| | 0 | 799 | | } |
| | | 800 | | finally |
| | | 801 | | { |
| | 0 | 802 | | _isCreatingChild = false; |
| | 0 | 803 | | StateHasChanged(); |
| | | 804 | | } |
| | 0 | 805 | | } |
| | | 806 | | |
| | | 807 | | #endregion |
| | | 808 | | |
| | | 809 | | #region Keyboard Shortcuts |
| | | 810 | | |
| | | 811 | | private void OnMetadataDrawerToggle() |
| | | 812 | | { |
| | 0 | 813 | | _openMetadata = !_openMetadata; |
| | 0 | 814 | | InvokeAsync(StateHasChanged); |
| | 0 | 815 | | } |
| | | 816 | | |
| | | 817 | | private async void HandleSaveShortcut() |
| | | 818 | | { |
| | 0 | 819 | | await InvokeAsync(SaveArticle); |
| | 0 | 820 | | } |
| | | 821 | | |
| | | 822 | | [JSInvokable("HandleCtrlN")] |
| | | 823 | | public static Task HandleCtrlN() |
| | | 824 | | { |
| | 0 | 825 | | OnCtrlNPressed?.Invoke(); |
| | 0 | 826 | | return Task.CompletedTask; |
| | | 827 | | } |
| | | 828 | | |
| | | 829 | | private static event Action? OnCtrlNPressed; |
| | | 830 | | |
| | 0 | 831 | | private async void HandleCtrlNInstance() => await InvokeAsync(CreateSiblingArticle); |
| | | 832 | | |
| | | 833 | | #endregion |
| | | 834 | | |
| | | 835 | | #region Wiki Link Autocomplete |
| | | 836 | | |
| | | 837 | | [JSInvokable] |
| | | 838 | | public async Task OnAutocompleteTriggered(string query, double x, double y) |
| | | 839 | | { |
| | 0 | 840 | | _autocompletePosition = (x, y); |
| | 0 | 841 | | _showAutocomplete = true; |
| | 0 | 842 | | _autocompleteSelectedIndex = 0; |
| | | 843 | | |
| | 0 | 844 | | _autocompleteIsExternalQuery = TryParseExternalAutocompleteQuery(query, out var sourceKey, out var remainder); |
| | 0 | 845 | | _autocompleteExternalSourceKey = _autocompleteIsExternalQuery ? sourceKey : null; |
| | 0 | 846 | | _autocompleteQuery = _autocompleteIsExternalQuery ? remainder : query; |
| | | 847 | | |
| | | 848 | | // For external queries (like srd/...), we have different length requirements: |
| | | 849 | | // - Empty or short remainder: show categories (handled by server) |
| | | 850 | | // - Any length after category selected: search immediately |
| | | 851 | | // For internal queries: require 3 characters minimum |
| | 0 | 852 | | var minLength = _autocompleteIsExternalQuery ? 0 : 3; |
| | | 853 | | |
| | 0 | 854 | | if (_autocompleteQuery.Length < minLength) |
| | | 855 | | { |
| | 0 | 856 | | _autocompleteSuggestions = new(); |
| | 0 | 857 | | StateHasChanged(); |
| | 0 | 858 | | return; |
| | | 859 | | } |
| | | 860 | | |
| | 0 | 861 | | _autocompleteLoading = true; |
| | 0 | 862 | | StateHasChanged(); |
| | | 863 | | |
| | | 864 | | try |
| | | 865 | | { |
| | 0 | 866 | | var worldId = AppContext.CurrentWorldId ?? _article?.WorldId ?? Guid.Empty; |
| | | 867 | | |
| | 0 | 868 | | if (_autocompleteIsExternalQuery) |
| | | 869 | | { |
| | 0 | 870 | | var externalSuggestions = await ExternalLinkApiService.GetSuggestionsAsync( |
| | 0 | 871 | | worldId, |
| | 0 | 872 | | _autocompleteExternalSourceKey ?? string.Empty, |
| | 0 | 873 | | _autocompleteQuery, |
| | 0 | 874 | | CancellationToken.None); |
| | 0 | 875 | | _autocompleteSuggestions = externalSuggestions |
| | 0 | 876 | | .Select(WikiLinkAutocompleteItem.FromExternal) |
| | 0 | 877 | | .ToList(); |
| | | 878 | | } |
| | | 879 | | else |
| | | 880 | | { |
| | 0 | 881 | | var internalSuggestions = await LinkApiService.GetSuggestionsAsync(worldId, _autocompleteQuery); |
| | 0 | 882 | | _autocompleteSuggestions = internalSuggestions |
| | 0 | 883 | | .Select(WikiLinkAutocompleteItem.FromInternal) |
| | 0 | 884 | | .ToList(); |
| | | 885 | | } |
| | 0 | 886 | | } |
| | 0 | 887 | | catch (Exception ex) |
| | | 888 | | { |
| | 0 | 889 | | Logger.LogError(ex, "Error getting autocomplete suggestions"); |
| | 0 | 890 | | _autocompleteSuggestions = new(); |
| | 0 | 891 | | } |
| | | 892 | | finally |
| | | 893 | | { |
| | 0 | 894 | | _autocompleteLoading = false; |
| | 0 | 895 | | StateHasChanged(); |
| | | 896 | | } |
| | 0 | 897 | | } |
| | | 898 | | |
| | | 899 | | [JSInvokable] |
| | | 900 | | public Task OnAutocompleteHidden() |
| | | 901 | | { |
| | 0 | 902 | | _showAutocomplete = false; |
| | 0 | 903 | | _autocompleteSuggestions = new(); |
| | 0 | 904 | | _autocompleteIsExternalQuery = false; |
| | 0 | 905 | | _autocompleteExternalSourceKey = null; |
| | 0 | 906 | | StateHasChanged(); |
| | 0 | 907 | | return Task.CompletedTask; |
| | | 908 | | } |
| | | 909 | | |
| | | 910 | | [JSInvokable] |
| | | 911 | | public Task OnAutocompleteArrowDown() |
| | | 912 | | { |
| | 0 | 913 | | if (_autocompleteSuggestions.Any()) |
| | | 914 | | { |
| | 0 | 915 | | _autocompleteSelectedIndex = (_autocompleteSelectedIndex + 1) % _autocompleteSuggestions.Count; |
| | 0 | 916 | | StateHasChanged(); |
| | | 917 | | } |
| | 0 | 918 | | return Task.CompletedTask; |
| | | 919 | | } |
| | | 920 | | |
| | | 921 | | [JSInvokable] |
| | | 922 | | public Task OnAutocompleteArrowUp() |
| | | 923 | | { |
| | 0 | 924 | | if (_autocompleteSuggestions.Any()) |
| | | 925 | | { |
| | 0 | 926 | | _autocompleteSelectedIndex = (_autocompleteSelectedIndex - 1 + _autocompleteSuggestions.Count) % _autocomple |
| | 0 | 927 | | StateHasChanged(); |
| | | 928 | | } |
| | 0 | 929 | | return Task.CompletedTask; |
| | | 930 | | } |
| | | 931 | | |
| | | 932 | | [JSInvokable] |
| | | 933 | | public async Task OnAutocompleteEnter() |
| | | 934 | | { |
| | 0 | 935 | | if (_autocompleteSuggestions.Any() && _autocompleteSelectedIndex < _autocompleteSuggestions.Count) |
| | 0 | 936 | | await OnAutocompleteSelect(_autocompleteSuggestions[_autocompleteSelectedIndex]); |
| | 0 | 937 | | } |
| | | 938 | | |
| | | 939 | | private Task OnAutocompleteIndexChanged(int index) |
| | | 940 | | { |
| | 0 | 941 | | _autocompleteSelectedIndex = index; |
| | 0 | 942 | | StateHasChanged(); |
| | 0 | 943 | | return Task.CompletedTask; |
| | | 944 | | } |
| | | 945 | | |
| | | 946 | | private async Task OnAutocompleteSelect(WikiLinkAutocompleteItem suggestion) |
| | | 947 | | { |
| | 0 | 948 | | if (_article == null) return; |
| | | 949 | | |
| | | 950 | | try |
| | | 951 | | { |
| | | 952 | | // Handle category selection - update the editor text instead of inserting a link |
| | 0 | 953 | | if (suggestion.IsCategory && !string.IsNullOrEmpty(suggestion.CategoryKey)) |
| | | 954 | | { |
| | 0 | 955 | | await JSRuntime.InvokeVoidAsync( |
| | 0 | 956 | | "updateAutocompleteText", |
| | 0 | 957 | | $"tiptap-editor-{_article.Id}", |
| | 0 | 958 | | $"{suggestion.Source}/{suggestion.CategoryKey}/"); |
| | | 959 | | |
| | | 960 | | // Trigger autocomplete again with the new text |
| | | 961 | | // The user can now type a search term |
| | 0 | 962 | | return; |
| | | 963 | | } |
| | | 964 | | |
| | 0 | 965 | | if (suggestion.IsExternal) |
| | | 966 | | { |
| | 0 | 967 | | if (string.IsNullOrWhiteSpace(suggestion.Source) || string.IsNullOrWhiteSpace(suggestion.ExternalId)) |
| | | 968 | | { |
| | 0 | 969 | | Logger.LogWarning("External suggestion missing source or id"); |
| | 0 | 970 | | return; |
| | | 971 | | } |
| | | 972 | | |
| | 0 | 973 | | await JSRuntime.InvokeVoidAsync( |
| | 0 | 974 | | "insertExternalLinkToken", |
| | 0 | 975 | | $"tiptap-editor-{_article.Id}", |
| | 0 | 976 | | suggestion.Source, |
| | 0 | 977 | | suggestion.ExternalId, |
| | 0 | 978 | | suggestion.Title); |
| | | 979 | | } |
| | | 980 | | else |
| | | 981 | | { |
| | 0 | 982 | | if (!suggestion.ArticleId.HasValue) |
| | | 983 | | { |
| | 0 | 984 | | Logger.LogWarning("Internal suggestion missing article id"); |
| | 0 | 985 | | return; |
| | | 986 | | } |
| | | 987 | | |
| | | 988 | | // If matched via alias, display "Alias → Title", otherwise just title |
| | 0 | 989 | | var displayText = !string.IsNullOrWhiteSpace(suggestion.MatchedAlias) |
| | 0 | 990 | | ? $"{suggestion.MatchedAlias} → {suggestion.Title}" |
| | 0 | 991 | | : suggestion.Title; |
| | | 992 | | |
| | 0 | 993 | | await JSRuntime.InvokeVoidAsync( |
| | 0 | 994 | | "insertWikiLink", |
| | 0 | 995 | | $"tiptap-editor-{_article.Id}", |
| | 0 | 996 | | suggestion.ArticleId.Value.ToString(), |
| | 0 | 997 | | displayText); |
| | | 998 | | } |
| | 0 | 999 | | _showAutocomplete = false; |
| | 0 | 1000 | | _autocompleteSuggestions = new(); |
| | 0 | 1001 | | StateHasChanged(); |
| | 0 | 1002 | | await Task.Delay(50); |
| | 0 | 1003 | | await SaveArticle(); |
| | 0 | 1004 | | } |
| | 0 | 1005 | | catch (Exception ex) |
| | | 1006 | | { |
| | 0 | 1007 | | Logger.LogError(ex, "Error inserting wiki link"); |
| | 0 | 1008 | | Snackbar.Add("Failed to insert link", Severity.Error); |
| | 0 | 1009 | | } |
| | 0 | 1010 | | } |
| | | 1011 | | |
| | | 1012 | | private async Task OnAutocompleteCreate(string articleName) |
| | | 1013 | | { |
| | 0 | 1014 | | if (_autocompleteIsExternalQuery) |
| | | 1015 | | { |
| | 0 | 1016 | | return; |
| | | 1017 | | } |
| | | 1018 | | |
| | 0 | 1019 | | if (_article == null || string.IsNullOrWhiteSpace(articleName)) return; |
| | | 1020 | | |
| | | 1021 | | try |
| | | 1022 | | { |
| | 0 | 1023 | | var worldId = AppContext.CurrentWorldId ?? _article.WorldId ?? Guid.Empty; |
| | 0 | 1024 | | var created = await WikiLinkService.CreateArticleFromAutocompleteAsync(articleName, worldId); |
| | | 1025 | | |
| | 0 | 1026 | | if (created == null) |
| | | 1027 | | { |
| | 0 | 1028 | | Snackbar.Add("Failed to create article", Severity.Error); |
| | 0 | 1029 | | return; |
| | | 1030 | | } |
| | | 1031 | | |
| | 0 | 1032 | | await JSRuntime.InvokeVoidAsync("insertWikiLink", $"tiptap-editor-{_article.Id}", created.Id.ToString(), cre |
| | 0 | 1033 | | _showAutocomplete = false; |
| | 0 | 1034 | | _autocompleteSuggestions = new(); |
| | 0 | 1035 | | StateHasChanged(); |
| | | 1036 | | |
| | 0 | 1037 | | await Task.Delay(50); |
| | 0 | 1038 | | await SaveArticle(); |
| | 0 | 1039 | | await TreeState.RefreshAsync(); |
| | 0 | 1040 | | Snackbar.Add($"Created and linked '{articleName}'", Severity.Success); |
| | 0 | 1041 | | } |
| | 0 | 1042 | | catch (Exception ex) |
| | | 1043 | | { |
| | 0 | 1044 | | Logger.LogError(ex, "Error creating article from autocomplete"); |
| | 0 | 1045 | | Snackbar.Add($"Failed to create article: {ex.Message}", Severity.Error); |
| | 0 | 1046 | | } |
| | 0 | 1047 | | } |
| | | 1048 | | |
| | | 1049 | | private static bool TryParseExternalAutocompleteQuery(string query, out string sourceKey, out string remainder) |
| | | 1050 | | { |
| | 0 | 1051 | | sourceKey = string.Empty; |
| | 0 | 1052 | | remainder = string.Empty; |
| | | 1053 | | |
| | 0 | 1054 | | if (string.IsNullOrWhiteSpace(query)) |
| | | 1055 | | { |
| | 0 | 1056 | | return false; |
| | | 1057 | | } |
| | | 1058 | | |
| | 0 | 1059 | | var slashIndex = query.IndexOf('/'); |
| | 0 | 1060 | | if (slashIndex <= 0) |
| | | 1061 | | { |
| | 0 | 1062 | | return false; |
| | | 1063 | | } |
| | | 1064 | | |
| | 0 | 1065 | | sourceKey = query.Substring(0, slashIndex).Trim().ToLowerInvariant(); |
| | 0 | 1066 | | remainder = query.Substring(slashIndex + 1); |
| | | 1067 | | |
| | 0 | 1068 | | return !string.IsNullOrWhiteSpace(sourceKey); |
| | | 1069 | | } |
| | | 1070 | | |
| | | 1071 | | [JSInvokable] |
| | | 1072 | | public async Task OnWikiLinkClicked(string targetArticleId) |
| | | 1073 | | { |
| | 0 | 1074 | | if (!Guid.TryParse(targetArticleId, out var articleId)) return; |
| | | 1075 | | |
| | | 1076 | | try |
| | | 1077 | | { |
| | 0 | 1078 | | var path = await ArticleCache.GetNavigationPathAsync(articleId); |
| | 0 | 1079 | | if (!string.IsNullOrEmpty(path)) |
| | 0 | 1080 | | Navigation.NavigateTo($"/article/{path}"); |
| | | 1081 | | else |
| | 0 | 1082 | | Snackbar.Add("Article not found", Severity.Warning); |
| | 0 | 1083 | | } |
| | 0 | 1084 | | catch (Exception ex) |
| | | 1085 | | { |
| | 0 | 1086 | | Logger.LogError(ex, "Error navigating to wiki link: {ArticleId}", targetArticleId); |
| | 0 | 1087 | | Snackbar.Add("Failed to navigate to article", Severity.Error); |
| | 0 | 1088 | | } |
| | 0 | 1089 | | } |
| | | 1090 | | |
| | | 1091 | | [JSInvokable] |
| | | 1092 | | public async Task<string?> GetArticlePath(string targetArticleId) |
| | | 1093 | | { |
| | 0 | 1094 | | if (!Guid.TryParse(targetArticleId, out var articleId)) return null; |
| | | 1095 | | |
| | | 1096 | | try |
| | | 1097 | | { |
| | 0 | 1098 | | return await ArticleCache.GetArticlePathAsync(articleId); |
| | | 1099 | | } |
| | 0 | 1100 | | catch (Exception ex) |
| | | 1101 | | { |
| | 0 | 1102 | | Logger.LogError(ex, "Error getting article path: {ArticleId}", targetArticleId); |
| | 0 | 1103 | | return null; |
| | | 1104 | | } |
| | 0 | 1105 | | } |
| | | 1106 | | |
| | | 1107 | | [JSInvokable] |
| | | 1108 | | public async Task<object?> GetArticleSummaryPreview(string targetArticleId) |
| | | 1109 | | { |
| | 0 | 1110 | | if (!Guid.TryParse(targetArticleId, out var articleId)) return null; |
| | | 1111 | | |
| | | 1112 | | try |
| | | 1113 | | { |
| | 0 | 1114 | | var preview = await SummaryApi.GetSummaryPreviewAsync(articleId); |
| | 0 | 1115 | | if (preview == null || !preview.HasSummary) return null; |
| | | 1116 | | |
| | 0 | 1117 | | return new |
| | 0 | 1118 | | { |
| | 0 | 1119 | | title = preview.Title, |
| | 0 | 1120 | | summary = preview.Summary, |
| | 0 | 1121 | | templateName = preview.TemplateName |
| | 0 | 1122 | | }; |
| | | 1123 | | } |
| | 0 | 1124 | | catch (Exception ex) |
| | | 1125 | | { |
| | 0 | 1126 | | Logger.LogError(ex, "Error getting summary preview: {ArticleId}", targetArticleId); |
| | 0 | 1127 | | return null; |
| | | 1128 | | } |
| | 0 | 1129 | | } |
| | | 1130 | | |
| | | 1131 | | [JSInvokable] |
| | | 1132 | | public async Task OnExternalLinkClicked(string source, string id, string title) |
| | | 1133 | | { |
| | 0 | 1134 | | if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(id)) return; |
| | | 1135 | | |
| | 0 | 1136 | | _externalPreviewOpen = true; |
| | 0 | 1137 | | _externalPreviewLoading = true; |
| | 0 | 1138 | | _externalPreviewError = null; |
| | 0 | 1139 | | _externalPreviewSource = source; |
| | 0 | 1140 | | _externalPreviewId = id; |
| | 0 | 1141 | | _externalPreviewTitle = string.IsNullOrWhiteSpace(title) ? "External Link" : title; |
| | 0 | 1142 | | _externalPreviewContent = null; |
| | 0 | 1143 | | StateHasChanged(); |
| | | 1144 | | |
| | 0 | 1145 | | var cacheKey = GetExternalCacheKey(source, id); |
| | 0 | 1146 | | if (_externalLinkCache.TryGetValue(cacheKey, out var cached)) |
| | | 1147 | | { |
| | 0 | 1148 | | _externalPreviewContent = cached; |
| | 0 | 1149 | | _externalPreviewLoading = false; |
| | 0 | 1150 | | StateHasChanged(); |
| | 0 | 1151 | | return; |
| | | 1152 | | } |
| | | 1153 | | |
| | | 1154 | | try |
| | | 1155 | | { |
| | 0 | 1156 | | var content = await ExternalLinkApiService.GetContentAsync(source, id, CancellationToken.None); |
| | | 1157 | | |
| | 0 | 1158 | | if (content == null || string.IsNullOrWhiteSpace(content.Markdown)) |
| | | 1159 | | { |
| | 0 | 1160 | | _externalPreviewError = "No content available."; |
| | 0 | 1161 | | _externalPreviewContent = null; |
| | | 1162 | | } |
| | | 1163 | | else |
| | | 1164 | | { |
| | 0 | 1165 | | _externalPreviewContent = content; |
| | 0 | 1166 | | _externalLinkCache[cacheKey] = content; |
| | | 1167 | | } |
| | 0 | 1168 | | } |
| | 0 | 1169 | | catch (Exception ex) |
| | | 1170 | | { |
| | 0 | 1171 | | Logger.LogError(ex, "Error loading external link preview for {Source} {Id}", source, id); |
| | 0 | 1172 | | _externalPreviewError = "Failed to load external content."; |
| | 0 | 1173 | | _externalPreviewContent = null; |
| | 0 | 1174 | | } |
| | | 1175 | | finally |
| | | 1176 | | { |
| | 0 | 1177 | | _externalPreviewLoading = false; |
| | 0 | 1178 | | StateHasChanged(); |
| | | 1179 | | } |
| | 0 | 1180 | | } |
| | | 1181 | | |
| | | 1182 | | private void CloseExternalPreview() |
| | | 1183 | | { |
| | 0 | 1184 | | _externalPreviewOpen = false; |
| | 0 | 1185 | | StateHasChanged(); |
| | 0 | 1186 | | } |
| | | 1187 | | |
| | 0 | 1188 | | private static string GetExternalCacheKey(string source, string id) => $"{source}:{id}".ToLowerInvariant(); |
| | | 1189 | | |
| | | 1190 | | #endregion |
| | | 1191 | | |
| | | 1192 | | #region Image Upload |
| | | 1193 | | |
| | | 1194 | | [JSInvokable] |
| | | 1195 | | public async Task<object?> OnImageUploadRequested(string fileName, string contentType, long fileSize) |
| | | 1196 | | { |
| | 0 | 1197 | | if (_article == null) return null; |
| | | 1198 | | |
| | 0 | 1199 | | var worldId = AppContext.CurrentWorldId ?? _article.WorldId ?? Guid.Empty; |
| | 0 | 1200 | | if (worldId == Guid.Empty) return null; |
| | | 1201 | | |
| | | 1202 | | try |
| | | 1203 | | { |
| | 0 | 1204 | | var request = new WorldDocumentUploadRequestDto |
| | 0 | 1205 | | { |
| | 0 | 1206 | | FileName = fileName, |
| | 0 | 1207 | | ContentType = contentType, |
| | 0 | 1208 | | FileSizeBytes = fileSize, |
| | 0 | 1209 | | ArticleId = _article.Id, |
| | 0 | 1210 | | Description = $"Inline image for article: {_article.Title}" |
| | 0 | 1211 | | }; |
| | | 1212 | | |
| | 0 | 1213 | | var response = await WorldApi.RequestDocumentUploadAsync(worldId, request); |
| | 0 | 1214 | | if (response == null) return null; |
| | | 1215 | | |
| | 0 | 1216 | | return new |
| | 0 | 1217 | | { |
| | 0 | 1218 | | uploadUrl = response.UploadUrl, |
| | 0 | 1219 | | documentId = response.DocumentId.ToString() |
| | 0 | 1220 | | }; |
| | | 1221 | | } |
| | 0 | 1222 | | catch (Exception ex) |
| | | 1223 | | { |
| | 0 | 1224 | | Logger.LogError(ex, "Error requesting image upload for article {ArticleId}", _article.Id); |
| | 0 | 1225 | | return null; |
| | | 1226 | | } |
| | 0 | 1227 | | } |
| | | 1228 | | |
| | | 1229 | | [JSInvokable] |
| | | 1230 | | public async Task OnImageUploadConfirmed(string documentIdStr) |
| | | 1231 | | { |
| | 0 | 1232 | | if (_article == null || !Guid.TryParse(documentIdStr, out var documentId)) return; |
| | | 1233 | | |
| | 0 | 1234 | | var worldId = AppContext.CurrentWorldId ?? _article.WorldId ?? Guid.Empty; |
| | 0 | 1235 | | if (worldId == Guid.Empty) return; |
| | | 1236 | | |
| | | 1237 | | try |
| | | 1238 | | { |
| | 0 | 1239 | | await WorldApi.ConfirmDocumentUploadAsync(worldId, documentId); |
| | 0 | 1240 | | } |
| | 0 | 1241 | | catch (Exception ex) |
| | | 1242 | | { |
| | 0 | 1243 | | Logger.LogError(ex, "Error confirming image upload {DocumentId}", documentId); |
| | 0 | 1244 | | } |
| | 0 | 1245 | | } |
| | | 1246 | | |
| | | 1247 | | [JSInvokable] |
| | | 1248 | | public string GetImageProxyUrl(string documentIdStr) |
| | | 1249 | | { |
| | | 1250 | | // Return stable chronicis-image: reference (not a URL) |
| | 0 | 1251 | | return $"chronicis-image:{documentIdStr}"; |
| | | 1252 | | } |
| | | 1253 | | |
| | | 1254 | | [JSInvokable] |
| | | 1255 | | public async Task<string?> ResolveImageUrl(string documentIdStr) |
| | | 1256 | | { |
| | 0 | 1257 | | if (!Guid.TryParse(documentIdStr, out var documentId)) return null; |
| | | 1258 | | |
| | | 1259 | | try |
| | | 1260 | | { |
| | 0 | 1261 | | var result = await WorldApi.DownloadDocumentAsync(documentId); |
| | 0 | 1262 | | return result?.DownloadUrl; |
| | | 1263 | | } |
| | 0 | 1264 | | catch (Exception ex) |
| | | 1265 | | { |
| | 0 | 1266 | | Logger.LogError(ex, "Error resolving image URL for document {DocumentId}", documentId); |
| | 0 | 1267 | | return null; |
| | | 1268 | | } |
| | 0 | 1269 | | } |
| | | 1270 | | |
| | | 1271 | | [JSInvokable] |
| | | 1272 | | public void OnImageUploadStarted(string fileName) |
| | | 1273 | | { |
| | 0 | 1274 | | InvokeAsync(() => |
| | 0 | 1275 | | { |
| | 0 | 1276 | | Snackbar.Add($"Uploading {fileName}...", Severity.Info); |
| | 0 | 1277 | | StateHasChanged(); |
| | 0 | 1278 | | }); |
| | 0 | 1279 | | } |
| | | 1280 | | |
| | | 1281 | | [JSInvokable] |
| | | 1282 | | public void OnImageUploadError(string message) |
| | | 1283 | | { |
| | 0 | 1284 | | InvokeAsync(() => |
| | 0 | 1285 | | { |
| | 0 | 1286 | | Snackbar.Add(message, Severity.Error); |
| | 0 | 1287 | | StateHasChanged(); |
| | 0 | 1288 | | }); |
| | 0 | 1289 | | } |
| | | 1290 | | |
| | | 1291 | | private async Task InsertImageFromToolbar() |
| | | 1292 | | { |
| | 0 | 1293 | | if (_article == null || !_editorInitialized) return; |
| | 0 | 1294 | | await JSRuntime.InvokeVoidAsync("triggerImageUpload", $"tiptap-editor-{_article.Id}", _dotNetHelper); |
| | 0 | 1295 | | } |
| | | 1296 | | |
| | | 1297 | | #endregion |
| | | 1298 | | } |