From 12d8436700f4c28a0b871fc990022675f85c17a6 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 30 Dec 2023 16:53:56 -0600 Subject: [PATCH] Make autolinking work with multiple links in a topic or note --- .../Sources/VinOutlineKit/Outline.swift | 61 ++++++++++++------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/VinOutlineKit/Sources/VinOutlineKit/Outline.swift b/VinOutlineKit/Sources/VinOutlineKit/Outline.swift index d83b9a03..94adea34 100644 --- a/VinOutlineKit/Sources/VinOutlineKit/Outline.swift +++ b/VinOutlineKit/Sources/VinOutlineKit/Outline.swift @@ -2326,6 +2326,8 @@ public final class Outline: RowContainer, Identifiable, Equatable, Hashable, Cod } func outlineElementsDidChange(_ changes: OutlineElementChanges) { + guard isBeingUsed else { return } + var userInfo = [AnyHashable: Any]() userInfo[OutlineElementChanges.userInfoKey] = changes NotificationCenter.default.post(name: .OutlineElementsDidChange, object: self, userInfo: userInfo) @@ -2868,38 +2870,53 @@ private extension Outline { func replaceLinkTitleIfPossible(row: Row, newText: NSAttributedString?, isInNotes: Bool) { guard let newText else { return } - let mutableText = NSMutableAttributedString(attributedString: newText) + incrementBeingUsedCount() + + var pageTitles = [URL: String]() + let group = DispatchGroup() - mutableText.enumerateAttribute(.link, in: NSRange(location: 0, length: mutableText.length)) { [weak self] (value, range, match) in - guard let url = value as? URL, let self = self else { return } - guard mutableText.attributedSubstring(from: range).string == url.absoluteString else { return } + newText.enumerateAttribute(.link, in: NSRange(location: 0, length: newText.length)) { (value, range, match) in + guard let url = value as? URL else { return } - incrementBeingUsedCount() + group.enter() + WebPageTitle.find(forURL: url) { pageTitle in + pageTitles[url] = pageTitle + group.leave() + } + } + + group.notify(queue: .main) { [weak self ] in + guard let self else { return } - WebPageTitle.find(forURL: url) { [weak self] pageTitle in - guard let pageTitle = pageTitle, let self = self else { return } + let mutableText = NSMutableAttributedString(attributedString: newText) + + mutableText.enumerateAttribute(.link, in: NSRange(location: 0, length: mutableText.length)) { (value, range, match) in + guard let url = value as? URL, + let pageTitle = pageTitles[url], + mutableText.attributedSubstring(from: range).string == url.absoluteString else { + return + } mutableText.removeAttribute(.link, range: range) mutableText.replaceCharacters(in: range, with: pageTitle) mutableText.addAttribute(.link, value: url, range: NSRange(location: range.location, length: pageTitle.count)) - - guard let row = self.findRow(id: row.id) else { return } - - if !isInNotes { - row.topic = mutableText - } else { - row.note = mutableText - } - - self.decrementBeingUsedCount() - self.unload() + } + + guard let row = self.findRow(id: row.id) else { return } + + if !isInNotes { + row.topic = mutableText + } else { + row.note = mutableText + } - guard self.isBeingUsed else { return } + self.decrementBeingUsedCount() + self.unload() - if let shadowTableIndex = row.shadowTableIndex { - self.outlineElementsDidChange(OutlineElementChanges(section: self.adjustedRowsSection, reloads: Set([shadowTableIndex]))) - } + if let shadowTableIndex = row.shadowTableIndex { + self.outlineElementsDidChange(OutlineElementChanges(section: self.adjustedRowsSection, reloads: Set([shadowTableIndex]))) } + } }