|
| 1 | += Bundling TinyMCE with a Svelte application |
| 2 | +:navtitle: Using a package manager with bundling |
| 3 | +:description: A guide on bundling TinyMCE with a Svelte application using a module loader. |
| 4 | +:keywords: integration, integrate, svelte, bundle, vite |
| 5 | +:productSource: package-manager |
| 6 | + |
| 7 | +IMPORTANT: {companyname} does not recommend bundling `tinymce` and `@tinymce/tinymce-svelte` with a module loader. Bundling these packages can be complex and error prone. |
| 8 | + |
| 9 | +The https://github.com/tinymce/tinymce-svelte[Official {productname} Svelte component] integrates {productname} into https://svelte.dev/[Svelte applications]. This procedure creates a basic Svelte application containing a bundled {productname} editor. |
| 10 | + |
| 11 | +For examples of the {productname} integration, visit the https://tinymce.github.io/tinymce-svelte/[tinymce-svelte storybook]. |
| 12 | + |
| 13 | +== Prerequisites |
| 14 | + |
| 15 | +Requires https://nodejs.org/[Node.js (and npm)]. |
| 16 | + |
| 17 | +== Procedure |
| 18 | + |
| 19 | +. Use the https://github.com/vitejs/vite[Vite] package to create a new Svelte project named `+tinymce-svelte-demo+`, such as: |
| 20 | ++ |
| 21 | +[source,sh] |
| 22 | +---- |
| 23 | +npm create vite@6 tinymce-svelte-demo -- --template svelte |
| 24 | +---- |
| 25 | +. Change to the project directory. |
| 26 | ++ |
| 27 | +[source,sh] |
| 28 | +---- |
| 29 | +cd tinymce-svelte-demo |
| 30 | +---- |
| 31 | +. Install project dependencies. |
| 32 | ++ |
| 33 | +[source,sh] |
| 34 | +---- |
| 35 | +npm install |
| 36 | +---- |
| 37 | +. Install the `+tinymce+` and `+tinymce-svelte+` packages: |
| 38 | ++ |
| 39 | +[source,sh,subs="attributes+"] |
| 40 | +---- |
| 41 | +npm install tinymce@^{productmajorversion} @tinymce/tinymce-svelte |
| 42 | +---- |
| 43 | + |
| 44 | +include::partial$integrations/premium-plugins-install-step-link.adoc[] |
| 45 | ++ |
| 46 | +[NOTE] |
| 47 | +==== |
| 48 | +For information on configuring premium plugins when bundling, see: xref:bundling-plugins.adoc#using-premium-plugins[Using premium plugins]. |
| 49 | +==== |
| 50 | ++ |
| 51 | +. Using a text editor, create `+src/editor.js+` and set the contents to: |
| 52 | ++ |
| 53 | +[source,js] |
| 54 | +---- |
| 55 | +// Add the @tinymce/tinymce-svelte wrapper to the bundle |
| 56 | +import Editor from '@tinymce/tinymce-svelte'; |
| 57 | +
|
| 58 | +// TinyMCE so the global var exists |
| 59 | +import 'tinymce/tinymce'; |
| 60 | +// DOM model |
| 61 | +import 'tinymce/models/dom/model'; |
| 62 | +// Theme |
| 63 | +import 'tinymce/themes/silver'; |
| 64 | +// Toolbar icons |
| 65 | +import 'tinymce/icons/default'; |
| 66 | +// Editor styles |
| 67 | +import 'tinymce/skins/ui/oxide/skin.min.css'; |
| 68 | +
|
| 69 | +// Import plugins |
| 70 | +import 'tinymce/plugins/advlist'; |
| 71 | +import 'tinymce/plugins/autolink'; |
| 72 | +import 'tinymce/plugins/link'; |
| 73 | +import 'tinymce/plugins/image'; |
| 74 | +import 'tinymce/plugins/lists'; |
| 75 | +import 'tinymce/plugins/table'; |
| 76 | +import 'tinymce/plugins/code'; |
| 77 | +import 'tinymce/plugins/help'; |
| 78 | +import 'tinymce/plugins/wordcount'; |
| 79 | +
|
| 80 | +include::partial$integrations/premium-plugins-bundling.adoc[] |
| 81 | +
|
| 82 | +// Content styles, including inline UI like fake cursors |
| 83 | +import 'tinymce/skins/content/default/content.js'; |
| 84 | +import 'tinymce/skins/ui/oxide/content.js'; |
| 85 | +
|
| 86 | +// Export Editor component for use in Svelte components |
| 87 | +export default Editor; |
| 88 | +---- |
| 89 | ++ |
| 90 | +. Using a text editor, open `+src/App.svelte+` and replace the contents with: |
| 91 | ++ |
| 92 | +[source,svelte] |
| 93 | +---- |
| 94 | +<script> |
| 95 | +import Editor from './editor.js'; |
| 96 | +let content = '<p>This is the initial content of the editor.</p>'; |
| 97 | +let conf = { |
| 98 | + height: 500, |
| 99 | + menubar: false, |
| 100 | + plugins: [ |
| 101 | + 'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', |
| 102 | + 'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen', |
| 103 | + 'insertdatetime', 'media', 'table', 'preview', 'help', 'wordcount' |
| 104 | + ], |
| 105 | + toolbar: 'undo redo | blocks | ' + |
| 106 | + 'bold italic forecolor | alignleft aligncenter ' + |
| 107 | + 'alignright alignjustify | bullist numlist outdent indent | ' + |
| 108 | + 'removeformat | help', |
| 109 | +} |
| 110 | +</script> |
| 111 | +
|
| 112 | +<main> |
| 113 | + <h1>TinyMCE Svelte Demo</h1> |
| 114 | + <Editor |
| 115 | + licenseKey='gpl' |
| 116 | + value={content} |
| 117 | + conf={conf} |
| 118 | + /> |
| 119 | +</main> |
| 120 | +---- |
| 121 | ++ |
| 122 | +. Run the development server: |
| 123 | ++ |
| 124 | +[source,sh] |
| 125 | +---- |
| 126 | +npm run dev |
| 127 | +---- |
| 128 | ++ |
| 129 | +The development server uses Vite to bundle your application on-the-fly. Vite processes all the `import` statements and includes the TinyMCE modules in the development bundle. |
| 130 | + |
| 131 | +. Build the application for production: |
| 132 | ++ |
| 133 | +[source,sh] |
| 134 | +---- |
| 135 | +npm run build |
| 136 | +---- |
| 137 | ++ |
| 138 | +This command creates an optimized production bundle in the `+dist+` directory. |
| 139 | + |
| 140 | +. Preview the production build: |
| 141 | ++ |
| 142 | +[source,sh] |
| 143 | +---- |
| 144 | +npm run preview |
| 145 | +---- |
| 146 | ++ |
| 147 | +. Update the `+licenseKey+` property in the editor component and include your xref:license-key.adoc[License Key]. |
| 148 | + |
| 149 | +== Next Steps |
| 150 | + |
| 151 | +* For information on bundling, see: xref:introduction-to-bundling-tinymce.adoc[] |
| 152 | +* See the following for additional information: |
| 153 | +** https://tinymce.github.io/tinymce-svelte/[tinymce-svelte storybook] |
| 154 | +** xref:basic-setup.adoc[{productname} basic setup] |
| 155 | +** xref:svelte-ref.adoc[{productname} Svelte integration technical reference] |
| 156 | +* For a complete example, see: https://github.com/tinymce/tinymce-svelte-integration-examples/tree/bundle_with_npm_premium_plugins[the Svelte bundling example repository]. |
0 commit comments