Answer the question
In order to leave comments, you need to log in
What is the best way to store and present data?
Good day,
I am writing a document layout system for online publishing. The system is aimed at automatic rendering of text and images, first of all, the emphasis is on the output of text in columns with alignment on the left line, that is, verses.
All data is stored as JSON objects. The structure is built as follows: there is the concept of an object (entity), which can be either a text page or an image page. At the same time, a text page is designed to display text (it contains text and a lot of related parameters, including headings, indents, alignment, and more), although it may have a background behind it (for the convenience of creating documents). An illustration page is intended for cases where text is not needed at all, while such a page can have nested objects. Nested objects are no different from ordinary ones (rendering is performed by the same method), and, in turn, can have their own arrays of nested objects - there is no fundamental restriction on the level of nesting.
Each nested object has a size, coordinates, and corner radius.
At the same time, simplified markup (pseudo-HTML) is supported so that the user can quickly change coordinates and sizes right on the spot (if he does not want to do this by dragging with the mouse), and see what is where. The markup itself is stored as a text field on the parent object, and the child elements are <n pos="x, y" size="w,h" r="r">
. Moreover, at the rendering stage, this markup is inserted into the generated HTML almost as is, after a series of processing with regular expressions, and putting down the necessary style properties.
Everything was quite simple and clear, until the turn came to the implementation of containers.
Containers are elements that provide the layout of the objects nested in them. At the same time, they themselves are not objects (according to the original plan). You can think of them as some kind of layout managers. They, by design, should also turn into plain HTML after processing. Two types of containers were conceived - rows and columns (<row> and <col>). However, a row cannot contain a nested row, and a column cannot contain a nested column. Nesting a column inside a row and vice versa is allowed.
Now I have a function that, after editing the child object (containers are not yet implemented at all), updates the parent's markup, finding the desired fragment and replacing it with a new one. By the way, an important note: the markup of child objects, if any, is not included in the markup of the parent - that is, the text markup "serves" only direct children (and containers).
Turning to containers, I need, firstly, to implement the creation of a container with a given number of cells (let it be image pages by default), change the type of an arbitrary cell, change the order of cells (movement), and change the dimensions of cells (everything is trivial here ). Also, you may need functions for extracting an object from all containers to the very top and placing an object in a specified container (with extraction from the current,
I started trying to implement it. Wrote a function that bypasses all the markup and builds a flat list of all containers (in case, so that later it would be easy to implement the "place in container" function with an arbitrary destination). At the same time, for each container, the list of its descendants is successfully saved, including the containers nested in it. The indexes of the beginning and end of the markup of this container are also stored in the line containing the markup of the current page.
Further, I had a number of questions, and I do not know in which direction it is better to move on.
1. With the current model, each change in the type of a cell inside a container from a container to a non-container will require adding or removing an element from the array of child objects, as well as replacing a piece of markup. At the same time, I would like to avoid a situation where the order of the elements will be violated (for example, such
<row split="1,1,1"><col split="1,1"><1><2></col><3><6></row><4><5>
). Therefore, it may be necessary to completely replace all the numbers in the markup and rebuild the array of objects to ensure the correct order. <4><1><2><3>
is a dubious advantage, I don't even know why it might be needed.<obj pos="x,y" size="w,h" r="r">
. This will immediately remove a lot of problems during development and debugging, and the user will be able to change the contents of the blocks without any buttons by cutting-paste markup fragments. Quite original, although not particularly visual and familiar, as for me.Answer the question
In order to leave comments, you need to log in
I answer myself: indeed, the easiest way turned out to be to store lists of integers in a separate field for each container. When deleting and adding, you just need to bypass all containers and adjust numbers that are greater than the desired value by one up or down.
In addition, in JS, removing an element from an array is done with one call to splice (), so with the "array rebuilding" I certainly turned down here.
Refused to markup, coordinate data is now stored in a nested layout object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question