Commit f8bc1c13 authored by jan.koester's avatar jan.koester
Browse files

editor beta

parent 2b2bfd94
Loading
Loading
Loading
Loading
+1314 −37

File changed.

Preview size limit exceeded, changes collapsed.

+1 −0
Changes for data/template/default/index.html: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
       </div>
       <div id="main">
         <!-- content -->
         <div id="WebEditor"></div>
       </div>
       <div id="ghost"> 
       </div>
+1 −0
Changes for data/template/default/mobile.html: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
    </div>
    <div id="main">
        <!-- content -->
        <div id="WebEditor"></div>
    </div>
    <div id="ghost">
    </div>
+147 −0
Changes for data/template/default/webedit/default.xml: 147 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Blogi WebEditor XML Template Format
    ====================================
    
    Root element: <WebEditor Version="1.0" editor="blogi">
    
    Each child element corresponds to a widget plugin.
    The element name matches the plugin name (case-insensitive).
    Attributes store widget properties, nested elements store children.
    
    Available widgets:
      - Container: Layout wrapper (can have children)
      - Grid:      CSS Grid layout (can have children)
      - TextBox:   Rich text content (leaf widget)
      - Table:     HTML table with configurable rows/columns (leaf widget)
      - Image:     Image with optional link (leaf widget)
      - Button:    Styled link button (leaf widget)
-->
<WebEditor Version="1.0" editor="blogi">
    
    <!-- Header section with a grid layout -->
    <Container background_color="#f8f9fa">
        <Grid columns="1fr" rows="auto" gap="0" padding="20px 40px">
            <TextBox font_family="Arial, sans-serif"
                     font_size="32px"
                     font_color="#333333"
                     text_align="center"
                     font_weight="bold"
                     font_style="normal">Welcome to My Blog</TextBox>
        </Grid>
    </Container>

    <!-- Navigation bar -->
    <Container background_color="#343a40">
        <Grid columns="auto auto auto auto" rows="auto" gap="10px" padding="10px 40px">
            <Button label="Home"
                    link_url="/"
                    background_color="transparent"
                    text_color="#ffffff"
                    font_size="14px"
                    padding="8px 16px"
                    border_radius="4px"
                    border_style="1px solid #6c757d"
                    text_align="center"
                    open_new_tab="false" />
            <Button label="About"
                    link_url="/about"
                    background_color="transparent"
                    text_color="#ffffff"
                    font_size="14px"
                    padding="8px 16px"
                    border_radius="4px"
                    border_style="1px solid #6c757d"
                    text_align="center"
                    open_new_tab="false" />
            <Button label="Blog"
                    link_url="/blog"
                    background_color="transparent"
                    text_color="#ffffff"
                    font_size="14px"
                    padding="8px 16px"
                    border_radius="4px"
                    border_style="1px solid #6c757d"
                    text_align="center"
                    open_new_tab="false" />
            <Button label="Contact"
                    link_url="/contact"
                    background_color="transparent"
                    text_color="#ffffff"
                    font_size="14px"
                    padding="8px 16px"
                    border_radius="4px"
                    border_style="1px solid #6c757d"
                    text_align="center"
                    open_new_tab="false" />
        </Grid>
    </Container>

    <!-- Hero section with image -->
    <Container background_color="#ffffff">
        <Grid columns="1fr 1fr" rows="auto" gap="30px" padding="40px">
            <TextBox font_family="Georgia, serif"
                     font_size="18px"
                     font_color="#555555"
                     text_align="left"
                     font_weight="normal"
                     font_style="normal">This is a sample blog page built with the Blogi WebEditor. You can customize every widget using the editor interface or by modifying this XML template directly.</TextBox>
            <Image src="/media/hero.jpg"
                   alt="Hero Image"
                   width="100%"
                   height="300px"
                   object_fit="cover"
                   border_radius="8px"
                   link_url="" />
        </Grid>
    </Container>

    <!-- Content section with a table -->
    <Container background_color="#f8f9fa">
        <Grid columns="1fr" rows="auto" gap="20px" padding="40px">
            <TextBox font_family="Arial, sans-serif"
                     font_size="24px"
                     font_color="#333333"
                     text_align="left"
                     font_weight="bold">Recent Posts</TextBox>
            <Table num_rows="4" num_cols="3"
                   has_header="true"
                   border_style="1px solid #dee2e6"
                   background_color="#ffffff"
                   cell_padding="12px">
                <Row index="0">
                    <Cell col="0">Title</Cell>
                    <Cell col="1">Date</Cell>
                    <Cell col="2">Category</Cell>
                </Row>
                <Row index="1">
                    <Cell col="0">Getting Started with Blogi</Cell>
                    <Cell col="1">2025-01-15</Cell>
                    <Cell col="2">Tutorial</Cell>
                </Row>
                <Row index="2">
                    <Cell col="0">Widget System Deep Dive</Cell>
                    <Cell col="1">2025-02-01</Cell>
                    <Cell col="2">Development</Cell>
                </Row>
                <Row index="3">
                    <Cell col="0">XML Templates Guide</Cell>
                    <Cell col="1">2025-03-01</Cell>
                    <Cell col="2">Documentation</Cell>
                </Row>
            </Table>
        </Grid>
    </Container>

    <!-- Footer -->
    <Container background_color="#343a40">
        <Grid columns="1fr" rows="auto" gap="0" padding="20px 40px">
            <TextBox font_family="Arial, sans-serif"
                     font_size="14px"
                     font_color="#adb5bd"
                     text_align="center"
                     font_weight="normal">&copy; 2025 My Blog — Powered by Blogi</TextBox>
        </Grid>
    </Container>

