Skip to content

Commit

Permalink
🔀 merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
侑夕 committed Sep 2, 2022
2 parents e42be80 + cc49772 commit 6eb1d34
Show file tree
Hide file tree
Showing 41 changed files with 32,516 additions and 574 deletions.
17 changes: 15 additions & 2 deletions Common/Shared/CodeTextProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ class CodeTextProcessor {
self.textStorage = textStorage
}

public func getCodeBlockRanges() -> [NSRange]? {
var paragraphRanges = [NSRange]()
var paragraphList = [String]()

let string = textStorage.string as NSString
string.enumerateSubstrings(in: NSRange(0..<string.length), options: .byParagraphs) {value, range, _, _ in
paragraphRanges.append(range)
paragraphList.append(value!)
}

return getBlockRanges(ranges: paragraphRanges, pars: paragraphList)
}

public func getCodeBlockRanges(parRange: NSRange) -> [NSRange]? {
let min = scanCodeBlockUp(location: parRange.location - 1)
let max = scanCodeBlockDown(location: parRange.upperBound)
Expand Down Expand Up @@ -114,7 +127,7 @@ class CodeTextProcessor {
}

private func isCodeBlock(_ attributedString: NSAttributedString) -> Bool {
if attributedString.string.starts(with: "\t") || attributedString.string.starts(with: " ") {
if attributedString.string.starts(with: "\t") || attributedString.string.starts(with: " ") {
return true
}

Expand Down Expand Up @@ -180,7 +193,7 @@ class CodeTextProcessor {
}

public func isCodeBlockParagraph(_ paragraph: String) -> Bool {
if paragraph.starts(with: "\t") || paragraph.starts(with: " ") {
if paragraph.starts(with: "\t") || paragraph.starts(with: " ") {
return true
}

Expand Down
2 changes: 2 additions & 0 deletions Common/Shared/CustomTextStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ extension NSTextStorage: NSTextStorageDelegate {
if let value = value as? NSTextAttachment, textStorage.attribute(.todo, at: range.location, effectiveRange: nil) == nil {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .left
paragraph.lineSpacing = CGFloat(UserDefaultsManagement.editorLineSpacing)
paragraph.lineHeightMultiple = CGFloat(UserDefaultsManagement.editorLineHeight)
textStorage.addAttribute(.paragraphStyle, value: paragraph, range: range)

let imageKey = NSAttributedString.Key(rawValue: "com.tw93.miaoyan.image.url")
Expand Down
24 changes: 13 additions & 11 deletions Mac/AppDelegate+URLRoutes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,22 @@ extension AppDelegate {
}
} else if notes.count == 1 {
if let items = vc.storageOutlineView.sidebarItems {
if let sidebarItem = items.first(where: { ($0 as? SidebarItem)?.project == notes[0].project }) {
let sidebarIndex = vc.storageOutlineView.row(forItem: sidebarItem)
vc.updateTable {
DispatchQueue.main.async {
vc.storageOutlineView.selectRowIndexes([sidebarIndex], byExtendingSelection: false)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.04) {
if let index = vc.notesTableView.noteList.firstIndex(where: { $0 === notes[0] }) {
vc.notesTableView.selectRowIndexes([index], byExtendingSelection: false)
vc.notesTableView.scrollRowToVisible(row: index, animated: true)
}
// 修复在根目录的场景
var sidebarIndex = 0
if let sidebarItem = items.first(where: { ($0 as? SidebarItem)?.project == notes[0].project }){
sidebarIndex = vc.storageOutlineView.row(forItem: sidebarItem)
}
vc.updateTable {
DispatchQueue.main.async {
vc.storageOutlineView.selectRowIndexes([sidebarIndex], byExtendingSelection: false)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.04) {
if let index = vc.notesTableView.noteList.firstIndex(where: { $0 === notes[0] }) {
vc.notesTableView.selectRowIndexes([index], byExtendingSelection: false)
vc.notesTableView.scrollRowToVisible(row: index, animated: true)
}
}
}
}
}
}
} else {
vc.toastNoTitle()
Expand Down
20 changes: 11 additions & 9 deletions Mac/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21223" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="20037"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21223"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -1252,7 +1252,7 @@ CA
<constraint firstItem="1dd-W4-MiY" firstAttribute="leading" secondItem="I0q-sX-uBq" secondAttribute="leading" constant="3" id="KTq-ca-aXt"/>
<constraint firstAttribute="trailing" secondItem="bVt-gT-Six" secondAttribute="trailing" id="VUS-Wa-wfe"/>
<constraint firstItem="kTp-qw-RCd" firstAttribute="leading" secondItem="I0q-sX-uBq" secondAttribute="leading" constant="24" id="bav-Au-PqY"/>
<constraint firstItem="kTp-qw-RCd" firstAttribute="centerY" secondItem="1dd-W4-MiY" secondAttribute="centerY" constant="-1" id="etK-XI-kVT"/>
<constraint firstItem="kTp-qw-RCd" firstAttribute="centerY" secondItem="1dd-W4-MiY" secondAttribute="centerY" constant="-1.2" id="etK-XI-kVT"/>
<constraint firstAttribute="bottom" secondItem="bVt-gT-Six" secondAttribute="bottom" id="h6b-pM-7dD"/>
<constraint firstItem="bVt-gT-Six" firstAttribute="leading" secondItem="I0q-sX-uBq" secondAttribute="leading" id="oLU-KS-Pam"/>
<constraint firstItem="1dd-W4-MiY" firstAttribute="centerY" secondItem="I0q-sX-uBq" secondAttribute="centerY" id="slG-ke-sdb"/>
Expand Down Expand Up @@ -1527,10 +1527,10 @@ CA
<customView translatesAutoresizingMaskIntoConstraints="NO" id="xSU-Mk-2F1" customClass="TitleBarView" customModule="MiaoYan" customModuleProvider="target">
<rect key="frame" x="0.0" y="588" width="559" height="52"/>
<subviews>
<textField focusRingType="none" horizontalHuggingPriority="1" verticalHuggingPriority="750" horizontalCompressionResistancePriority="1" translatesAutoresizingMaskIntoConstraints="NO" id="rZx-hk-GcJ" customClass="TitleTextField" customModule="MiaoYan" customModuleProvider="target">
<rect key="frame" x="25" y="9" width="378" height="34"/>
<textField autoresizesSubviews="NO" focusRingType="none" horizontalHuggingPriority="1" verticalHuggingPriority="750" horizontalCompressionResistancePriority="1" translatesAutoresizingMaskIntoConstraints="NO" id="rZx-hk-GcJ" customClass="TitleTextField" customModule="MiaoYan" customModuleProvider="target">
<rect key="frame" x="25" y="10" width="378" height="32"/>
<constraints>
<constraint firstAttribute="height" constant="34" id="3QH-0a-na2"/>
<constraint firstAttribute="height" constant="32" id="3QH-0a-na2"/>
</constraints>
<textFieldCell key="cell" lineBreakMode="truncatingMiddle" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" focusRingType="none" alignment="left" title="Title" placeholderString="输入标题" drawsBackground="YES" id="O9K-a1-3eu">
<font key="font" metaFont="systemBold" size="20"/>
Expand Down Expand Up @@ -1601,7 +1601,7 @@ CA
<constraints>
<constraint firstItem="rZx-hk-GcJ" firstAttribute="leading" secondItem="xSU-Mk-2F1" secondAttribute="leading" constant="25" id="1I0-Qm-T21"/>
<constraint firstAttribute="trailing" secondItem="Ua8-RE-q01" secondAttribute="trailing" id="Uos-hm-kn6"/>
<constraint firstItem="rZx-hk-GcJ" firstAttribute="centerY" secondItem="xSU-Mk-2F1" secondAttribute="centerY" id="dAk-7a-dZK"/>
<constraint firstItem="rZx-hk-GcJ" firstAttribute="top" secondItem="xSU-Mk-2F1" secondAttribute="top" constant="10" id="Vst-qt-Crj"/>
<constraint firstItem="Ua8-RE-q01" firstAttribute="leading" secondItem="rZx-hk-GcJ" secondAttribute="trailing" constant="16" id="eWY-C1-IBp"/>
<constraint firstItem="Ua8-RE-q01" firstAttribute="top" secondItem="xSU-Mk-2F1" secondAttribute="top" id="esv-qQ-apf"/>
<constraint firstAttribute="bottom" secondItem="Ua8-RE-q01" secondAttribute="bottom" id="nac-xp-D3Y"/>
Expand Down Expand Up @@ -1648,6 +1648,7 @@ CA
</view>
<connections>
<outlet property="ProjectHeaderView" destination="vp7-cq-QSI" id="kED-nV-8xh"/>
<outlet property="TitleTopConstraint" destination="Vst-qt-Crj" id="bsj-AS-wer"/>
<outlet property="addProjectButton" destination="Yei-Or-qiI" id="bPx-nk-z5u"/>
<outlet property="editArea" destination="cmZ-KL-T9k" id="sJd-5P-0mf"/>
<outlet property="editAreaScroll" destination="qJO-7f-vkL" id="X69-yq-N67"/>
Expand All @@ -1674,6 +1675,7 @@ CA
<outlet property="titleBarAdditionalView" destination="Ua8-RE-q01" id="CfN-3p-UH3"/>
<outlet property="titleBarView" destination="xSU-Mk-2F1" id="lbu-pA-1Hs"/>
<outlet property="titleLabel" destination="rZx-hk-GcJ" id="wKj-zD-Ovz"/>
<outlet property="titleTopConstraint" destination="Vst-qt-Crj" id="ZOq-K2-BNC"/>
</connections>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
Expand Down Expand Up @@ -1880,8 +1882,8 @@ CA
</scene>
</scenes>
<resources>
<image name="NSQuickLookTemplate" width="21" height="13"/>
<image name="NSSlideshowTemplate" width="18" height="14"/>
<image name="NSQuickLookTemplate" width="26" height="17"/>
<image name="NSSlideshowTemplate" width="24" height="18"/>
<image name="makeNoteAsset" width="840" height="788"/>
<image name="new" width="120" height="120"/>
<image name="new_rep" width="120" height="120"/>
Expand Down
13 changes: 8 additions & 5 deletions Mac/Business/Note.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,8 @@ public class Note: NSObject {
}

func getPrettifiedContent() -> String {
var content = content.string

content = NotesTextProcessor.convertAppLinks(in: content)

return cleanMetaData(content: content)
let content = NotesTextProcessor.convertAppLinks(in: content)
return cleanMetaData(content: content.string)
}

public func overwrite(url: URL) {
Expand Down Expand Up @@ -1128,9 +1125,15 @@ public class Note: NSObject {

public func getTitle() -> String? {
if title.count > 0 {

if title.isValidUUID {
return getDefaultTitle()
}

if title.starts(with: "![") {
return nil;
}

return title
}

Expand Down
9 changes: 3 additions & 6 deletions Mac/Business/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,9 @@ class Storage {
}

private func sortQuery(note: Note, next: Note, project: Project?) -> Bool {
var sortDirection: SortDirection = UserDefaultsManagement.sortDirection ? .desc : .asc
if let project = project, project.sortBySettings != .none {
sortDirection = project.sortDirection
}
let sortDirection: SortDirection = UserDefaultsManagement.sortDirection ? .desc : .asc

let sort = project?.sortBy ?? UserDefaultsManagement.sort
let sort = UserDefaultsManagement.sort

if note.isPinned == next.isPinned {
switch sort {
Expand Down Expand Up @@ -536,7 +533,7 @@ class Storage {
noteList.remove(at: i)
}
}

func getNextId() -> Int {
noteList.count
}
Expand Down
92 changes: 75 additions & 17 deletions Mac/Helpers/NotesTextProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ public class NotesTextProcessor {

return foundRange
}

public static func getSpanCodeBlockRange(content: NSMutableAttributedString, range: NSRange) -> NSRange? {
var codeSpan: NSRange?
let paragraphRange = content.mutableString.paragraphRange(for: range)
let paragraph = content.attributedSubstring(from: paragraphRange).string

if paragraph.contains("`") {
NotesTextProcessor.codeSpanRegex.matches(content.string, range: paragraphRange) { (result) -> Void in
if let spanRange = result?.range, spanRange.intersection(range) != nil {
codeSpan = spanRange
}
}
}

return codeSpan
}

public static var hl: Highlightr?

Expand All @@ -230,7 +246,9 @@ public class NotesTextProcessor {
if UserDataService.instance.isDark {
codeTheme = "tomorrow-night-blue"
}

highlightr.setTheme(to: codeTheme)
highlightr.ignoreIllegals = true

hl = highlightr

Expand All @@ -254,7 +272,7 @@ public class NotesTextProcessor {
guard let highlighter = NotesTextProcessor.getHighlighter() else { return }
let codeString = attributedString.mutableString.substring(with: range)
let preDefinedLanguage = language ?? getLanguage(codeString)

if let code = highlighter.highlight(codeString, as: preDefinedLanguage) {
if (range.location + range.length) > attributedString.length {
return
Expand All @@ -263,7 +281,7 @@ public class NotesTextProcessor {
if attributedString.length >= range.upperBound, code.string != attributedString.mutableString.substring(with: range) {
return
}

code.enumerateAttributes(
in: NSMakeRange(0, code.length),
options: [],
Expand All @@ -290,8 +308,8 @@ public class NotesTextProcessor {
}

public static func applyCodeBlockStyle(attributedString: NSMutableAttributedString, range: NSRange) {
// let style = TextFormatter.getCodeParagraphStyle()
// attributedString.addAttribute(.paragraphStyle, value: style, range: range)
// let style = TextFormatter.getCodeParagraphStyle()
// attributedString.addAttribute(.paragraphStyle, value: style, range: range)
}

public static var languages: [String]?
Expand All @@ -311,6 +329,10 @@ public class NotesTextProcessor {
languages = getHighlighter()?.supportedLanguages()

if let lang = languages, lang.contains(detectedLang) {
// 兼容一下go
if(detectedLang == "go"){
return nil
}
return detectedLang
}
}
Expand All @@ -326,25 +348,47 @@ public class NotesTextProcessor {
- returns: Content string with converted links
*/

public static func convertAppLinks(in content: String) -> String {
var resultString = content
NotesTextProcessor.appUrlRegex.matches(content, range: NSRange(location: 0, length: (content as NSString).length), completion: { result in
guard let innerRange = result?.range else { return }
var _range = innerRange
_range.location = _range.location + 2
_range.length = _range.length - 4
public static func convertAppLinks(in content: NSMutableAttributedString) -> NSMutableAttributedString {
let attributedString = content.mutableCopy() as! NSMutableAttributedString
let range = NSRange(0..<content.string.count)
let tagQuery = "miaoyan://goto/"

let lintTitle = (content as NSString).substring(with: _range)
NotesTextProcessor.appUrlRegex.matches(content.string, range: range, completion: { (result) -> (Void) in
guard let innerRange = result?.range else { return }

let allowedCharacters = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
let escapedString = lintTitle.addingPercentEncoding(withAllowedCharacters: allowedCharacters)!
var substring = attributedString.mutableString.substring(with: innerRange)
substring = substring
.replacingOccurrences(of: "[[", with: "")
.replacingOccurrences(of: "]]", with: "")
.trim()

let newLink = "[\(lintTitle)](miaoyan://goto/\(escapedString))"
guard let tag = substring.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) else { return }

resultString = resultString.replacingOccurrences(of: "[[\(lintTitle)]]", with: newLink)
attributedString.addAttribute(.link, value: "\(tagQuery)\(tag)", range: innerRange)
})

return resultString
attributedString.enumerateAttribute(.link, in: range) { (value, range, _) in
if let value = value as? String, value.starts(with: tagQuery) {
if let tag = value
.replacingOccurrences(of: tagQuery, with: "")
.removingPercentEncoding
{

if NotesTextProcessor.getSpanCodeBlockRange(content: attributedString, range: range) != nil {
return
}

if NotesTextProcessor.getFencedCodeBlockRange(paragraphRange: range, string: attributedString) != nil {
return
}

let link = "[\(tag)](\(value))"
attributedString.replaceCharacters(in: range, with: link)
}
}
}

return attributedString
}

public static func highlight(note: Note) {
Expand Down Expand Up @@ -373,6 +417,20 @@ public class NotesTextProcessor {
NotesTextProcessor.highlightCode(attributedString: attributedString, range: r.range)
}
)

// let codeTextProcessor = CodeTextProcessor(textStorage: attributedString)
// if let codeBlockRanges = codeTextProcessor.getCodeBlockRanges() {
//
// for range in codeBlockRanges {
//
// if isIntersect(fencedRanges: fencedRanges, indentRange: range) {
// continue
// }
//
// NotesTextProcessor.highlightCode(attributedString: attributedString, range: range)
// }
// }

}

public static func isIntersect(fencedRanges: [NSRange], indentRange: NSRange) -> Bool {
Expand Down
Loading

0 comments on commit 6eb1d34

Please sign in to comment.