Serenity Operating System
at master 195 lines 5.1 kB view raw
1/* 2 * Copyright (c) 2020, the SerenityOS developers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "Cell.h" 8#include "Spreadsheet.h" 9#include <AK/StringBuilder.h> 10#include <AK/TemporaryChange.h> 11 12namespace Spreadsheet { 13 14void Cell::set_data(DeprecatedString new_data) 15{ 16 // If we are a formula, we do not save the beginning '=', if the new_data is "" we can simply change our kind 17 if (m_kind == Formula && m_data.is_empty() && new_data.is_empty()) { 18 m_kind = LiteralString; 19 return; 20 } 21 22 if (m_data == new_data) 23 return; 24 25 if (new_data.starts_with('=')) { 26 new_data = new_data.substring(1, new_data.length() - 1); 27 m_kind = Formula; 28 } else { 29 m_kind = LiteralString; 30 } 31 32 m_data = move(new_data); 33 m_dirty = true; 34 m_evaluated_externally = false; 35} 36 37void Cell::set_data(JS::Value new_data) 38{ 39 m_dirty = true; 40 m_evaluated_externally = true; 41 42 StringBuilder builder; 43 44 builder.append(new_data.to_string_without_side_effects().release_value_but_fixme_should_propagate_errors()); 45 m_data = builder.to_deprecated_string(); 46 47 m_evaluated_data = move(new_data); 48} 49 50void Cell::set_type(CellType const* type) 51{ 52 m_type = type; 53} 54 55void Cell::set_type(StringView name) 56{ 57 auto* cell_type = CellType::get_by_name(name); 58 if (cell_type) { 59 return set_type(cell_type); 60 } 61 62 VERIFY_NOT_REACHED(); 63} 64 65void Cell::set_type_metadata(CellTypeMetadata&& metadata) 66{ 67 m_type_metadata = move(metadata); 68} 69 70CellType const& Cell::type() const 71{ 72 if (m_type) 73 return *m_type; 74 75 if (m_kind == LiteralString) { 76 if (m_data.to_int().has_value()) 77 return *CellType::get_by_name("Numeric"sv); 78 } 79 80 return *CellType::get_by_name("Identity"sv); 81} 82 83JS::ThrowCompletionOr<DeprecatedString> Cell::typed_display() const 84{ 85 return type().display(const_cast<Cell&>(*this), m_type_metadata); 86} 87 88JS::ThrowCompletionOr<JS::Value> Cell::typed_js_data() const 89{ 90 return type().js_value(const_cast<Cell&>(*this), m_type_metadata); 91} 92 93void Cell::update_data(Badge<Sheet>) 94{ 95 TemporaryChange cell_change { m_sheet->current_evaluated_cell(), this }; 96 if (!m_dirty) 97 return; 98 99 if (m_dirty) { 100 m_dirty = false; 101 if (m_kind == Formula) { 102 if (!m_evaluated_externally) { 103 auto value_or_error = m_sheet->evaluate(m_data, this); 104 if (value_or_error.is_error()) { 105 m_evaluated_data = JS::js_undefined(); 106 m_thrown_value = *value_or_error.release_error().release_value(); 107 } else { 108 m_evaluated_data = value_or_error.release_value(); 109 m_thrown_value = {}; 110 } 111 } 112 } 113 114 for (auto& ref : m_referencing_cells) { 115 if (ref) { 116 ref->m_dirty = true; 117 ref->update(); 118 } 119 } 120 } 121 122 m_evaluated_formats.background_color.clear(); 123 m_evaluated_formats.foreground_color.clear(); 124 if (m_thrown_value.is_empty()) { 125 for (auto& fmt : m_conditional_formats) { 126 if (!fmt.condition.is_empty()) { 127 auto value_or_error = m_sheet->evaluate(fmt.condition, this); 128 if (value_or_error.is_error()) { 129 m_thrown_value = *value_or_error.release_error().release_value(); 130 } else { 131 if (value_or_error.release_value().to_boolean()) { 132 if (fmt.background_color.has_value()) 133 m_evaluated_formats.background_color = fmt.background_color; 134 if (fmt.foreground_color.has_value()) 135 m_evaluated_formats.foreground_color = fmt.foreground_color; 136 } 137 } 138 } 139 } 140 } 141} 142 143void Cell::update() 144{ 145 m_sheet->update(*this); 146} 147 148JS::Value Cell::js_data() 149{ 150 if (m_dirty) 151 update(); 152 153 if (m_kind == Formula) 154 return m_evaluated_data; 155 156 auto& vm = m_sheet->interpreter().vm(); 157 return JS::PrimitiveString::create(vm, m_data); 158} 159 160DeprecatedString Cell::source() const 161{ 162 StringBuilder builder; 163 if (m_kind == Formula) 164 builder.append('='); 165 builder.append(m_data); 166 return builder.to_deprecated_string(); 167} 168 169// FIXME: Find a better way to figure out dependencies 170void Cell::reference_from(Cell* other) 171{ 172 if (!other || other == this) 173 return; 174 175 if (!m_referencing_cells.find_if([other](auto const& ptr) { return ptr.ptr() == other; }).is_end()) 176 return; 177 178 m_referencing_cells.append(other->make_weak_ptr()); 179} 180 181void Cell::copy_from(Cell const& other) 182{ 183 m_dirty = true; 184 m_evaluated_externally = other.m_evaluated_externally; 185 m_data = other.m_data; 186 m_evaluated_data = other.m_evaluated_data; 187 m_kind = other.m_kind; 188 m_type = other.m_type; 189 m_type_metadata = other.m_type_metadata; 190 m_conditional_formats = other.m_conditional_formats; 191 m_evaluated_formats = other.m_evaluated_formats; 192 m_thrown_value = other.m_thrown_value; 193} 194 195}