</WebEditor>
+123 −0
Changes for plugins/content/content.cpp: 123 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -816,6 +816,129 @@ namespace blogi {

        }

        // --- Publish Interface ---
        bool canPublish() override {
            return true;
        }

        json_object* getPublishForm(const int tid) override {
            json_object *fields = json_object_new_array();

            json_object *f_title = json_object_new_object();
            json_object_object_add(f_title, "key", json_object_new_string("title"));
            json_object_object_add(f_title, "label", json_object_new_string("Title"));
            json_object_object_add(f_title, "type", json_object_new_string("text"));
            json_object_object_add(f_title, "required", json_object_new_boolean(1));
            json_object_object_add(f_title, "placeholder", json_object_new_string("Post title"));
            json_object_array_add(fields, f_title);

            json_object *f_desc = json_object_new_object();
            json_object_object_add(f_desc, "key", json_object_new_string("description"));
            json_object_object_add(f_desc, "label", json_object_new_string("Description"));
            json_object_object_add(f_desc, "type", json_object_new_string("text"));
            json_object_object_add(f_desc, "required", json_object_new_boolean(1));
            json_object_object_add(f_desc, "placeholder", json_object_new_string("Short description"));
            json_object_array_add(fields, f_desc);

            json_object *f_tags = json_object_new_object();
            json_object_object_add(f_tags, "key", json_object_new_string("tags"));
            json_object_object_add(f_tags, "label", json_object_new_string("Tags"));
            json_object_object_add(f_tags, "type", json_object_new_string("text"));
            json_object_object_add(f_tags, "placeholder", json_object_new_string("tag1 tag2 tag3"));
            json_object_array_add(fields, f_tags);

            return fields;
        }

        void Publish(const int tid, const std::string &html, json_object *params, json_object *response) override {
            std::vector<char> sbuf;
            json_object *val_obj = nullptr;

            std::string title, description, tags;

            if (params) {
                if (json_object_object_get_ex(params, "title", &val_obj)) {
                    const char *v = json_object_get_string(val_obj);
                    if (v) title = v;
                }
                if (json_object_object_get_ex(params, "description", &val_obj)) {
                    const char *v = json_object_get_string(val_obj);
                    if (v) description = v;
                }
                if (json_object_object_get_ex(params, "tags", &val_obj)) {
                    const char *v = json_object_get_string(val_obj);
                    if (v) tags = v;
                }
            }

            if (title.empty() || description.empty()) {
                json_object_object_add(response, "status", json_object_new_string("error"));
                json_object_object_add(response, "message", json_object_new_string("Title and description are required."));
                return;
            }

            try {
                std::string time_str;
                auto now = std::chrono::system_clock::now();
                try {
                    time_str = std::format("{:%Y-%m-%d}", now);
                } catch (...) {
                    time_str = "1970-01-01";
                }

                dbpp::SQL sql;
                dbpp::DBResult res;

                // Use author=1 as default (admin) since we don't have request context here
                sql << "INSERT INTO content (title,descrition,text,author,created) VALUES ('"
                    << dbpp::SQL::escaped(sbuf, title.c_str()) << "','"
                    << dbpp::SQL::escaped(sbuf, description.c_str()) << "','"
                    << dbpp::SQL::escaped(sbuf, html.c_str()) << "',1,'"
                    << time_str.c_str() << "') RETURNING id;";

                Args->database[tid]->exec(sql, res);
                int content_id = atoi(res[0][0]);

                // Process tags
                if (!tags.empty()) {
                    size_t start = 0;
                    for (size_t i = 0; i <= tags.size(); ++i) {
                        if (i == tags.size() || tags[i] == ' ') {
                            if (i > start) {
                                std::string tag = tags.substr(start, i - start);
                                // Find or create tag
                                sql.clear();
                                sql << "SELECT id FROM tags WHERE name='" << dbpp::SQL::escaped(sbuf, tag.c_str()) << "' LIMIT 1;";
                                int tc = Args->database[tid]->exec(sql, res);
                                if (tc < 1) {
                                    sql.clear();
                                    sql << "INSERT INTO tags (name) VALUES ('" << dbpp::SQL::escaped(sbuf, tag.c_str()) << "') RETURNING id;";
                                    Args->database[tid]->exec(sql, res);
                                }
                                // Link tag to content
                                std::string tag_id = res[0][0];
                                sql.clear();
                                sql << "INSERT INTO tags_content (content_id,tag_id) VALUES ('" << content_id << "','" << tag_id.c_str() << "');";
                                try { Args->database[tid]->exec(sql, res); } catch (...) {}
                            }
                            start = i + 1;
                        }
                    }
                }

                json_object_object_add(response, "status", json_object_new_string("success"));
                json_object_object_add(response, "message", json_object_new_string("Content published successfully."));
                std::string url = Args->config->getprefix() + "/content/read/" + std::to_string(content_id);
                json_object_object_add(response, "url", json_object_new_string(url.c_str()));
            } catch (std::exception &e) {
                json_object_object_add(response, "status", json_object_new_string("error"));
                json_object_object_add(response, "message", json_object_new_string(e.what()));
            } catch (...) {
                json_object_object_add(response, "status", json_object_new_string("error"));
                json_object_object_add(response, "message", json_object_new_string("Unknown error during content publish."));
            }
        }

    };
};

Loading