diff --git a/rhino/panel_base.py b/rhino/panel_base.py index c839bf0..f7f990f 100644 --- a/rhino/panel_base.py +++ b/rhino/panel_base.py @@ -367,7 +367,8 @@ def attach_webview(panel, bridge, mode): # --- Satelliten-Fenster (echtes Rhino-Fenster mit eingebetteter WebView) ---- def open_satellite_window(mode, params=None, title=None, size=(420, 560), - on_save=None, on_cancel=None, bridge=None): + on_save=None, on_cancel=None, bridge=None, + topmost=False): """Oeffnet ein echtes Rhino-Fenster (Eto.Form) mit eingebetteter WebView. Die WebView laedt die React-App mit dem gegebenen `mode` und `params`. @@ -389,7 +390,7 @@ def open_satellite_window(mode, params=None, title=None, size=(420, 560), form.ClientSize = drawing.Size(int(size[0]), int(size[1])) except Exception: pass form.Resizable = True - form.Topmost = False + form.Topmost = bool(topmost) wv = forms.WebView() diff --git a/rhino/text_create.py b/rhino/text_create.py index 0a22040..4a461c4 100644 --- a/rhino/text_create.py +++ b/rhino/text_create.py @@ -862,69 +862,21 @@ def read_selection_settings(doc): def create_text(): - """DOSSIER Custom Text-Workflow: + """DOSSIER Custom Text-Workflow (React WYSIWYG-Editor): 1. Frame ziehen (Live-Rechteck-Vorschau) - 2. _dossier_text_editor (eigener Editor mit Toolbar, Sonderzeichen, - Farbe, Sub/Superscript) oeffnet sich neben dem Frame - 3. TextEntity wird im Frame mit allen gewaehlten Settings erstellt - und mit UserString "dossier_text=1" getagged (fuer evtl. spaeteren - Double-Click-Hook auf unseren Editor) + 2. text_editor.open_with_frame oeffnet das React-WYSIWYG-Fenster + (Topmost, neben dem Frame). Editor handlet die Eingabe + erstellt + die TextEntity bei COMMIT. """ - import System doc = Rhino.RhinoDoc.ActiveDoc if doc is None: return - settings = load_settings(doc) - fonts = available_fonts() frame = _pick_text_frame() if frame is None: return p1, p2, origin, width, height = frame - new_state = _dossier_text_editor(p1, p2, settings, fonts) - if not new_state: return - text = (new_state.get("text") or "").strip() - if not text: return - - # Defaults aus Editor uebernehmen (ohne color) - save_settings(doc, { - "font": new_state.get("font"), - "size": new_state.get("size"), - "bold": new_state.get("bold"), - "italic": new_state.get("italic"), - "underline": new_state.get("underline"), - "align": new_state.get("align"), - }) - try: - te = rg.TextEntity() - te.Plane = rg.Plane(origin, rg.Vector3d.ZAxis) - te.PlainText = text - try: te.TextHeight = float(new_state.get("size") or 0.2) - except Exception: pass - _apply_font(te, new_state.get("font") or "Helvetica", - new_state.get("bold"), new_state.get("italic"), - new_state.get("underline")) - _apply_align(te, new_state.get("align") or "left") - for attr in ("FormatWidth", "TextWidth", "MaskWidth"): - try: setattr(te, attr, width); break - except Exception: pass - try: te.TextIsWrapped = True - except Exception: - try: te.TextWrap = True - except Exception: pass - - # Object-Attribute: Farbe wenn explizit gesetzt + DOSSIER-Tag - attrs = Rhino.DocObjects.ObjectAttributes() - col = new_state.get("color") - if col is not None: - try: - attrs.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject - attrs.ObjectColor = System.Drawing.Color.FromArgb( - int(col[0]), int(col[1]), int(col[2])) - except Exception as ex: - print("[TEXT] color attr:", ex) - attrs.SetUserString("dossier_text", "1") - doc.Objects.AddText(te, attrs) - doc.Views.Redraw() + import text_editor + text_editor.open_with_frame(p1, p2, origin, width, height) except Exception as ex: - print("[TEXT] create:", ex) + print("[TEXT] open editor:", ex) diff --git a/rhino/text_editor.py b/rhino/text_editor.py new file mode 100644 index 0000000..167e1da --- /dev/null +++ b/rhino/text_editor.py @@ -0,0 +1,131 @@ +#! python 3 +# -*- coding: utf-8 -*- +""" +text_editor.py +React-WYSIWYG-Editor in Satellite-WebView (Topmost). User picked Frame +in create_text(), dann oeffnet sich dieser Editor neben dem Frame. +TextEditorBridge haelt Frame-Daten + Settings, auf COMMIT erstellt es +die TextEntity und schliesst das Fenster. +""" +import os +import sys +import Rhino +import Rhino.Geometry as rg +import scriptcontext as sc + +_HERE = os.path.dirname(os.path.abspath(__file__)) +if _HERE not in sys.path: + sys.path.insert(0, _HERE) + +import panel_base +import text_create + + +class TextEditorBridge(panel_base.BaseBridge): + def __init__(self, frame_data, settings, fonts): + panel_base.BaseBridge.__init__(self, "text_editor") + self._frame = frame_data # (origin, width, height, p1, p2) + self._initial_settings = settings + self._fonts = fonts + self._form_ref = None + + def set_form(self, form): + self._form_ref = form + + def _on_ready(self): + self.send("INIT", { + "settings": self._initial_settings, + "fonts": self._fonts, + }) + + def handle(self, data): + if not isinstance(data, dict): return + t = data.get("type", "") + p = data.get("payload") or {} + if t == "READY": + self._on_ready() + elif t == "COMMIT": + self._commit(p) + try: self._form_ref.Close() + except Exception: pass + elif t == "CANCEL": + try: self._form_ref.Close() + except Exception: pass + + def _commit(self, payload): + import System + doc = Rhino.RhinoDoc.ActiveDoc + if doc is None or self._frame is None: return + text = (payload.get("text") or "").strip() + if not text: return + st = payload.get("settings") or {} + origin, width, height, _p1, _p2 = self._frame + + try: + te = rg.TextEntity() + te.Plane = rg.Plane(origin, rg.Vector3d.ZAxis) + te.PlainText = text + try: te.TextHeight = float(st.get("size") or 0.2) + except Exception: pass + text_create._apply_font( + te, + st.get("font") or "Helvetica", + st.get("bold"), st.get("italic"), + st.get("underline")) + text_create._apply_align(te, st.get("align") or "left") + for attr in ("FormatWidth", "TextWidth", "MaskWidth"): + try: + setattr(te, attr, width); break + except Exception: pass + try: te.TextIsWrapped = True + except Exception: + try: te.TextWrap = True + except Exception: pass + + attrs = Rhino.DocObjects.ObjectAttributes() + col = st.get("color") # [r,g,b] oder None + if col is not None and len(col) >= 3: + try: + attrs.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject + attrs.ObjectColor = System.Drawing.Color.FromArgb( + int(col[0]), int(col[1]), int(col[2])) + except Exception as ex: + print("[TEXT-EDITOR] color:", ex) + attrs.SetUserString("dossier_text", "1") + doc.Objects.AddText(te, attrs) + doc.Views.Redraw() + + # Defaults speichern (ohne color) + text_create.save_settings(doc, { + "font": st.get("font"), + "size": st.get("size"), + "bold": st.get("bold"), + "italic": st.get("italic"), + "underline": st.get("underline"), + "align": st.get("align"), + }) + except Exception as ex: + print("[TEXT-EDITOR] commit:", ex) + + +def open_with_frame(p1, p2, origin, width, height): + """Aufgerufen aus text_create.create_text() nach Frame-Pick. + Oeffnet das React-WYSIWYG-Editor-Fenster (Topmost) neben dem Frame. + Non-blocking — Bridge handlet die Eingabe + erstellt TextEntity bei + COMMIT. + """ + doc = Rhino.RhinoDoc.ActiveDoc + settings = text_create.load_settings(doc) + fonts = text_create.available_fonts() + bridge = TextEditorBridge((origin, width, height, p1, p2), + settings, fonts) + sc.sticky["text_editor_bridge"] = bridge + + form = panel_base.open_satellite_window( + "text_editor", + title="Dossier Text", + size=(640, 480), + bridge=bridge, + topmost=True) + if form is not None: + bridge.set_form(form) diff --git a/src/TextEditorApp.jsx b/src/TextEditorApp.jsx new file mode 100644 index 0000000..e86b285 --- /dev/null +++ b/src/TextEditorApp.jsx @@ -0,0 +1,281 @@ +import { useState, useEffect, useRef } from 'react' +import Icon from './components/Icon' +import { onMessage, notifyReady, send } from './lib/rhinoBridge' + +const SYMBOLS = [ + '∅', 'Ø', '⌀', '°', '±', '×', '÷', + '²', '³', '½', '¼', '¾', '⅓', '⅔', + '≤', '≥', '≠', '≈', '∞', '√', '∆', 'π', 'µ', + '←', '→', '↑', '↓', '↔', '↕', + '•', '·', '▪', '◆', '★', '☆', '✓', '✗', + '§', '¶', '©', '®', '™', +] +const SIZE_PRESETS = [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.50, 0.70, 1.00] + +const BAR_H = 22 + +function Pill({ children, onClick, active, disabled, title, style }) { + return ( + + ) +} + +function Dropdown({ value, onChange, options, width, title }) { + return ( +