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

Command for printing content styles #1805

Merged
merged 20 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
963336a
Introduced "yarn run docs:content-styles" command for priting out con…
pomek Jun 7, 2019
e44db47
Provided more plugins to content-styles build.
pomek Jun 10, 2019
a4bbc00
Merge branch 'master' into t/773
oleq Jul 23, 2019
662811e
Changes absolute paths to relative in content-style.css
pomek Jul 26, 2019
2768620
content-style.css contains variables definitions.
pomek Jul 26, 2019
8391b3a
Merge branch 'master' into t/773
oleq Jul 29, 2019
903a2b0
content-styles.css will include only used variables.
pomek Jul 29, 2019
62c6a60
Merge branch 't/773' of github.com:ckeditor/ckeditor5 into t/773
pomek Jul 29, 2019
b217e32
Fixed a comment.
pomek Jul 29, 2019
aca8fc1
Simplified CSS paths in comments and added a header to the content-st…
pomek Jul 31, 2019
f4353fb
Merge branch 'master' into t/773
oleq Aug 6, 2019
1353215
Changed the URL to the documentation rendered in the header of the ge…
oleq Aug 6, 2019
81cb706
Docs: Created a guide about editor content styles.
oleq Aug 6, 2019
41a3217
Docs: Corrected links and grammar in the Content styles guide.
oleq Aug 6, 2019
f554bff
Docs: Updated the FAQ entry about content styles.
oleq Aug 6, 2019
1f048c8
Docs: Corrected language in the content-styles script and the guide.
oleq Aug 6, 2019
ab1d33d
Merge branch 'master' into t/773
Reinmar Aug 12, 2019
76a0567
It reads better this way, to me.
Reinmar Aug 12, 2019
0f0a100
Merge branch 'master' into t/773
oleq Aug 13, 2019
65a56b6
Docs: Moved the bit about generating content styles to the 'Developme…
oleq Aug 13, 2019
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"docs:api": "node ./scripts/docs/build-api-docs.js",
"docs:build-and-publish": "node ./scripts/docs/build-and-publish.js",
"docs:build-and-publish-nightly": "node ./scripts/docs/build-and-publish-nightly.js",
"docs:content-styles": "node ./scripts/docs/build-content-styles.js",
"translations:collect": "ckeditor5-dev-env-translations collect",
"translations:download": "ckeditor5-dev-env-translations download",
"translations:upload": "ckeditor5-dev-env-translations upload",
Expand Down
191 changes: 191 additions & 0 deletions scripts/docs/build-content-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* eslint-env node */

const path = require( 'path' );
const fs = require( 'fs' );
const webpack = require( 'webpack' );
const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );

const DESTINATION_DIRECTORY = path.join( __dirname, '..', '..', 'build', 'content-styles' );
const VARIABLE_REGEXP = /(--[\w-]+):\s+(.*);/g;

const contentRules = {
selector: [],
variables: []
};

const webpackConfig = getWebpackConfig();
const parentCwd = path.join( process.cwd(), '..' );

