mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:27:45 +00:00
Spreadsheet: Add support for example views and hyperlinks in the docs
Now the functions can actually be demonstrated by small examples, embedded right inside the documentation via: spreadsheet://example/<page>#<example_name> Also allows pages to link to each other via the same scheme: spreadsheet://doc/<page>
This commit is contained in:
parent
a598a2c19d
commit
1bd3a2d09f
6 changed files with 310 additions and 23 deletions
|
@ -25,9 +25,12 @@
|
|||
*/
|
||||
|
||||
#include "HelpWindow.h"
|
||||
#include "SpreadsheetWidget.h"
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Frame.h>
|
||||
#include <LibGUI/ListView.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/Splitter.h>
|
||||
#include <LibMarkdown/Document.h>
|
||||
|
@ -97,18 +100,63 @@ HelpWindow::HelpWindow(GUI::Window* parent)
|
|||
m_listview->set_model(HelpListModel::create());
|
||||
|
||||
m_webview = splitter.add<Web::OutOfProcessWebView>();
|
||||
m_webview->on_link_click = [this](auto& url, auto&, auto&&) {
|
||||
ASSERT(url.protocol() == "spreadsheet");
|
||||
if (url.host() == "example") {
|
||||
auto entry = LexicalPath(url.path()).basename();
|
||||
auto doc_option = m_docs.get(entry);
|
||||
if (!doc_option.is_object()) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("No documentation entry found for '{}'", url.path()));
|
||||
return;
|
||||
}
|
||||
auto& doc = doc_option.as_object();
|
||||
const auto& name = url.fragment();
|
||||
|
||||
auto example_data_value = doc.get_or("example_data", JsonObject {});
|
||||
if (!example_data_value.is_object()) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("No example data found for '{}'", url.path()));
|
||||
return;
|
||||
}
|
||||
|
||||
auto& example_data = example_data_value.as_object();
|
||||
auto value = example_data.get(name);
|
||||
if (!value.is_object()) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("Example '{}' not found for '{}'", name, url.path()));
|
||||
return;
|
||||
}
|
||||
|
||||
auto dialog = GUI::Window::construct(this);
|
||||
dialog->resize(size());
|
||||
dialog->set_icon(icon());
|
||||
dialog->set_title(String::formatted("Spreadsheet Help - Example {} for {}", name, entry));
|
||||
auto& widget = dialog->set_main_widget<SpreadsheetWidget>(NonnullRefPtrVector<Sheet> {}, false);
|
||||
auto sheet = Sheet::from_json(value.as_object(), widget.workbook());
|
||||
if (!sheet) {
|
||||
GUI::MessageBox::show_error(this, String::formatted("Corrupted example '{}' in '{}'", name, url.path()));
|
||||
return;
|
||||
}
|
||||
|
||||
widget.add_sheet(sheet.release_nonnull());
|
||||
dialog->show();
|
||||
} else if (url.host() == "doc") {
|
||||
auto entry = LexicalPath(url.path()).basename();
|
||||
m_webview->load(URL::create_with_data("text/html", render(entry)));
|
||||
} else {
|
||||
dbgln("Invalid spreadsheet action domain '{}'", url.host());
|
||||
}
|
||||
};
|
||||
|
||||
m_listview->on_activation = [this](auto& index) {
|
||||
if (!m_webview)
|
||||
return;
|
||||
|
||||
m_webview->load(URL::create_with_data("text/html", render(index)));
|
||||
auto key = static_cast<HelpListModel*>(m_listview->model())->key(index);
|
||||
m_webview->load(URL::create_with_data("text/html", render(key)));
|
||||
};
|
||||
}
|
||||
|
||||
String HelpWindow::render(const GUI::ModelIndex& index)
|
||||
String HelpWindow::render(const StringView& key)
|
||||
{
|
||||
auto key = static_cast<HelpListModel*>(m_listview->model())->key(index);
|
||||
auto doc_option = m_docs.get(key);
|
||||
ASSERT(doc_option.is_object());
|
||||
|
||||
|
@ -158,6 +206,7 @@ String HelpWindow::render(const GUI::ModelIndex& index)
|
|||
if (!examples.is_empty()) {
|
||||
markdown_builder.append("# EXAMPLES\n");
|
||||
examples.for_each_member([&](auto& text, auto& description_value) {
|
||||
dbgln("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
|
||||
markdown_builder.appendff("- {}\n\n```js\n{}\n```\n", description_value.to_string(), text);
|
||||
});
|
||||
}
|
||||
|
@ -176,5 +225,4 @@ void HelpWindow::set_docs(JsonObject&& docs)
|
|||
HelpWindow::~HelpWindow()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public:
|
|||
|
||||
private:
|
||||
static RefPtr<HelpWindow> s_the;
|
||||
String render(const GUI::ModelIndex&);
|
||||
String render(const StringView& key);
|
||||
HelpWindow(GUI::Window* parent = nullptr);
|
||||
|
||||
JsonObject m_docs;
|
||||
|
|
|
@ -100,6 +100,8 @@ public:
|
|||
Cell*& current_evaluated_cell() { return m_current_cell_being_evaluated; }
|
||||
bool has_been_visited(Cell* cell) const { return m_visited_cells_in_update.contains(cell); }
|
||||
|
||||
const Workbook& workbook() const { return m_workbook; }
|
||||
|
||||
private:
|
||||
explicit Sheet(Workbook&);
|
||||
explicit Sheet(const StringView& name, Workbook&);
|
||||
|
|
|
@ -186,10 +186,18 @@ void SpreadsheetWidget::add_sheet()
|
|||
name.append("Sheet");
|
||||
name.appendff(" {}", m_workbook->sheets().size() + 1);
|
||||
|
||||
auto& sheet = m_workbook->add_sheet(name.string_view());
|
||||
NonnullRefPtrVector<Sheet> new_sheets;
|
||||
new_sheets.append(m_workbook->add_sheet(name.string_view()));
|
||||
setup_tabs(move(new_sheets));
|
||||
}
|
||||
|
||||
void SpreadsheetWidget::add_sheet(NonnullRefPtr<Sheet>&& sheet)
|
||||
{
|
||||
ASSERT(m_workbook == &sheet->workbook());
|
||||
|
||||
NonnullRefPtrVector<Sheet> new_sheets;
|
||||
new_sheets.append(sheet);
|
||||
new_sheets.append(move(sheet));
|
||||
m_workbook->sheets().append(new_sheets);
|
||||
setup_tabs(new_sheets);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,11 +42,15 @@ public:
|
|||
void save(const StringView& filename);
|
||||
void load(const StringView& filename);
|
||||
void add_sheet();
|
||||
void add_sheet(NonnullRefPtr<Sheet>&&);
|
||||
|
||||
const String& current_filename() const { return m_workbook->current_filename(); }
|
||||
const Sheet& current_worksheet() const { return m_selected_view->sheet(); }
|
||||
void set_filename(const String& filename);
|
||||
|
||||
Workbook& workbook() { return *m_workbook; }
|
||||
const Workbook& workbook() const { return *m_workbook; }
|
||||
|
||||
private:
|
||||
explicit SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets = {}, bool should_add_sheet_if_empty = true);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue