return to table of content

Nerdy internals of an Apple text editor

kccqzy
11 replies
22h18m

Are the internals of other Mac-native text editors like TextMate similar to this as well?

Someone
4 replies
21h57m

Depends on what you call “similar”. They’ll have storage, layout, a part of the layout that’s visible, etc, but they may not separate them that clearly, and won’t have that rich an API. TextView is way more flexible than what the typical _text_ editor needs. Text editors typically don’t need multiple fonts, multiple font sizes, varying line spacing, paragraph indents, proportional fonts, right-aligning or decimal tabs, images inside text, etc.

All those features make TextView relatively slow and memory hungry. Because of that, I don’t think _text_ editors that handle multi-megabyte files will use a single NSAttributedString to store the entire document’s text.

papereditor
3 replies
20h6m

Yep. TextView is a beast.

For multi-megabyte files I always use Sublime.

KerrAvon
1 replies
15h56m

BBEdit is more Mac-like than Sublime and handles multi-gig files with ease.

papereditor
0 replies
8h56m

Indeed, there are a handful of those dev-oriented editors that crunch through mega files with ease. No idea why I prefer Sublime. :D

kccqzy
0 replies
18h54m

And for multi-gigabyte files I use HexFiend.

Ezhik
1 replies
8h39m

Using CodeMirror in a desktop app seems like an interesting choice. I'd love to hear more about why they took this approach.

papereditor
0 replies
7h54m

Web tech is an obvious pragmatic choice. Cross-platform by default, amazing libraries with great support.

The average user does not care.

pvg
0 replies
22h8m

That depends on the extent on which they rely on the system-provided frameworks. When the needs of the editor get specialized enough, all sorts of hackery happens.

yuchi
4 replies
21h37m

In the era of DOM documents (see notion, gitbook) I very often resort to attributed strings to do magic stuff with text parsing and manipulation. It’s such an elegant structure and I don’t understand why is so unknown.

Incredible article btw

LikeAnElephant
1 replies
18h44m

Do you have an example? I’d love to learn more!

yuchi
0 replies
1h47m

Let’s say you have a rich text where you want to highlight all occurrences of some token or syntax. When you have tree data structures you cannot use regexps or a simple parsers, since the full text may be split in different nodes.

With attributes strings it’s trivial, you just need to move attributes accordingly and to normalize stuff at the end.

papereditor
0 replies
20h11m

Thank you!

Ezhik
0 replies
1h16m

I wonder if there are any web rich text editor components that use attributed strings? Tired of the DOM.

jakey_bakey
4 replies
21h2m

The UI of this doc is absolutely beautiful

troupo
1 replies
20h52m

There are several elements that are practically invisible though: play/pause buttons on the sides, comments in parentheses, explanatory text on graphics.

It's a shame because otherwise the presentation is really good.

papereditor
0 replies
20h17m

Thanks!

The play/pause buttons are meant to not disturb the reading. They are only for the rare occasions when you want to pause the video to examine it closer.

Comments can be hovered over to make them fully visible.

The text, yes, should have been more prominent…

papereditor
0 replies
20h20m

Thanks!

Eggs-n-Jakey
0 replies
20h21m

I like your handle, Jake.

soapdog
3 replies
1d4h

I love articles like this.

whartung
1 replies
21h7m

I'd like to see a similar treatise on the Apple Document system. I love that thing, it's a favorite part of all the Apple apps.

jitl
0 replies
14h52m

That makes me super curious about it. I've only had some small interaction while learning SwiftUI and I was confused in passing, and sort of just smashed things until it worked.

papereditor
0 replies
1d4h

Thanks!

Someone
1 replies
2h51m

For iOS 15.0 or later/MacOS Monterey or later, you want to look at TextKit 2. See https://developer.apple.com/documentation/appkit/textkit/usi...

That’s a new layout engine that currently is the default for text fields and on iOS and MacOS, but likely will be the only option some time in the future.

papereditor
0 replies
2h8m

Indeed!

I want to migrate Paper to it this or next year.

