Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
adds ace editor example (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Feb 1, 2018
1 parent 51c5ad2 commit a7ed81a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Binary file added docs/assets/ace-editor-extension.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions samples/ace-editor/README.md
@@ -0,0 +1,20 @@
Ace editor
--------------

This extension shows how to implement the powerful [Ace editor](https://ace.c9.io/) as an extension for text fields.

### Installation and usage

Ensure you checked [the samples requirements listed here](../README.md).

![Screenshot of extension](https://github.com/contentful/extensions/raw/master/docs/assets/ace-editor-extension.png)

Install extension for space:

```bash
export SPACE=<id of space you want to install extension for>

contentful extension create --space-id $SPACE
```

From this point on, you can use the [other commands](https://github.com/contentful/contentful-cli/tree/master/docs/extension) the contentful-cli provides for extensions.
69 changes: 69 additions & 0 deletions samples/ace-editor/app.html
@@ -0,0 +1,69 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Ace Editor Extension</title>
<!--
Include styles from the Contentful app.
See http://contentful.github.io/ui-extensions-sdk/styleguide for information on
how to include and apply these styles.
-->
<link rel="stylesheet" href="https://contentful.github.io/ui-extensions-sdk/cf-extension.css">
<style>
body { margin: 0; }
#editor {
min-height: 300px;
}
</style>
<!--
Load the Extensions API that is used to communicate with the containing app.
-->
<script src="https://contentful.github.io/ui-extensions-sdk/cf-extension-api.js"></script>
<!--
We use lodash.throttle to avoid spamming the API with changes
-->
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<!--
Include ace editor core script.
-->
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/ace.js"></script>
<!--
Add editor theme and syntax highlighting mode scripts.
Find more here: https://www.jsdelivr.com/package/npm/ace-builds?path=src-min
-->
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/mode-handlebars.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.2.9/src-min-noconflict/theme-monokai.js"></script>
</head>
<body>
<pre id="editor"></pre>
<script>
// This is the main entry point for extensions.
//
// The extension API reference explains in detail what you can do with
// the 'api' object.
var cfExt = window.contentfulExtension || window.contentfulWidget
cfExt.init(function (api) {
function updateFieldValue () {
var value = editor.getValue()
api.field.setValue(value)
}
// Whenever the size of this document changes we adjust the size of
// the IFrame in the Contentful App.
api.window.startAutoResizer()

// Get the field value from the UI Extension SDK
var value = api.field.getValue() || ''
document.getElementById('editor').innerText = value

// Configure Ace editor
var editor = ace.edit('editor')
editor.setTheme('ace/theme/monokai')
editor.session.setMode('ace/mode/handlebars')

// Listen to change events and fire an update every 500ms
editor.on('change', _.throttle(updateFieldValue, 500, { leading: false }))
})
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions samples/ace-editor/extension.json
@@ -0,0 +1,6 @@
{
"id": "ace-editor",
"name": "Ace editor",
"srcdoc": "./app.html",
"fieldTypes": ["Text"]
}

0 comments on commit a7ed81a

Please sign in to comment.