8
0
Piotrek Koszuliński 8 лет назад
Родитель
Сommit
fe4c7ab1b1

+ 3 - 0
docs/_snippets/basic/integration1.html

@@ -0,0 +1,3 @@
+<div id="editor">
+	<p>This is the editor!</p>
+</div>

+ 24 - 0
docs/_snippets/basic/integration1.js

@@ -0,0 +1,24 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals console, window, document */
+
+import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
+
+import ArticlePreset from '@ckeditor/ckeditor5-presets/src/article';
+
+ClassicEditor.create( document.querySelector( '#editor' ), {
+	plugins: [ ArticlePreset ],
+	toolbar: [ 'headings', 'bold', 'italic', 'link', 'unlink', 'bulletedList', 'numberedList', 'blockQuote', 'undo', 'redo' ],
+	image: {
+		toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ]
+	}
+} )
+.then( editor => {
+	window.editor = editor;
+} )
+.catch( err => {
+	console.error( err.stack );
+} );

+ 20 - 0
scripts/snippet-adapter/package.json

@@ -0,0 +1,20 @@
+{
+  "name": "docs-adapter",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "babili-webpack-plugin": "^0.1.2",
+    "css-loader": "^0.28.4",
+    "node-sass": "^4.5.3",
+    "raw-loader": "^0.5.1",
+    "sass-loader": "^6.0.6",
+    "style-loader": "^0.18.2",
+    "webpack": "^3.0.0"
+  }
+}

+ 97 - 0
scripts/snippet-adapter/snippetadapter.js

@@ -0,0 +1,97 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* eslint-env node */
+
+const path = require( 'path' );
+const fs = require( 'fs' );
+const webpack = require( 'webpack' );
+const { bundler } = require( '@ckeditor/ckeditor5-dev-utils' );
+
+const BabiliPlugin = require( 'babili-webpack-plugin' );
+
+module.exports = function snippetAdapter( data ) {
+	const webpackConfig = getWebpackConfig( {
+		entry: data.snippetSource.js,
+		outputPath: path.join( data.outputPath, data.snippetPath )
+	} );
+
+	return runWebpack( webpackConfig )
+		.then( () => {
+			return {
+				html: generateSnippetHtml( {
+					htmlPath: data.snippetSource.html,
+					scriptPath: path.join( data.relativeOutputPath, data.snippetPath, 'snippet.js' )
+				} )
+			};
+		} );
+};
+
+function getWebpackConfig( config ) {
+	return {
+		devtool: 'source-map',
+
+		entry: config.entry,
+
+		output: {
+			path: config.outputPath,
+			filename: 'snippet.js'
+		},
+
+		plugins: [
+			new BabiliPlugin( null, {
+				comments: false
+			} ),
+			new webpack.BannerPlugin( {
+				banner: bundler.getLicenseBanner(),
+				raw: true
+			} )
+		],
+
+		module: {
+			rules: [
+				{
+					test: /\.svg$/,
+					use: [ 'raw-loader' ]
+				},
+				{
+					test: /\.scss$/,
+					use: [
+						'style-loader',
+						{
+							loader: 'css-loader',
+							options: {
+								minimize: true
+							}
+						},
+						'sass-loader'
+					]
+				}
+			]
+		}
+	};
+}
+
+function runWebpack( webpackConfig ) {
+	return new Promise( ( resolve, reject ) => {
+		webpack( webpackConfig, ( err, stats ) => {
+			if ( err ) {
+				reject( err );
+			} else if ( stats.hasErrors() ) {
+				reject( new Error( stats.toString() ) );
+			} else {
+				resolve();
+			}
+		} );
+	} );
+}
+
+function generateSnippetHtml( data ) {
+	let html = fs.readFileSync( data.htmlPath );
+
+	html += `<script src="${ data.scriptPath }"></script>`;
+
+	return html;
+}

+ 28 - 0
scripts/snippet-adapter/test.js

@@ -0,0 +1,28 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* eslint-env node */
+
+const snippetAdapter = require( './snippetadapter' );
+const path = require( 'path' );
+
+snippetAdapter( {
+	snippetSource: {
+		html: path.resolve( __dirname, '../../docs/_snippets/basic/integration1.html' ),
+		js: path.resolve( __dirname, '../../docs/_snippets/basic/integration1.js' )
+	},
+	snippetPath: 'basic/integration1',
+	outputPath: path.resolve( __dirname, '../../build/docs/_snippets' ),
+	relativeOutputPath: '../../_snippets',
+	basePath: '../..'
+} )
+.then( data => {
+	console.log( 'Success!' );
+	console.log( data.html );
+} )
+.catch( err => {
+	console.error( err.stack );
+} );
+