papereditor
0 replies
20h31m

That looks like a good article, indeed. I have not seen it before. Thanks!

lilyball
3 replies
20h57m

I'm a little confused at the part about decorative attributes being done outside of the editing transaction, saying "And they are not even aware of the transaction since they live in the NSLayoutManager itself, not in NSTextStorage". Decorative attributes like color do normally live in the NSTextStorage! Is the author implying that the colors applied to markdown characters are being done with NSLayoutManager's temporary attributes support (typically used to color misspelled words)? What would be the purpose of that?

papereditor
2 replies
20h32m

Yes. NSTextStorage can also contain the same attributes as the temporary attributes (rendering attributes in TextKit 2).

The benefit of using temporary attributes is that changing them is very fast compared to invoking the full transaction on NSTextStorage. For example, switching between Light/Dark modes is very fast in Paper.

lilyball
1 replies
20h19m

I would have expected a transaction to track whether any changes affect layout and avoid invalidating layout if only changing rendering attributes. I assume you've actually measured this though, and so I have to infer that this optimization does not in fact happen.

papereditor
0 replies
20h13m

Things might have changed already, but yes, when I measured it there was a noticeable difference.

Ideally, I would have expected it to behave like you have described.

jrvarela56
3 replies
21h30m

This is amazing content marketing. Developers are an obvious target niche for such a product. Exposing internals of the software in a way HNers enjoy sounds like a great idea, would love to see if it worked as a traction experiment.

papereditor
2 replies
20h22m

