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

Automatically start Trix on the next tick after import #1008

Merged
merged 1 commit into from
Nov 3, 2022
Merged
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
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ Trix is built with established web standards, notably [Custom Elements](https://

Trix comes bundled in ESM and UMD formats and works with any asset packaging system.

The easiest way to start with Trix is including it from an npm CDN in the `<head>` of your page and then calling `Trix.start()` to initialize the library:
The easiest way to start with Trix is including it from an npm CDN in the `<head>` of your page:

```html
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/trix@2.0.0-beta.0/dist/trix.css">
<script type="text/javascript" src="https://unpkg.com/trix@2.0.0-beta.0/dist/trix.umd.min.js"></script>
<script type="text/javascript">
Trix.start()
</script>
</head>
```

Expand All @@ -43,9 +40,9 @@ Alternatively, you can install the npm package and import it in your application
```js
import Trix from "trix"

// Change Trix.config if you need

Trix.start()
document.addEventListener("trix-before-initialize", () => {
// Change Trix.config if you need
})
```

## Creating an Editor
Expand Down Expand Up @@ -354,7 +351,7 @@ element.editor.loadJSON(JSON.parse(localStorage["editorState"]))

The `<trix-editor>` element emits several events which you can use to observe and respond to changes in editor state.

* `trix-before-initialize` fires when the `<trix-editor>` element is attached to the DOM just before Trix installs its `editor` object.
* `trix-before-initialize` fires when the `<trix-editor>` element is attached to the DOM just before Trix installs its `editor` object. If you need to use a custom Trix configuration you can change `Trix.config` here.

* `trix-initialize` fires when the `<trix-editor>` element is attached to the DOM and its `editor` object is ready for use.

Expand Down
2 changes: 0 additions & 2 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@
}, 30)
}
});

Trix.start()
</script>
</head>
<body>
Expand Down
2 changes: 0 additions & 2 deletions src/test/test_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ document.head.insertAdjacentHTML(
#qunit { position: relative !important; }
</style>`
)

Trix.start()
24 changes: 12 additions & 12 deletions src/trix/trix.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import * as operations from "trix/operations"
import * as elements from "trix/elements"
import * as filters from "trix/filters"

let started = false

function start() {
if (!started) {
customElements.define("trix-toolbar", elements.TrixToolbarElement)
customElements.define("trix-editor", elements.TrixEditorElement)
started = true
}
}

const Trix = {
VERSION: version,
config,
Expand All @@ -30,10 +20,20 @@ const Trix = {
observers,
operations,
elements,
filters,
start
filters
}

function start() {
if (!customElements.get("trix-toolbar")) {
customElements.define("trix-toolbar", elements.TrixToolbarElement)
}

if (!customElements.get("trix-editor")) {
customElements.define("trix-editor", elements.TrixEditorElement)
}
}

window.Trix = Trix
setTimeout(start, 0)

export default Trix