Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from ckeditor/t/1
Browse files Browse the repository at this point in the history
Feature: Initial implementation (moved from the `ckeditor5-image` package). Closes #1.
  • Loading branch information
Reinmar authored Mar 31, 2017
2 parents c064294 + 3a9e774 commit 564dd97
Show file tree
Hide file tree
Showing 11 changed files with 1,425 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Software License Agreement
==========================

**CKEditor 5 Image Feature**https://github.com/ckeditor/ckeditor5-image <br>
**CKEditor 5 widget API**https://github.com/ckeditor/ckeditor5-image <br>
Copyright (c) 2003-2017, [CKSource](http://cksource.com) Frederico Knabben. All rights reserved.

Licensed under the terms of any of the following licenses at your choice:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CKEditor 5 Widget Feature
CKEditor 5 widget API
========================================

[![npm version](https://badge.fury.io/js/%40ckeditor%2Fckeditor5-widget.svg)](https://www.npmjs.com/package/@ckeditor/ckeditor5-widget)
Expand All @@ -7,7 +7,7 @@ CKEditor 5 Widget Feature
[![Dependency Status](https://david-dm.org/ckeditor/ckeditor5-widget/status.svg)](https://david-dm.org/ckeditor/ckeditor5-widget)
[![devDependency Status](https://david-dm.org/ckeditor/ckeditor5-widget/dev-status.svg)](https://david-dm.org/ckeditor/ckeditor5-widget?type=dev)

Widget feature for CKEditor 5. More information about the project can be found at the following URL: <https://github.com/ckeditor/ckeditor5-widget>.
Widget API for CKEditor 5. More information about the project can be found at the following URL: <https://github.com/ckeditor/ckeditor5-widget>.

## License

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"name": "@ckeditor/ckeditor5-widget",
"version": "0.0.1",
"description": "Widget feature for CKEditor 5.",
"description": "Widget API for CKEditor 5.",
"keywords": [],
"dependencies": {
"@ckeditor/ckeditor5-core": "^0.7.0",
"@ckeditor/ckeditor5-engine": "^0.8.0",
"@ckeditor/ckeditor5-utils": "^0.8.0",
"@ckeditor/ckeditor5-theme-lark": "^0.6.1"
},
"devDependencies": {
"@ckeditor/ckeditor5-dev-lint": "^2.0.2",
Expand Down
121 changes: 121 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/**
* @module widget/utils
*/

const widgetSymbol = Symbol( 'isWidget' );
const labelSymbol = Symbol( 'label' );

/**
* CSS class added to each widget element.
*
* @const {String}
*/
export const WIDGET_CLASS_NAME = 'ck-widget';

/**
* CSS class added to currently selected widget element.
*
* @const {String}
*/
export const WIDGET_SELECTED_CLASS_NAME = 'ck-widget_selected';

/**
* Returns `true` if given {@link module:engine/view/element~Element} is a widget.
*
* @param {module:engine/view/element~Element} element
* @returns {Boolean}
*/
export function isWidget( element ) {
return !!element.getCustomProperty( widgetSymbol );
}

/**
* Converts given {@link module:engine/view/element~Element} to widget in following way:
* * sets `contenteditable` attribute to `true`,
* * adds custom `getFillerOffset` method returning `null`,
* * adds `ck-widget` CSS class,
* * adds custom property allowing to recognize widget elements by using {@link ~isWidget}.
*
* @param {module:engine/view/element~Element} element
* @param {Object} [options]
* @param {String|Function} [options.label] Element's label provided to {@link ~setLabel} function. It can be passed as
* a plain string or a function returning a string.
* @returns {module:engine/view/element~Element} Returns same element.
*/
export function toWidget( element, options ) {
options = options || {};
element.setAttribute( 'contenteditable', false );
element.getFillerOffset = getFillerOffset;
element.addClass( WIDGET_CLASS_NAME );
element.setCustomProperty( widgetSymbol, true );

if ( options.label ) {
setLabel( element, options.label );
}

return element;
}

/**
* Sets label for given element.
* It can be passed as a plain string or a function returning a string. Function will be called each time label is retrieved by
* {@link ~getLabel}.
*
* @param {module:engine/view/element~Element} element
* @param {String|Function} labelOrCreator
*/
export function setLabel( element, labelOrCreator ) {
element.setCustomProperty( labelSymbol, labelOrCreator );
}

/**
* Returns label for provided element.
*
* @param {module:engine/view/element~Element} element
* @return {String}
*/
export function getLabel( element ) {
const labelCreator = element.getCustomProperty( labelSymbol );

if ( !labelCreator ) {
return '';
}

return typeof labelCreator == 'function' ? labelCreator() : labelCreator;
}

/**
* Adds functionality to provided {module:engine/view/editableelement~EditableElement} to act as a widget's editable:
* * sets `contenteditable` attribute to `true`,
* * adds `ck-editable` CSS class,
* * adds `ck-editable_focused` CSS class when editable is focused and removes it when it's blurred.
*
* @param {module:engine/view/editableelement~EditableElement} editable
* @returns {module:engine/view/editableelement~EditableElement} Returns same element that was provided in `editable` param.
*/
export function toWidgetEditable( editable ) {
editable.setAttribute( 'contenteditable', 'true' );
editable.addClass( 'ck-editable' );

editable.on( 'change:isFocused', ( evt, property, is ) => {
if ( is ) {
editable.addClass( 'ck-editable_focused' );
} else {
editable.removeClass( 'ck-editable_focused' );
}
} );

return editable;
}

// Default filler offset function applied to all widget elements.
//
// @returns {null}
function getFillerOffset() {
return null;
}
Loading

0 comments on commit 564dd97

Please sign in to comment.