Skip to content
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
193 changes: 111 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@ Given below is the detailed guide and helpful resources to get started with our

## Prerequisite

Latest Android Studio or IntelliJ IDEA or Visual Studio Code
You need the latest version of either Android Studio, IntelliJ IDEA, or VS Code for this

- Setup and Installation

- We need to install dart
- [Setup and Installation](https://dart.dev/get-dart)
- [Latest version of Dart](https://github.com/dart-lang/sdk/tags)
- Latest version of Android Studio or IntelliJ IDEA or Visual Studio Code
- If using vscode, install the dart and flutter plugin extensions

## Dependency

Add the following to your pom.xml file:
Add the following package to your pubspec.yaml. use [latest](https://pub.dev/packages/contentstack_utils) version of package

```dart
contentstack-util: any
contentstack_utils: any
```

Note: If you are using Contentstack Dart SDK we have already imported contentstack-utils into it.

```dart
contentstack: any
contentstack: any
```

## Usage
Expand All @@ -36,100 +37,128 @@ Create Render Option:
To render Embedded objects within RTE create renderOption as follows:

```dart
Utils.renderContents(rteArray, localJsonObj, (embeddedObject, metadata) -> {
switch (metadata.getStyleType()) {
case BLOCK:
String title = embeddedObject.getString("title");
String multi_line = embeddedObject.getString("multi_line");
return "<p>" + title + "</p><span>" + multi_line + "</span>";
case INLINE:
String titleInline = embeddedObject.getString("title");
String mlInline = embeddedObject.getString("multi_line");
return "<p>" + titleInline + "</p><span>" + mlInline + "</span>";
case LINKED:
String titleLinked = embeddedObject.getString("title");
String mlLinked = embeddedObject.getString("multi_line");
return "<p>" + titleLinked + "</p><span>" + mlLinked + "</span>";
case DISPLAY:
String titleDiplayable = embeddedObject.getString("title");
String mlDiplayable = embeddedObject.getString("multi_line");
return "<p>" + titleDiplayable + "</p><span>" + mlDiplayable + "</span>";
default:
return null;
}
});
import 'package:contentstack_utils/src/helper/Metadata.dart';
import 'package:contentstack_utils/src/model/Option.dart';

class OptionDemo implements Option {
@override
String renderMark(String markType, String renderText) {
// TODO: implement renderMark
switch (markType) {
case 'bold':
return '<b>' + renderText + '</b>';
break;
default:
return '';
}
}

@override
String renderNode(nodeType, Map node_obj, callback) {
// TODO: implement renderNode
switch (nodeType) {
case 'paragraph':
String children = callback.renderChildren(node_obj['children']);
return "<p class='class-id'>" + children + '</p>';
break;
default:
return '';
}
}

@override
String renderOption(Map obj, Metadata metadata) {
switch (metadata.getStyleType) {
case 'block':
return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>';
break;
case 'inline':
return '<p>' + obj['title'] + '</p><span>' + obj['line'] + '</span>';
break;
case 'link':
return '<p>' + obj['title'] + '</p><span>' + obj['key'] + '</span>';
break;
case 'display':
return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>';
break;
default:
return '';
}
}
}

```

## Using Contentstack Utils from Contentstack Dart SDK
### Using Contentstack Utils from Contentstack Dart SDK

### Fetch entry/entries including embedded using Contentstack SDK
#### Fetch entry/entries including embedded using Contentstack SDK

```dart
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final entry = stack.contentType('contentTypeUid').entry(entryUid: 'entryUid');
entry..includeEmbeddedItems();
await entry.fetch().then((response) {
const keyPath = [ "rich_text_editor", "global_rich_multiple.group.rich_text_editor"]
final jsonObject = response['entry'];
Utils.render(jsonObject, keyPath, Option);
}).catchError((error) {
print(error.message.toString());
});
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final entry = stack.contentType('contentTypeUid').entry(entryUid: 'entryUid');
entry..includeEmbeddedItems();
await entry.fetch().then((response) {
const keyPath = [ "rich_text_editor", "global_rich_multiple.group.rich_text_editor"]
final jsonObject = response['entry'];
Utils.render(jsonObject, keyPath, Option);
}).catchError((error) {
print(error.message.toString());
});
```

### Fetch multiple entries including embedded object and render RTE fields
#### Fetch multiple entries including embedded object and render RTE fields

```dart
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor", "global_rich_multiple.group.rich_text_editor"]
entries.forEach((entry){
Utils.render(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor", "global_rich_multiple.group.rich_text_editor"]
entries.forEach((entry){
Utils.render(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
```

### Supercharged (SRTE)
##### Supercharged (SRTE)

```dart
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor"]
entries.forEach((entry){
Utils.jsonToHtml(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor"]
entries.forEach((entry){
Utils.jsonToHtml(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
```

### GraphQl SRTE
##### GraphQl Supercharged RTE

```dart
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor"]
entries.forEach((entry){
GQL.jsonToHtml(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
import 'package:contentstack/contentstack.dart' as contentstack;
final stack = contentstack.Stack(apiKey, deliveryToken, environment);
final query = stack.contentType('contentTypeUid').entry().query();
await query.find().then((response) {
var entries = response['entries'];
const keyPath = ["rich_text_editor"]
entries.forEach((entry){
GQL.jsonToHtml(entry, keyPath, Option);
})
}).catchError((error) {
print(error.message.toString());
});
```

## Features and bugs
### Features and bugs

Please file feature requests and bugs at the [issue tracker][tracker].

Expand Down
2 changes: 1 addition & 1 deletion coverage_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ dev_dependencies:
pedantic: ^1.11.1
test: ^1.17.5
# test_coverage: ^0.4.1

49 changes: 49 additions & 0 deletions test/options.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:contentstack_utils/src/helper/Metadata.dart';
import 'package:contentstack_utils/src/model/Option.dart';

class OptionDemo implements Option {
@override
String renderMark(String markType, String renderText) {
// TODO: implement renderMark
switch (markType) {
case 'bold':
return '<b>' + renderText + '</b>';
break;
default:
return '';
}
}

@override
String renderNode(nodeType, Map node_obj, callback) {
// TODO: implement renderNode
switch (nodeType) {
case 'paragraph':
String children = callback.renderChildren(node_obj['children']);
return "<p class='class-id'>" + children + '</p>';
break;
default:
return '';
}
}

@override
String renderOption(Map obj, Metadata metadata) {
switch (metadata.getStyleType) {
case 'block':
return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>';
break;
case 'inline':
return '<p>' + obj['title'] + '</p><span>' + obj['line'] + '</span>';
break;
case 'link':
return '<p>' + obj['title'] + '</p><span>' + obj['key'] + '</span>';
break;
case 'display':
return '<p>' + obj['title'] + '</p><span>' + obj['multi'] + '</span>';
break;
default:
return '';
}
}
}