Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP:color span added. #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/notus/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:notus/notus.dart';
import 'package:quill_delta/quill_delta.dart';

void main() {
// final doc = NotusDocument.fromDelta(Delta.fromJson([{"insert":"Zefyr\n","attributes":{"c":[255, 34, 67, 220]}}]));
final doc = new NotusDocument();
// Modify this document with insert, delete and format operations
doc.insert(
0, 'Notus package provides rich text document model for Zefyr editor');
doc.format(0, 5, NotusAttribute.bold); // Makes first word bold.
doc.format(0, 5, NotusAttribute.color.fromString([255, 34, 67, 220]));
doc.format(0, 0, NotusAttribute.h1); // Makes first line a heading.
doc.delete(23, 10); // Deletes "rich text " segment.

Expand Down
7 changes: 7 additions & 0 deletions packages/notus/lib/src/convert/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NotusMarkdownCodec extends Codec<Delta, String> {
}

class _NotusMarkdownEncoder extends Converter<Delta, String> {
static const kColor = '&&';
static const kBold = '**';
static const kItalic = '_';
static final kSimpleBlocks = <NotusAttribute, String>{
Expand Down Expand Up @@ -160,6 +161,8 @@ class _NotusMarkdownEncoder extends Converter<Delta, String> {
{bool close: false}) {
if (attribute == NotusAttribute.bold) {
_writeBoldTag(buffer);
} else if (attribute == NotusAttribute.color) {
_writeColorTag(buffer);
} else if (attribute == NotusAttribute.italic) {
_writeItalicTag(buffer);
} else if (attribute.key == NotusAttribute.link.key) {
Expand All @@ -177,6 +180,10 @@ class _NotusMarkdownEncoder extends Converter<Delta, String> {
buffer.write(kBold);
}

void _writeColorTag(StringBuffer buffer) {
buffer.write(kColor);
}

void _writeItalicTag(StringBuffer buffer) {
buffer.write(kItalic);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/notus/lib/src/document/attributes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ abstract class NotusAttributeBuilder<T> implements NotusAttributeKey<T> {
/// List of supported attributes:
///
/// * [NotusAttribute.bold]
/// * [NotusAttribute.color]
/// * [NotusAttribute.italic]
/// * [NotusAttribute.link]
/// * [NotusAttribute.heading]
/// * [NotusAttribute.block]
class NotusAttribute<T> implements NotusAttributeBuilder<T> {
static final Map<String, NotusAttributeBuilder> _registry = {
NotusAttribute.bold.key: NotusAttribute.bold,
NotusAttribute.color.key: NotusAttribute.color,
NotusAttribute.italic.key: NotusAttribute.italic,
NotusAttribute.link.key: NotusAttribute.link,
NotusAttribute.heading.key: NotusAttribute.heading,
Expand All @@ -87,6 +89,9 @@ class NotusAttribute<T> implements NotusAttributeBuilder<T> {
/// Italic style attribute.
static const italic = const _ItalicAttribute();

/// Color style attribute.
static const color = const ColorAttributeBuilder._();

/// Link style attribute.
static const link = const LinkAttributeBuilder._();

Expand Down Expand Up @@ -322,6 +327,20 @@ class _ItalicAttribute extends NotusAttribute<bool> {
const _ItalicAttribute() : super._('i', NotusAttributeScope.inline, true);
}


/// Builder for color attribute values.
///
/// There is no need to use this class directly, consider using
/// [NotusAttribute.color] instead.
class ColorAttributeBuilder extends NotusAttributeBuilder<List<int>> {
static const _cLink = 'c';
const ColorAttributeBuilder._() : super._(_cLink, NotusAttributeScope.inline);

/// Creates a color attribute with specified link [value].
NotusAttribute<List<int>> fromString(List<int> value) =>
new NotusAttribute<List<int>>._(key, scope, value);
}

/// Builder for link attribute values.
///
/// There is no need to use this class directly, consider using
Expand Down
17 changes: 16 additions & 1 deletion packages/zefyr/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ Delta getDelta() {
return Delta.fromJson(json.decode(doc));
}

NotusDocument getdocument(){
final doc = new NotusDocument();
// Modify this document with insert, delete and format operations
doc.insert(
0, 'Notus package provides rich text document model for Zefyr editor');
doc.format(0, 5, NotusAttribute.bold); // Makes first word bold.
doc.format(0, 5, NotusAttribute.color.fromString([255,34,67,220])); // Makes first word color.
doc.format(0, 0, NotusAttribute.h1); // Makes first line a heading.
doc.delete(23, 10); // Deletes "rich text " segment.

// Collects style attributes at 1 character in this document.
doc.collectStyle(1, 0); // returned style would include "bold" and "h1".
return doc;
}

class _MyHomePageState extends State<MyHomePage> {
final ZefyrController _controller =
ZefyrController(NotusDocument.fromDelta(getDelta()));
ZefyrController(getdocument());
final FocusNode _focusNode = new FocusNode();
bool _editing = false;

Expand Down
5 changes: 5 additions & 0 deletions packages/zefyr/lib/src/widgets/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ class _RawZefyrLineState extends State<RawZefyrLine> {
if (style.contains(NotusAttribute.link)) {
result = result.merge(theme.linkStyle);
}
if(style.contains(NotusAttribute.color)){
List<int> notusAttribute = style.value(NotusAttribute.color);
TextStyle textStyle=new TextStyle(color: Color.fromARGB(notusAttribute[0], notusAttribute[1], notusAttribute[2], notusAttribute[3]));
result = result.merge(textStyle);
}
return result;
}

Expand Down