memspace / zefyr

Soft and gentle rich text editing for Flutter applications.

Home Page:https://zefyr-editor.gitbook.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add image?

InAnadea opened this issue · comments

Steps to Reproduce

  1. Add image tag to html
  2. Decode html to Notus document
  3. Set document in controller

Initial html:

<p>123<img src=\"https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/best-girl-cat-names-1606245046.jpg?crop=0.668xw:1.00xh;0.126xw,0&resize=640:*\" alt=\"\" width=\"301\" height=\"300\" /></p>

Dependencies:

  zefyr: 1.0.0-rc.4
  notustohtml: 1.0.0-rc.1

Full sample code:

import 'package:flutter/material.dart';
import 'package:notustohtml/notustohtml.dart';
import 'package:zefyr/zefyr.dart';

const _html =
    "<p>123<img src=\"https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/best-girl-cat-names-1606245046.jpg?crop=0.668xw:1.00xh;0.126xw,0&resize=640:*\" alt=\"\" width=\"301\" height=\"300\" /></p>";

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final ZefyrController _controller;
  final _focuseNode = FocusNode();

  final _htmlConverter = const NotusHtmlCodec();

  @override
  void initState() {
    super.initState();
    _initController();
    _controller.addListener(_onChange);
  }

  void _initController() {
    // Delete all comments 
    final preparedHtml = _html.replaceAll('\n', '').replaceAll(
          RegExp(r'(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)'),
          '',
        );
    final delta = _htmlConverter.decoder.convert(preparedHtml);
    final doc = NotusDocument.fromDelta(delta);
    _controller = ZefyrController(doc);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.removeListener(_onChange);
  }

  void _onChange() {
    print(_htmlConverter.encoder.convert(_controller.document.toDelta()));
  }

  @override
  Widget build(BuildContext context) {
    return ZefyrEditor(
      focusNode: _focuseNode,
      controller: _controller,
    );
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(20.0),
          child: Column(
            children: [
              ZefyrToolbar.basic(
                controller: _controller,
                hideHeadingStyle: false,
              ),
              Flexible(
                child: ZefyrEditor(
                  focusNode: _focuseNode,
                  controller: _controller,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Logs

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building TextLine(dirty, dependencies:
[_ZefyrPressedKeysAccess],
state: _TextLineState#bee03):
'package:notus/src/document/line.dart': Failed assertion: line 29 pos 12: 'children.every((child)
=>
child is TextNode)': is not true.

The relevant error-causing widget was:
  TextLine
  TextLine:file:///Users/ivan/.pub-cache/hosted/pub.dartlang.org/zefyr-1.0.0-rc.4/lib/src/widgets
  /editor.dart:1307:19

When the exception was thrown, this was the stack:
#2      LineNode.hasEmbed (package:notus/src/document/line.dart:29:12)
#3      _TextLineState.build (package:zefyr/src/widgets/text_line.dart:122:21)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4705:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4588:15)
#6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4763:11)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)
#8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4566:5)
#9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4754:11)
#10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4561:5)
#11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#12     Element.updateChild (package:flutter/src/widgets/framework.dart:3383:18)
#13     _RenderEditableTextLineElement._mountChild
(package:zefyr/src/widgets/editable_text_line.dart:123:22)
#14     _RenderEditableTextLineElement.mount
(package:zefyr/src/widgets/editable_text_line.dart:136:5)
...     Normal element mounting (7 frames)
#21     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#22     MultiChildRenderObjectElement.inflateWidget
(package:flutter/src/widgets/framework.dart:6261:36)
#23     MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6272:32)
...     Normal element mounting (213 frames)
#236    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#237    MultiChildRenderObjectElement.inflateWidget
(package:flutter/src/widgets/framework.dart:6261:36)
#238    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6272:32)
...     Normal element mounting (173 frames)
#411    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#412    MultiChildRenderObjectElement.inflateWidget
(package:flutter/src/widgets/framework.dart:6261:36)
#413    MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6272:32)
...     Normal element mounting (391 frames)
#804    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)
#805    Element.updateChild (package:flutter/src/widgets/framework.dart:3383:18)
#806    RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)
#807    RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)
#808    RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure>
(package:flutter/src/widgets/binding.dart:1112:18)
#809    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2531:19)
#810    RenderObjectToWidgetAdapter.attachToRenderTree
(package:flutter/src/widgets/binding.dart:1111:13)
#811    WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#812    WidgetsBinding.scheduleAttachRootWidget.<anonymous closure>
(package:flutter/src/widgets/binding.dart:924:7)
(elided 13 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer,
dart:async, and dart:async-patch)

═════════════════════════════════════════════════════════════════════════════════════════════════
═══

Another exception was thrown: type 'RenderErrorBox' is not a subtype of type
'RenderContentProxyBox?' in type cast

Another exception was thrown: type 'RenderErrorBox' is not a subtype of type
'RenderContentProxyBox?' in type cast

Another exception was thrown: A RenderEditor expected a child of type RenderEditableBox but
received a child of type RenderErrorBox.

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion:
line 4313 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.

...

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion:
line 4313 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true. 

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion:
line 4313 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 2535 pos 20: '_debugCurrentBuildTarget == context': is not true.
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:47:61)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2535:20)
#3      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2539:12)
#4      RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#5      WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#6      WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7)
#7      _rootRun (dart:async/zone.dart:1420:47)
#8      _CustomZone.run (dart:async/zone.dart:1328:19)
#9      _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
#10     _Cu<…>
[✓] Flutter (Channel stable, 2.8.1, on macOS 11.5.2 20G95 darwin-arm, locale en-GB)
    • Flutter version 2.8.1 at /Users/ivan/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 77d935af4d (5 weeks ago), 2021-12-16 08:37:33 -0800
    • Engine revision 890a5fca2e
    • Dart version 2.15.1

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0)
    • Android SDK at /Users/ivan/Library/Android/sdk
    • Platform android-32, build-tools 32.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] IntelliJ IDEA Community Edition (version 2021.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.63.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.32.0