runWebpack( webpackConfig )
.then( () => {
// All variables are placed inside `:root` selector.
const variablesCss = contentRules.variables
.map( rule => {
// Let's extract all of them as an array of simple strings.
const allRules = [];
let match;

while ( ( match = VARIABLE_REGEXP.exec( rule.css ) ) ) {
allRules.push( `${ match[ 1 ] }: ${ match[ 2 ] };` );
}

return allRules;
} )
.reduce( ( previousValue, currentValue ) => {
// And simplify nested arrays as a single array.
previousValue.push( ...currentValue );

return previousValue;
}, [] )
.map( rule => {
return `\t${ rule }`;
} )
.join( '\n' );

const selectorCss = contentRules.selector
.map( rule => {
// Removes all comments from the rule definition.
const cssAsArray = rule.css.replace( /\/\*[^*]+\*\//g, '' ).split( '\n' );

// We want to fix invalid indentations. We need to find a number of how many indentations we want to remove.
// Because the last line ends the block, we can use this value.
const lastLineIndent = cssAsArray[ cssAsArray.length - 1 ].length - 1;

const css = cssAsArray
.filter( line => line.trim().length > 0 )
.map( ( line, index ) => {
// Do not touch the first line. It is always correct.
if ( index === 0 ) {
return line;
}

return line.slice( lastLineIndent );
} )
.join( '\n' );

return `/* ${ rule.file.replace( parentCwd + path.sep, '' ) } */\n${ css }`;
} )
.filter( rule => {
// 1st: path to the css file, 2nd: selector definition - start block, 3rd: end block
// If the rule contains only 3 lines, it means that it does not define any rules.
return rule.split( '\n' ).length > 3;
} )
.join( '\n' );

const data = `:root {\n${ variablesCss }\n}\n\n${ selectorCss }`;

return writeFile( path.join( DESTINATION_DIRECTORY, 'content-styles.css' ), data );
} )
.then( () => {
console.log( `Content styles has saved under the path: ${ path.join( DESTINATION_DIRECTORY, 'content-styles.css' ) }` );
} )
.catch( err => {
console.log( err );
} );

/**
* Prepares configuration for Webpack.
*
* @returns {Object}
*/
function getWebpackConfig() {
const postCssConfig = styles.getPostCssConfig( {
themeImporter: {
themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
},
minify: false
} );

const contentStylesPlugin = require( './content-styles/list-content-styles' )( { contentRules } );

postCssConfig.plugins.push( contentStylesPlugin );

return {
mode: 'development',

devtool: 'source-map',

entry: {
ckeditor5: path.join( __dirname, 'content-styles', 'ckeditor.js' )
},

output: {
path: DESTINATION_DIRECTORY,
filename: '[name].js'
},

// Configure the paths so building CKEditor 5 snippets work even if the script
// is triggered from a directory outside ckeditor5 (e.g. multi-project case).
resolve: {
modules: getModuleResolvePaths()
},

resolveLoader: {
modules: getModuleResolvePaths()
},

module: {
rules: [
{
test: /\.svg$/,
use: [ 'raw-loader' ]
},
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'postcss-loader',
options: postCssConfig
}
]
}
]
}
};
}

/**
* @param {Object} webpackConfig
* @returns {Promise}
*/
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();
}
} );
} );
}

/**
* @returns {Array.<String>}
*/
function getModuleResolvePaths() {
return [
path.resolve( __dirname, '..', '..', 'node_modules' ),
'node_modules'
];
}

function writeFile( file, data ) {
return new Promise( ( resolve, reject ) => {
fs.writeFile( file, data, err => {
if ( err ) {
return reject( err );
}

return resolve();
} );
} );
}
72 changes: 72 additions & 0 deletions scripts/docs/content-styles/ckeditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

// The editor creator to use.
import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';

import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Code from '@ckeditor/ckeditor5-basic-styles/src/code';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import Strikethrough from '@ckeditor/ckeditor5-basic-styles/src/strikethrough';
import Subscript from '@ckeditor/ckeditor5-basic-styles/src/subscript';
import Superscript from '@ckeditor/ckeditor5-basic-styles/src/superscript';
import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline';
import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
import EasyImage from '@ckeditor/ckeditor5-easy-image/src/easyimage';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
import Link from '@ckeditor/ckeditor5-link/src/link';
import List from '@ckeditor/ckeditor5-list/src/list';
import MediaEmbed from '@ckeditor/ckeditor5-media-embed/src/mediaembed';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import PasteFromOffice from '@ckeditor/ckeditor5-paste-from-office/src/pastefromoffice';
import Table from '@ckeditor/ckeditor5-table/src/table';
import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
import Font from '@ckeditor/ckeditor5-font/src/font';
import Highlight from '@ckeditor/ckeditor5-highlight/src/highlight';
import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment';

export default class ClassicEditor extends ClassicEditorBase {}

// Plugins to include in the build.
ClassicEditor.builtinPlugins = [
Essentials,
UploadAdapter,
Autoformat,
Bold,
Code,
Italic,
Strikethrough,
Subscript,
Superscript,
Underline,
BlockQuote,
CKFinder,
EasyImage,
Heading,
Image,
ImageCaption,
ImageStyle,
ImageToolbar,
ImageUpload,
Link,
List,
MediaEmbed,
Paragraph,
PasteFromOffice,
Table,
TableToolbar,
Font,
Highlight,
Alignment
];
35 changes: 35 additions & 0 deletions scripts/docs/content-styles/list-content-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/* eslint-env node */

const postcss = require( 'postcss' );

module.exports = postcss.plugin( 'list-content-styles', function( options ) {
const selectorStyles = options.contentRules.selector;
const variables = options.contentRules.variables;

return root => {
root.walkRules( rule => {
rule.selectors.forEach( selector => {
if ( selector.match( ':root' ) ) {
variables.push( {
file: root.source.input.file,
css: rule.toString()
} );
}

if ( selector.match( '.ck-content' ) ) {
selectorStyles.push( {
file: root.source.input.file,
css: rule.toString()
} );
}
} );
} );
};
} );