I finally figured out a solution to fix the many issues I had with my approach for the editor 😅
Basically, I was using an independent and separate `NSTextView` inside of each block.
For example, if I had two text blocks, it would be two separate `NSTextView`s.
This was causing issues with caret navigation using the arrow up and down keys. The reason was that the textviews weren’t connected in any way, so I had to implement a custom solution. It mostly worked, but the x-position was sometimes off by a few characters.
Another problem was selecting text across multiple blocks. This isn’t possible by default either. I didn’t try to implement a custom solution for this, because I could already see that my approach of using separate textviews was flawed. I would’ve ended up spending a lot of time fixing basic things like text selection not working.
I then thought, _what if I could just use a single textview?_ I realized that using `NSMutableAttributedString` could be useful. I spent all of Sunday working on this, but I finally have a solution that seems pretty solid.
Now all blocks are displayed in the same textview, so selecting text across multiple blocks finally works 🚀
**Next tasks:**
- Get images and videos working using `NSTextAttachment`.
- I also need to get the animation working when collapsing a heading, so that the text moves up and its opacity decreases.
- The pointer cursor does not show correctly when hovering the ellipsis button. It still shows the text selection cursor.
#dev #Swift #AppKit #SwiftUI
In the current approach, I use an `NSTextView` in each block. This means, if you have two text blocks, there are going to be two separate `NSTextView`s.
This is problematic, as it’s really difficult to make keyboard navigation feel natural when navigating the caret between the two text blocks.
You know how when you use arrow up and down keys to navigate in some text, the x-position is remembered to ensure the caret stays in the same position on the horizontal axis.
I do have a solution that mostly work, but I have a bug where _(I think)_ it resets the x-position on each up and down click. Shouldn’t be too difficult to fix.
**However, there is another problematic issue:**
Another problem is text selection. Selecting text across two different `NSTextView`s must be faked, as it does not work by default, so I need to implement a custom solution.
**I see two options:**
- I can continue with the current approach of using separate `NSTextView`s for each block, but this requires much debugging and custom solutions. Maybe I will never be able to get it perfect 🤷♂️
- Or, I can pivot and use one single NSTextView which is apparently what Apple Notes is doing.
I’m not super familiar with `NSMutableAttributedString` and `NSAttributedString`, but apparently these allow for custom styling inside the same text string. For example, you can have a line with a bigger font size and then a line below with a smaller font size inside the same text paragraph.
You can even add attachments inside the text with `NSTextAttachment`. I think this should also work for checkboxes.
I will experiment with this new approach over the weekend. Hopefully, I can get something working. #dev #macOS #Swift #AppKit
View quoted note →
View quoted note →