Last time (https://news.ycombinator.com/item?id=38866170) there was a small spike in sales, but no lasting effect.

I would not say that developers are the target niche. The best customers are professional writers who use a ton of writing apps for different occasions.

glorioushubris
1 replies
17h6m

Professional writer who uses different writing apps for different occasions is me exactly. I’m also the (or at least a) guy who messaged you about inline comments. As soon as all the details of that feature are implemented, I’m buying it.

papereditor
0 replies
8h54m

Nice!

I'll be sure to prioritize this work then. :D

foxandmouse
3 replies
21h47m

I've been loving this app, It's replaced all other markdown apps for me including obsidian and ia Writer!

caycep
1 replies
20h50m

granted, I know Paper is an apt name, but there's at least 2 other iOS apps called "Paper" IIRc...

papereditor
0 replies
20h4m

Yep…

"paper writing app" is a good keyword to search by.

mk12
0 replies
21h16m

Interesting, those are the two main apps I use for writing. Maybe I should check this out.

dkjaudyeqooe
2 replies
21h36m

It's a real shame that Apple doesn't release the source to a recent version of TextEdit. Given its structure (more or less just a stock objects from the API) it would be super educational for anyone who wants to use NSTextView et al.

krzyzanowskim
0 replies
21h20m

For the educational purpose of modern TextKit and NSTextView-like implementation, you can check out https://github.com/krzyzanowskim/STTextView that is modern re-implementation of the text view.

jwells89
0 replies
20h57m

Yeah, you can get pretty close to TextEdit by just dropping an NSTextView into the document window XIB of a document-based Mac app, hooking up the data methods in your Document class to it, and doing a little bit of wiring of menu items to the textview. Text formatting bar, rulers, Find in Page, font panel, etc. There’s a few things missing but not a ton.

The document-based app template gets you a lot for “free” too, including tabs and conversion of windows into tabs and vice versa.

xenodium
1 replies
19h25m

Anyone know of a similarly well crafted app for Android?

papereditor
0 replies
8h59m

iA Writer (https://ia.net/writer) is on Android as well.

userbinator
1 replies
14h30m

The TextEdit app consists almost entirely of a single TextView

I believe WordPad is the Windows equivalent, based on the RichEdit control.

Another fun fact is that RTF is basically the serialized form of NSAttributedString.

The same is true of Windows' RichEdit control. In fact, it looks like Windows' implementation came first: https://en.wikipedia.org/wiki/Rich_Text_Format

papereditor
0 replies
9h0m

Interesting facts!

russellbeattie
1 replies
15h8m

Imagine going through all that effort, and then saving the output in Markdown. It's like finishing your astrophysics PhD and then getting a lobotomy.

Just to repeat my years-long crusade of yelling at clouds, we need a cross platform, Rich Text HTML Document standard.

jitl
0 replies
14h53m

Isn't that "just" HTML? What should be added to HTML?

personjerry
1 replies
20h11m

I wish there was documentation like this for more ios components!

papereditor
0 replies
20h2m

Thanks!

justinzollars
1 replies
19h31m

Awesome description! Thank you!

papereditor
0 replies
8h55m

Thanks!

indemnity
1 replies
18h37m

What a great article (and personally timely, wrangling NSTextViews at the moment).

How did you glean the information needed to write this? Other people’s code? Painful experience? developer.apple.com?

papereditor
0 replies
9h1m

Thanks!

I've been working on Paper for 9 years. I've had a lot of time to study how everything works. :D

dbalatero
1 replies
21h53m

The author did a great job using images to convey the various concerns each piece of code in the API is responsible for. Also super great detail in the writing. Nice work!

This feels like a technical-first article that also happens to generate goodwill for the product, as opposed to a marketing-first technical article that serves up the bare minimum of insights.

papereditor
0 replies
20h28m

Thanks!

Yep, it's a win-win. A good resource plus a bit of marketing for the product.

brcmthrowaway
1 replies
21h26m

Is openGl used here at all for rendering?

papereditor
0 replies
20h29m

I don't use any low-level rendering code.

anonymouse008
1 replies
20h16m

Haha! Love this so much.

Did you ever have to store custom metadata in NSAS to file? Before chatGPT, I had to write NSSecureCoding classes by hand, and that agony will remain in my soul next incarnation.

papereditor
0 replies
20h12m

Thanks!

I am offloading all the storage to NSDocument, so I don't have to deal with stuff like this. :)

Nijikokun
1 replies
21h51m

I clicked "hide these widgets" from the three dot dropdown menu on your animations and now I can't figure out how to bring them back. heh.

Great post btw!

papereditor
0 replies
20h21m

Thanks!

The state is saved to local storage. Just delete the key from DevTools and they will come back. :D

CaesarA
1 replies
20h46m

As someone who has attempted to write their own text editor completely from scratch before, this would've been a tremendous resource to have.

papereditor
0 replies
20h30m

Thank you!

kaichanvong
0 replies
13h7m

casual opinion; To the App developer (Mihhail L/@_mihhail?) themselves, to say: Loved just how it keeps itself, everything-from-the emoji of engineering, engi-peeping, mechanical heading–towards focus on screen/container mixed with data storage.

Never certain how it keeps for scrolling; the issue lies in drag, doomscrolls, scrollbar versus tap-for-scrolling… now, on topic of moving on: “a good OS citizen, you should provide many formats”, not keen on everything/too-many Media Formats keeping it sweet, simple; some of these Apps can become almost utility goto. Some of my favourites, include emoji meets paste formatting issues in export. How do they render correctly the almost, "interstitial flickering" change.

My expectations paused at Themes/Theming (not really happening? different looks) no simple mention of light Vs. dark; willow filter adjustment tones? Okay! Still, amazement. I believe, we choose our (kind of) Paper whenever possible.

…Thanks!

All the kindest of regards, from reading nerdy happenings! Kai V (@kaichanvong)

grishka
0 replies
11h6m

For me, as a long-time Android app developer, it was interesting to see how Apple approaches things somewhat differently and more thoughtfully.

On Android, you basically have the Layout class (and its subclasses) that does everything related to both layout and rendering, and TextView which implements some of the editing/selection logic. The only difference between EditText and TextView is that EditText enables that editing functionality already present in TextView. The problem with this rather monolithic approach (and poor APIs) is that if you need more control over how your app renders text, you're out of luck. Want to get at the individual glyphs after they've been laid out for example? Nope, sorry.

Ezhik
0 replies
8h39m

Oh thank god at least somebody is doing Cocoa in 2024.