Text editor base?

Hello,

I think youre almost as crazy as people who want to make their own OS :-).
I believe there are actually libraries out there that already provide text editing functionality.

But… if they forced me to do it myself, I would probably break things down to the smallest, inseparable unit: a character and create different classes.

A page class that covers all parameters for a page. The page class then has its own line class array.
There you handle line stuff. e.g. if the text in the line is too long, shove text to the next line.
Lines contain words -> e.g. to breaking words in two parts like “break- ing”
Words consist again of characters…

im not sure which would be the better direction. if you have a long string of text and break it down into characters, you can create “words” by assembling characters, then create lines by assembling words and so on.

that would make sense if you want to select a certain word or number of chars. -> the line object can detect your mouse positions and “collect” the appropriate words / characters and change their color to signify selection.

yet, again, to figure out sizes of lines, it seems to me that it would be interesting to fill a line with chars (including blanks). so that would be a top down creation, not vice versa.

maybe it could work if you have only two classes. a page and a char class.
the page class handles the positioning of the characters as well as selecting them, holding formatting information as well. it contains an array of all characters on that page and tells each of them if they are selected or not. or if some of them should be bold or normal style, and so on.

lines or words do not necessarily need to be a class of its own. the page class can store them in different lists, so when you want to do spell-checking you know which group of characters make a word…

the more I think of it - without testing - I think the last approach could indeed work well.

class Page () {
//------------------------------- variables
String fullText;
Character [] chars;
//------------------------------- constructor
Page () {
// create a character array with the size of all chars in the text
// fill them with each character
}
//------------------------------- methods
// stuff like detecting collision for selecting text
// create Lines and words
// do stuff like formatting or spell checking
// write stuff -> update the character collection (maybe using an ArrayList rather than an array - but I dislike ArrayLists :-))
}
class Character () {
// what's my format
// what's my position on screen
// am I selected
// what's my width and size
}
1 Like