Comments (2)
CT tools can't be used against nodes directly the DOM node must be converted to a ContentEdit.Node instance. Each ContentEdit.Node class has a class method of fromDOMElement
which can be used to convert a DOM node to a relevant instance, e.g:
const myNode = document.getElementById('my-node');
const myEditableNode = ContentEdit.fromDOMElement(myNode)
ContentTools.Bold.apply(myEditableNode, ContentSelect.Range.query(myNode));
However, the library isn't really designed to be used in this way so I'm not sure the above would be all that useful as a general solution here.
So there's 2 approaches I might recommend - you could either use the default editor but use CSS to make it appear how you want it to, you could changes the tools in the toolbox by updating the value of ContentTools.DEFAULT_TOOLS
(https://github.com/GetmeUK/ContentTools/blob/master/src/scripts/namespace.coffee). This is a fairly simple approach and one I've used before to implement minimal page editors where more appropriate.
However, if you want full control then you need to look at the EditorApp
class (https://github.com/GetmeUK/ContentTools/blob/master/src/scripts/editor.coffee) and basically implement a version of this that uses the UI you want to implement. That would be quite a big task.
If you are looking for really minimal editing features (bold, italic, link) there are libraries better suited I believe such as https://github.com/yabwe/medium-editor
from contenttools.
Thank you for your answer.
I managed to do what I wanted.
First, it seems that div Element are not allowed to be converted in editable Elements. So I added a
element to surround my text.
<button id="tag_b">B</button>
<div id="text_prompt" contenteditable="true" data-editable data-name="main-content"><p>Test test test test</p></div>
To convert my node to an editable node, I tried ContentEdit.fromDOMElement (it doesn't exist), I tried ContentEdit.Static.fromDOMElement (it's not the correct class for what I want) and it worked with ContentEdit.Text.fromDOMElement
When I tried to apply I had this error :
Uncaught TypeError: this._domElement is null
That's because I added the third line :
var myTextNode = document.getElementById('text_prompt').firstChild;
myEditableTextNode = ContentEdit.Text.fromDOMElement(myTextNode);
myEditableTextNode._domElement = myTextNode;
ContentTools.Tools.Bold.apply(myEditableTextNode, ContentSelect.Range.query($('text_prompt')), function(){});
So I think there's a bug when we create an editable element from a DOM element.
from contenttools.
Related Issues (20)
- Save issue - saves fine, but won't allow a second "save" HOT 5
- Making CT compatible with CSP style-src set to 'self'
- Can't edit files that were included or read by php HOT 17
- Edit multiple paragraphs at once. HOT 2
- Is there a way to change the default tag output for normal html tags HOT 2
- Missing translation in /src/scripts/ui/dialogs/table.coffee
- Problem update image classes HOT 5
- Build files to use dynamic import in pure javascript HOT 1
- Hot to keep style attributes on save.
- Support for Sass >= 1.33 HOT 11
- npm install failed HOT 2
- Feature: Support drag & drop of images
- The image uploader HOT 2
- Inspector bar doesn't show embedded HTML elements when wrapped with paragraph HOT 1
- Line breaks not saving
- Add class to a link
- Lazy loading of images
- Source code edit? Insert <script> tag HOT 3
- The part in italics is not the right one HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contenttools.