### Set Orientation (POST) vs Get Orientation (GET)
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Demonstrates how to use POST for setting the screen orientation and GET for retrieving it. The POST method requires parameters, while GET does not.
```php
// POST /session/:sessionId/orientation
$session->postOrientation(array('orientation' => 'LANDSCAPE'));
```
```php
// GET /session/:sessionId/orientation
$session->orientation();
```
--------------------------------
### startContainer
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the starting node of the range.
```APIDOC
## startContainer
### Description
Gets the starting node of the range.
### Returns
* Node: The starting node of the range.
```
--------------------------------
### Set Window Size (POST) vs Get Window Size (GET)
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Illustrates setting the size of a specific window using POST and retrieving the current window size using GET. The window handle can be specified or defaults to the current window.
```php
// If excluded, $window_handle defaults to 'current'
// POST /session/:sessionId/window/:windowHandle/size
$session
->window($window_handle)
->postSize(array('width' => 10, 'height' => 10));
```
```php
// GET /session/:sessionId/window/:windowHandle/size
$session->window()->size();
```
--------------------------------
### Trigger Dialog on Load
Source: https://github.com/kindsoft/kindeditor/blob/master/test/dialog.html
Automatically triggers the creation of the first dialog example when the page loads. This is often used for immediate user interaction or setup.
```javascript
K('#create1').click();
```
--------------------------------
### startOffset
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the offset of the starting position within the startContainer.
```APIDOC
## startOffset
### Description
Gets the offset of the starting position within the startContainer.
### Returns
* Integer: The offset of the starting position.
```
--------------------------------
### Get and Set Attributes
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Use 'attr()' to get all attributes as an object. Use 'attr(key)' to get a specific attribute's value. Use 'attr(key, val)' or 'attr(obj)' to set one or more attributes on all nodes.
```javascript
var attrList = K('#id').attr(); // return key-value data
var border = K('#id').attr('border');
K('#id img').attr('border', 1);
K('#id img').attr({
'width' : '100px',
'border' : 1
});
```
--------------------------------
### K.START_TO_START
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Constant used with compareBoundaryPoints(how, range) to indicate the start of the range.
```APIDOC
## K.START_TO_START
### Description
Constant used with [compareBoundaryPoints(how, range)](#krange-compareboundarypoints) to indicate the start of the range.
### Usage
Used as the `how` parameter in the `compareBoundaryPoints` method.
```
--------------------------------
### collapse(toStart)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Collapses the range to its start or end point.
```APIDOC
## collapse(toStart)
### Description
Collapses the range to its start or end point.
### Parameters
* **toStart** (Boolean) - If true, collapses to the start; otherwise, collapses to the end.
### Returns
* KRange: The modified KRange object.
```
--------------------------------
### Get Window Handles
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Fetches a list of all currently open window handles. This is a GET request to the /session/:sessionId/window_handles endpoint.
```php
$session->window_handles();
```
--------------------------------
### setStart(node, offset)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the start point of the range to a specific node and offset.
```APIDOC
## setStart(node, offset)
### Description
Sets the start point of the range to a specific node and offset.
### Parameters
* **node** (Node) - The node to set as the start container.
* **offset** (Integer) - The offset within the node.
### Returns
* KRange: The modified KRange object.
### Example
```js
var range = K.range(document);
range.setStart(document.body, 1);
```
```
--------------------------------
### Initialize KindEditor with Options
Source: https://github.com/kindsoft/kindeditor/blob/master/test/main.html
Initializes KindEditor with specified options including theme, plugins path, language path, and file manager enablement. This is a common setup for basic editor integration.
```javascript
K.create('textarea[name=content1]', {
themeType : 'default',
pluginsPath : '../plugins/',
langPath : '../lang/',
allowFileManager : true
});
```
--------------------------------
### Set Start After Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the start of the KRange to be just after the specified node. This is useful for selecting content starting immediately after a particular element.
```javascript
var range = K.range(document);
range.setStartAfter(K('div#id')[0]);
```
--------------------------------
### Set Start of KRange
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the starting point of the KRange to a specified node and offset. This method allows precise control over the beginning of the selected range.
```javascript
var range = K.range(document);
range.setStart(document.body, 1);
```
--------------------------------
### K.END_TO_START
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Constant used with compareBoundaryPoints(how, range) to indicate the start of the range.
```APIDOC
## K.END_TO_START
### Description
Constant used with [compareBoundaryPoints(how, range)](#krange-compareboundarypoints) to indicate the start of the range.
### Usage
Used as the `how` parameter in the `compareBoundaryPoints` method.
```
--------------------------------
### Make GET AJAX Request
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/ajax.md
Perform a GET request to a specified URL using K.ajax. The response data is passed to the callback function.
```javascript
K.ajax('test.php', function(data) {
console.log(data);
});
```
--------------------------------
### setStartBefore(node)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the start point of the range to be before the given node.
```APIDOC
## setStartBefore(node)
### Description
Sets the start point of the range to be before the given node.
### Parameters
* **node** (Node) - The node before which the range starts.
### Returns
* KRange: The modified KRange object.
### Example
```js
var range = K.range(document);
range.setStartBefore(K('div#id')[0]);
```
```
--------------------------------
### Set Start Before Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the start of the KRange to be just before the specified node. This is useful for selecting content up to, but not including, a particular element.
```javascript
var range = K.range(document);
range.setStartBefore(K('div#id')[0]);
```
--------------------------------
### K.ajax(url [, fn , method , data])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/ajax.md
Performs GET or POST requests to a specified URL, with optional callback and data.
```APIDOC
## K.ajax(url [, fn , method , data])
### Description
Performs GET or POST requests to a specified URL, with optional callback and data.
### Parameters
#### Path Parameters
* **url** (string) - Required - The URL to send the request to.
* **fn** (function) - Optional - The callback function to execute with the response data.
* **method** (string) - Optional - The HTTP method to use (e.g., "GET" or "POST"). Defaults to "GET".
* **data** (object) - Optional - Data to send with a POST request, in key-value format.
### Request Example
```js
//GET
K.ajax('test.php', function(data) {
console.log(data);
});
//POST
K.ajax('test.php', function(data) {
console.log(data);
}, 'POST', {
aa : 1,
bb : 2
});
```
```
--------------------------------
### Get session capabilities
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieve the capabilities of the current WebDriver session using the `capabilities` method. This corresponds to the GET /session/:sessionId command.
```php
$session->capabilities();
```
--------------------------------
### K.V
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Gets the version number of the current browser.
```APIDOC
## K.V
### Description
Gets the version number of the current browser.
### Returns
string - The browser version number.
```
--------------------------------
### setStartAfter(node)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Sets the start point of the range to be after the given node.
```APIDOC
## setStartAfter(node)
### Description
Sets the start point of the range to be after the given node.
### Parameters
* **node** (Node) - The node after which the range starts.
### Returns
* KRange: The modified KRange object.
### Example
```js
var range = K.range(document);
range.setStartAfter(K('div#id')[0]);
```
```
--------------------------------
### split(isStart, map)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Splits the start or end of the current range based on a provided map of rules.
```APIDOC
## split(isStart, map)
### Description
Splits the start or end position of the range based on the map rules.
### Parameters
#### Path Parameters
- **isStart** (boolean) - Required - True or false, indicating whether to split the start or end.
- **map** (object) - Required - The rules for splitting.
### Returns
- KCmd: The KCmd object.
### Example
```js
cmd.split(true, {
span : '*',
div : 'class,border'
});
```
```
--------------------------------
### scan(fn, order)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Scans the DOM tree starting from the first node in the KNode collection, executing a callback function for each node.
```APIDOC
## scan(fn, order)
### Description
Scans the DOM tree starting from the first node, executing a callback function for each node.
### Parameters
* **fn** (function) - The callback function to execute for each node.
* **order** (string) - Optional. The traversal order ('pre' for pre-order, 'post' for post-order).
### Returns
KNode
```
--------------------------------
### commonAncestor()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the common ancestor element of the range's start and end points.
```APIDOC
## commonAncestor()
### Description
Gets the common ancestor element of the range's start and end points.
### Returns
* Element: The common ancestor element.
### Example
```js
var range = K.range(document);
var element = range.commonAncestor();
```
```
--------------------------------
### K.widget(options)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/widget.md
Creates a widget with the specified configuration options. It returns a KWidget object.
```APIDOC
## K.widget(options)
### Description
Creates a widget with the specified configuration options. It returns a KWidget object.
### Parameters
#### Options Object
- **z** (number) - Optional - The z-index for the widget.
- **width** (number) - Optional - The width of the widget.
- **height** (number) - Optional - The height of the widget.
- **html** (string) - Optional - The HTML content to be placed inside the widget.
- **css** (object) - Optional - An object containing CSS properties to style the widget.
### Example
```js
var widget = K.widget({
z : 100,
width : 200,
height : 100,
html : 'abc123abcabc',
css : {
border : '1px solid #A0A0A0',
background : '#F0F0F0'
}
});
```
```
--------------------------------
### K.addParam(url, param)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Adds a GET parameter to a URL, intelligently handling the connector character.
```APIDOC
## K.addParam(url, param)
### Description
Appends a GET parameter to a given URL. It automatically determines whether to use '?' or '&' as the connector.
### Method
JavaScript Function
### Parameters
#### Path Parameters
- **url** (string) - The base URL.
- **param** (string) - The GET parameter string to add (e.g., 'key=value').
### Response
#### Success Response
- **string**: The URL with the added GET parameter.
### Request Example
```js
url = K.addParam('http://www.example.com/test.php', 'abc=123');
url = K.addParam('http://www.example.com/test.php?cde=456', 'abc=123');
```
```
--------------------------------
### Create a Widget
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/widget.md
Use K.widget to create a new widget with specified configuration options for dimensions, content, and styling.
```javascript
var widget = K.widget({
z : 100,
width : 200,
height : 100,
html : 'abc123abcabc',
css : {
border : '1px solid #A0A0A0',
background : '#F0F0F0'
}
});
```
--------------------------------
### Get Common Ancestor of KRange
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Retrieves the common ancestor element of the start and end points of the KRange. This is useful for operations that need to be performed on a shared parent node.
```javascript
var range = K.range(document);
var element = range.commonAncestor();
```
--------------------------------
### K.tabs(options)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/tabs.md
Creates a new tab instance with the specified configuration options. This is the primary method for initializing a tabbed interface.
```APIDOC
## K.tabs(options)
### Description
Creates a new tab instance with the specified configuration options.
### Method
`K.tabs(options)`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
* **options** (object) - Required - Configuration information for the tabs.
* **parent** (string) - The selector for the parent element where the tabs will be rendered.
* **afterSelect** (function) - A callback function that is executed after a tab is selected. It receives the index of the selected tab as an argument.
### Request Example
```js
var tabs = K.tabs({
parent : '#tabs',
afterSelect : function(i) {
K('#tab' + (i + 1)).html('选中了标签#' + (i + 1));
}
});
```
### Response
#### Success Response
* **KTabs** - Returns an instance of KTabs which can be used to further manipulate the tabs.
#### Response Example
```js
// The returned object is an instance of KTabs
// Example of using the returned object:
tabs.add({
title : '标签#1',
panel : '#tab1'
});
```
### Additional Methods
* **add(options)**: Adds a new tab to the instance.
* **options** (object)
* **title** (string) - The title of the tab.
* **panel** (string) - The selector for the panel associated with this tab.
* **select(index)**: Selects a tab by its index.
* **index** (number) - The index of the tab to select.
```
--------------------------------
### Initialize WebDriver Session
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Establishes a connection to the Selenium server and creates a new browser session. Ensure the Selenium server is running before executing this code.
```php
$wd_host = 'http://localhost:4444/wd/hub'; // this is the default
$web_driver = new WebDriver($wd_host);
// First param to session() is the 'browserName' (default = 'firefox')
// Second param is a JSON object of additional 'desiredCapabilities'
// POST /session
$session = $web_driver->session('firefox');
```
--------------------------------
### get([hasControlRange])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the range, optionally checking for a control range.
```APIDOC
## get([hasControlRange])
### Description
Gets the range, optionally checking for a control range.
### Parameters
* **hasControlRange** (Boolean, optional) - If true, checks for a control range.
### Returns
* KRange: The range object.
```
--------------------------------
### Add GET Parameter to URL
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Appends a GET parameter to a URL, automatically handling the correct separator ('?' or '&').
```javascript
url = K.addParam('http://www.example.com/test.php', 'abc=123');
```
```javascript
url = K.addParam('http://www.example.com/test.php?cde=456', 'abc=123');
```
--------------------------------
### Get Current URL
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieves the URL of the current page. This is a GET request to the /session/:sessionId/url endpoint.
```php
$session->url();
```
--------------------------------
### Open a URL in the browser
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Use the `open` method to navigate to a specific URL. This corresponds to the POST /session/:sessionId/url command.
```php
$session->open('http://www.facebook.com');
```
--------------------------------
### fullHtml()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Gets the complete HTML content of the editor, including the `` tags. This method is called on an editor object.
```APIDOC
## fullHtml()
### Description
Gets the complete HTML content of the editor, including the `` tags. This method is called on an editor object.
### Method
`editor.fullHtml()`
### Returns
* string - The full HTML content of the editor.
### Examples
```js
var fullHtml = editor.fullHtml();
```
```
--------------------------------
### Create a KindEditor instance
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Use K.create to initialize a new KindEditor instance. You can specify a textarea element or selector and optional configuration options. The created editor instance is also available in KindEditor.instances.
```javascript
// 1
// editor 等于 KindEditor.instances[0]
editor = K.create('textarea[name="content"]');
editor.html('HTML code');
```
```javascript
editor = K.create('#editor_id', {
filterMode : true,
langType : 'en'
});
```
--------------------------------
### Get all cookies
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieve all cookies associated with the current session using `getAllCookies`. Corresponds to GET /session/:sessionId/cookie.
```php
$session->getAllCookies();
```
--------------------------------
### K.create(expr [, options])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Creates a new KindEditor instance. It can target a textarea element or a selector and accepts an options object for initialization. It returns the first KEditor object created.
```APIDOC
## K.create(expr [, options])
### Description
Creates a new KindEditor instance. It can target a textarea element or a selector and accepts an options object for initialization. It returns the first KEditor object created.
### Method
`K.create(expr [, options])`
### Parameters
* **expr** (mixed) - Required - The element or selector for the textarea.
* **options** (object) - Optional - Editor initialization parameters.
### Returns
* KEditor - The created KEditor instance.
### Examples
```js
// Create an editor instance from a textarea
editor = K.create('textarea[name="content"]');
editor.html('HTML code');
// Create an editor with specific options
editor = K.create('#editor_id', {
filterMode : true,
langType : 'en'
});
```
### Note
Starting from version 4.1.2, `expr` can directly accept a jQuery object.
```
--------------------------------
### Create and Configure Tabs
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/tabs.md
Creates a new tab widget with specified parent container and an optional callback for tab selection. It then adds multiple tabs, each with a title and associated panel, and finally selects the first tab.
```javascript
var tabs = K.tabs({
parent : '#tabs',
afterSelect : function(i) {
K('#tab' + (i + 1)).html('选中了标签#' + (i + 1));
}
});
tabs.add({
title : '标签#1',
panel : '#tab1'
});
tabs.add({
title : '标签#2',
panel : '#tab2'
});
tabs.add({
title : '标签#3',
panel : '#tab3'
});
tabs.select(0);
```
--------------------------------
### K.extend(fn , proto) and K.extend(child , parent , proto)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Provides functionality for creating classes and extending them with inheritance.
```APIDOC
## K.extend(fn , proto)
### Description
Creates a new class constructor function with specified prototype properties.
### Method
JavaScript Function
### Parameters
#### Path Parameters
- **fn** (function) - The constructor function for the new class.
- **proto** (object) - An object containing properties to be added to the class's prototype.
### Response
#### Success Response
- **undefined**: This function modifies the `fn` in place.
### Request Example
```js
function Animal() {
this.init();
}
K.extend(Animal, {
init : function() {
console.log('init animal.');
},
run : function() {
console.log('animal is running.');
}
});
var animal = new Animal();
animal.run();
```
## K.extend(child , parent , proto)
### Description
Establishes inheritance between a child class and a parent class, optionally adding new prototype properties to the child.
### Method
JavaScript Function
### Parameters
#### Path Parameters
- **child** (function) - The child constructor function.
- **parent** (function) - The parent constructor function.
- **proto** (object) - An object containing properties to be added to the child's prototype. This can include an `init` method that calls the parent's `init`.
### Response
#### Success Response
- **undefined**: This function modifies the `child` and `parent` constructors in place.
### Request Example
```js
// create Animal class
function Animal(name) {
this.init(name);
}
K.extend(Animal, {
init : function(name) {
this.name = name;
},
run : function() {
console.log(this.name + ' is running.');
}
});
// create Cat class
function Cat(name, age) {
this.init(name, age);
}
K.extend(Cat, Animal, {
init : function(name, age) {
Cat.parent.init.call(this, name);
this.age = age;
}
});
var myCat = new Cat('Tony', 5);
console.log(myCat.name); // print 'Tony'
console.log(myCat.age); // print 5
myCat.run(); // print 'Tony is running.'
```
```
--------------------------------
### Get Children Nodes
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Use `children()` to get a list of all child nodes for the first element. This allows for manipulation or inspection of direct descendants.
```javascript
K('#id').children().css('color', 'red');
```
--------------------------------
### Create a Class with Methods
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Defines a new class by extending a function with a prototype object containing its methods. Use this for basic class creation.
```javascript
function Animal() {
this.init();
}
K.extend(Animal, {
init : function() {
console.log('init animal.');
},
run : function() {
console.log('animal is running.');
}
});
var animal = new Animal();
animal.run();
```
--------------------------------
### Get Height of First Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the computed height of the first selected node in pixels. Use this to get the current height of an element.
```javascript
var height = K('#id').height();
```
--------------------------------
### Get Width of First Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the computed width of the first selected node in pixels. Use this to get the current width of an element.
```javascript
var width = K('#id').width();
```
--------------------------------
### createBookmark([serialize])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Creates a bookmark to mark a position within the range. Optionally, it can serialize the bookmark to include temporary node IDs.
```APIDOC
## createBookmark([serialize])
### Description
Creates a bookmark (inserts a temporary node to mark the position).
### Parameters
* `serialize` (Boolean) - Optional - Whether to serialize the bookmark. If true, the bookmark includes temporary node IDs; otherwise, it includes the temporary node's Element. Defaults to false.
### Returns
* Object: A bookmark object containing start and end markers.
### Example
```js
bookmark = range.createBookmark();
console.log(bookmark); // print {start: startNode, end: endNode}
bookmark = range.createBookmark(true);
console.log(bookmark); // print {start: 'start_node_id', end: 'end_node_id'}
```
```
--------------------------------
### win
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Represents the window object within the KCmd context.
```APIDOC
## win
### Description
Represents the window object.
### Returns
- Window: The window object.
```
--------------------------------
### Initialize KindEditor
Source: https://github.com/kindsoft/kindeditor/blob/master/test/quirkmode.html
Initializes KindEditor with a textarea element, enabling file management and setting the language type.
```javascript
KindEditor.ready(function(K) {
window.editor = K.create('textarea', {
allowFileManager : true,
langType : 'en'
});
});
```
--------------------------------
### K.ready(fn)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/event.md
Binds a specified function to the DOMContentLoaded event, ensuring the function executes only after the DOM is fully loaded and parsed.
```APIDOC
## K.ready(fn)
### Description
Binds a specified function to the DOMContentLoaded event, ensuring it executes after the DOM is fully loaded.
### Method
JavaScript Function
### Parameters
* **fn** (function) - Required - The callback function to execute when the DOM is ready.
### Request Example
```js
K.ready(function() {
alert('DOM loaded');
});
```
```
--------------------------------
### Get Previous Sibling Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Use `prev()` to get the immediately preceding sibling node of the first element. This aids in navigating adjacent elements.
```javascript
var prevNode = K('#id').prev();
```
--------------------------------
### createBookmark([serialize])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Creates a bookmark for the range.
```APIDOC
## createBookmark([serialize])
### Description
Creates a bookmark for the range.
### Parameters
* **serialize** (Boolean, optional) - If true, the bookmark will be serialized.
### Returns
* Object: The bookmark object.
```
--------------------------------
### Get KindEditor plain text content
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Use the text() method to get the plain text content of the editor. This includes text from images and embeds.
```javascript
var text = editor.text();
```
--------------------------------
### Initialize KindEditor
Source: https://github.com/kindsoft/kindeditor/blob/master/test/remote.html
Initializes KindEditor on a specific element. Ensure KindEditor's basePath is correctly set.
```javascript
KindEditor.basePath = 'http://domain.com/kindeditor/trunk/';
KindEditor.ready(function(K) {
K.create('#editor1');
});
```
--------------------------------
### Initialize and Manage KindEditor Instances
Source: https://github.com/kindsoft/kindeditor/blob/master/test/total.html
This snippet shows how to initialize a KindEditor instance with file manager enabled and Chinese language support. It also includes event handlers for creating and removing editor instances.
```javascript
//document.domain = 'domain.com'; KindEditor.ready(function(K) {
window.editor = K.create('textarea', {
allowFileManager : true,
langType : 'zh-CN',
autoHeightMode : true
});
K('#removeAll').click(function(e) {
K.remove('textarea');
});
K('#create1').click(function(e) {
K.instances[0].create();
});
K('#remove1').click(function(e) {
K.instances[0].remove();
});
K('#create2').click(function(e) {
K.instances[1].create();
});
K('#remove2').click(function(e) {
K.instances[1].remove();
});
});
```
--------------------------------
### Get Outer HTML of First Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the outer HTML of the first selected node, including the node itself. Useful for getting the full markup of an element.
```javascript
var html = K('#id').outer();
```
--------------------------------
### Get Native DOM Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
The 'get' method retrieves the underlying native DOM node at a specified offset. It returns null if the KNode collection is empty.
```javascript
div1 = K('#id div').get(0);
div2 = K('#id div').get(1);
```
--------------------------------
### K.loadStyle(url)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/ajax.md
Loads a CSS file.
```APIDOC
## K.loadStyle(url)
### Description
Loads a CSS file.
### Parameters
#### Path Parameters
* **url** (string) - Required - The URL of the CSS file to load.
### Request Example
```js
K.loadStyle('test.css');
```
```
--------------------------------
### Get CSS Property Value
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieves the computed value of a specific CSS property for an element. This is a GET request to the /session/:sessionId/element/:id/css/:propertyName endpoint.
```php
$element->css($property_name);
```
--------------------------------
### Get Specific CSS Property of First Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the value of a specific CSS property for the first selected node. Use this to get computed styles like padding or margin.
```javascript
var padding = K('#id').css('padding');
```
--------------------------------
### K.TIME
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Gets the timestamp when KindEditor was loaded.
```APIDOC
## K.TIME
### Description
Returns the timestamp when the KindEditor script was loaded.
### Returns
number - The timestamp.
```
--------------------------------
### endContainer
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the ending node of the range.
```APIDOC
## endContainer
### Description
Gets the ending node of the range.
### Returns
* Node: The ending node of the range.
```
--------------------------------
### selectall()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Selects all content within the editor.
```APIDOC
## selectall()
### Description
Selects all content within the editor.
### Returns
- KCmd: The KCmd object.
### Example
```js
cmd.selectall();
```
```
--------------------------------
### Initialize Google Map and Geocoder
Source: https://github.com/kindsoft/kindeditor/blob/master/plugins/map/map.html
Initializes the Google Map with specified options and sets up the Geocoder. It also performs an initial geocode request to populate an address field.
```javascript
var map, geocoder;
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var options = {
zoom: 11,
center: latlng,
disableDefaultUI: true,
panControl: true,
zoomControl: true,
mapTypeControl: true,
scaleControl: true,
streetViewControl: false,
overviewMapControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
geocoder = new google.maps.Geocoder();
geocoder.geocode({latLng: latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[3]) {
parent.document.getElementById("kindeditor_plugin_map_address").value = results[3].formatted_address;
}
}
});
}
```
--------------------------------
### endOffset
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Gets the offset of the ending position within the endContainer.
```APIDOC
## endOffset
### Description
Gets the offset of the ending position within the endContainer.
### Returns
* Integer: The offset of the ending position.
```
--------------------------------
### Getting the Active Element
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieves the currently active element in the browser.
```APIDOC
## POST /session/:sessionId/element/active
### Description
Gets the element in the current browser context that has focus.
### Method
POST
### Endpoint
/session/:sessionId/element/active
```
--------------------------------
### Create Bookmark (Element)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Create a bookmark to mark the current range. When serialize is false (default), the bookmark contains the actual DOM nodes.
```javascript
bookmark = range.createBookmark();
console.log(bookmark); // print {start: startNode, end: endNode}
```
--------------------------------
### Initialize and Customize Baidu Map
Source: https://github.com/kindsoft/kindeditor/blob/master/plugins/baidumap/index.html
This function initializes the Baidu Map, sets its events, and adds controls. It requires parameters for center, zoom, dimensions, and markers.
```javascript
function getParam(name) { return location.href.match(new RegExp('[?&]' + name + '=(\[^?&]+)', 'i')) ? decodeURIComponent(RegExp.$1) : ''; }
var centerParam = getParam('center');
var zoomParam = getParam('zoom');
var widthParam = getParam('width');
var heightParam = getParam('height');
var markersParam = getParam('markers');
var markerStylesParam = getParam('markerStyles');
//创建和初始化地图函数:
function initMap(){
// [FF]切换模式后报错
if (!window.BMap) {
return;
}
var dituContent = document.getElementById('dituContent');
dituContent.style.width = widthParam + 'px';
dituContent.style.height = heightParam + 'px';
createMap(); //创建地图
setMapEvent(); //设置地图事件
addMapControl(); //向地图添加控件
// 创建标注
var markersArr = markersParam.split(',');
var point = new BMap.Point(markersArr[0], markersArr[1]);
var marker = new BMap.Marker(point);
map.addOverlay(marker); //将标注添加到地图中
}
```
--------------------------------
### K.loadScript(url [, fn])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/ajax.md
Loads a JavaScript file and optionally executes a callback function upon completion.
```APIDOC
## K.loadScript(url [, fn])
### Description
Loads a JavaScript file and optionally executes a callback function upon completion.
### Parameters
#### Path Parameters
* **url** (string) - Required - The URL of the JavaScript file to load.
* **fn** (function) - Optional - The callback function to execute after the script is loaded.
### Request Example
```js
K.loadScript('test.js', function() {
console.log('ok');
});
```
```
--------------------------------
### collapsed
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Checks if the range is collapsed (i.e., start and end points are the same).
```APIDOC
## collapsed
### Description
Checks if the range is collapsed (i.e., start and end points are the same).
### Returns
* Boolean: True if the range is collapsed, false otherwise.
```
--------------------------------
### K.extend(child, parent, proto)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Establishes inheritance between two constructor functions and adds properties.
```APIDOC
## K.extend(child, parent, proto)
### Description
Establishes inheritance where `child` inherits from `parent`. It also adds properties from `proto` to the `child`'s prototype.
### Parameters
* `child` (function) - The child constructor function.
* `parent` (function) - The parent constructor function.
* `proto` (object) - An object containing properties to add to the child's prototype.
### Returns
undefined
### Example
```javascript
function Parent() {}
function Child() {}
K.extend(Child, Parent, { method2: function() {} });
var childInstance = new Child();
// childInstance now inherits from Parent and has method2
```
```
--------------------------------
### Focus on a specific window
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Bring a specific browser window into focus using its handle with the `focusWindow` method. Corresponds to POST /session/:sessionId/window.
```php
$session->focusWindow($window_handle);
```
--------------------------------
### html()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the innerHTML of the first node. This is useful for getting the content of an element.
```APIDOC
## html()
### Description
Retrieves the innerHTML of the first node.
### Method
`html`
### Parameters
None
### Response
#### Success Response
- **html** (string) - The innerHTML of the first node.
### Request Example
```js
var html = K('#id').html();
```
```
--------------------------------
### type
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Gets the nodeType of the first node. 1 for Element, 3 for TextNode.
```APIDOC
## type
### Description
Gets the nodeType of the first node. 1: Element, 3: TextNode.
### Returns
number
```
--------------------------------
### Create Context Menu Item
Source: https://github.com/kindsoft/kindeditor/blob/master/lib/firebug-lite/build/chrome-extension/background.html
This code creates a context menu item labeled 'Inspect with Firebug Lite'. When clicked, it sends a request to the active tab to initiate inspection.
```javascript
chrome.contextMenus.create({
title: "Inspect with Firebug Lite",
"contexts": ["all"],
onclick: function(info, tab) {
chrome.tabs.sendRequest(tab.id, {name: "FB_contextMenuClick"});
}
});
```
--------------------------------
### Create Bookmark (Serialized ID)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Create a bookmark with serialized IDs. When serialize is true, the bookmark contains string IDs of temporary nodes, useful for preserving range across DOM changes.
```javascript
bookmark = range.createBookmark(true);
console.log(bookmark); // print {start: 'start_node_id', end: 'end_node_id'}
```
--------------------------------
### Get Node Name
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
The 'name' property returns the nodeName of the first node in the KNode collection.
```javascript
var nodeName = K('#id div').name;
```
--------------------------------
### up()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Moves the range up.
```APIDOC
## up()
### Description
Moves the range up.
### Returns
* KRange: The modified KRange object.
```
--------------------------------
### Get the Number of Nodes
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
The 'length' property returns the total number of nodes in a KNode collection.
```javascript
var length = K('#id div').length;
```
--------------------------------
### Load Image Plugin and Open Dialog
Source: https://github.com/kindsoft/kindeditor/blob/master/test/main.html
Loads the 'image' plugin and opens the image dialog. This snippet demonstrates how to trigger specific editor functionalities programmatically, such as inserting images.
```javascript
K('#image').click(function() {
editor3.loadPlugin('image', function() {
editor3.plugin.imageDialog({
imageUrl : K('#url').val(),
clickFn : function(url, title, width, height, border, align) {
K('#url').val(url);
editor3.hideDialog();
}
});
});
});
```
--------------------------------
### html()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Gets the HTML content of the current KindEditor instance. This method is called on an editor object.
```APIDOC
## html()
### Description
Gets the HTML content of the current KindEditor instance. This method is called on an editor object.
### Method
`editor.html()`
### Returns
* string - The HTML content of the editor.
### Examples
```js
var html = editor.html();
```
```
--------------------------------
### K.extend(fn, proto)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/core.md
Extends a constructor function with properties from a prototype object.
```APIDOC
## K.extend(fn, proto)
### Description
Extends a constructor function `fn` by adding properties from the `proto` object to its prototype.
### Parameters
* `fn` (function) - The constructor function to extend.
* `proto` (object) - An object containing properties to add to the constructor's prototype.
### Returns
undefined
### Example
```javascript
function MyClass() {}
K.extend(MyClass, { method1: function() {} });
var instance = new MyClass();
instance.method1(); // works
```
```
--------------------------------
### Splitting Range
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Splits the range at its start or end position based on a provided map of rules.
```javascript
cmd.split(true, {
span : '*',
div : 'class,border'
});
```
--------------------------------
### Opening Pages
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Opens a new URL in the current browser session.
```APIDOC
## POST /session/:sessionId/url
### Description
Opens a new URL in the current browser session.
### Method
POST
### Endpoint
/session/:sessionId/url
### Request Body
- **url** (string) - Required - The URL to open.
```
--------------------------------
### get([i])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Retrieves the native DOM node at a specified index. Returns null if the KNode collection is empty.
```APIDOC
## get([i])
### Description
Retrieves the native DOM node. Returns null if the KNode collection is empty.
### Parameters
* **i** (int) - Optional. The offset, defaults to 0.
### Returns
node
### Example
```js
div1 = K('#id div').get(0);
div2 = K('#id div').get(1);
```
```
--------------------------------
### select()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Selects the current range within the editor.
```APIDOC
## select()
### Description
Selects the current range.
### Returns
- KCmd: The KCmd object.
### Example
```js
cmd.select();
```
```
--------------------------------
### readonly(isReadonly)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Sets or gets the readonly state of the KindEditor instance. This method is called on an editor object.
```APIDOC
## readonly(isReadonly)
### Description
Sets or gets the readonly state of the KindEditor instance. This method is called on an editor object.
### Method
`editor.readonly(isReadonly)`
### Parameters
* **isReadonly** (boolean) - Required - `true` to make the editor readonly, `false` to make it editable.
### Returns
* KEditor - The KEditor instance (for chaining).
### Examples
```js
// Make the editor readonly
editor.readonly(true);
// Make the editor editable
editor.readonly(false);
```
```
--------------------------------
### clickToolbar(name, fn)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Simulates a click on a toolbar item by its name and executes a callback function `fn` after the click. This method is called on an editor object.
```APIDOC
## clickToolbar(name, fn)
### Description
Simulates a click on a toolbar item by its name and executes a callback function `fn` after the click. This method is called on an editor object.
### Method
`editor.clickToolbar(name, fn)`
### Parameters
* **name** (string) - Required - The name of the toolbar item to click.
* **fn** (function) - Optional - A callback function to execute after the click.
### Returns
* KEditor - The KEditor instance (for chaining).
### Examples
```js
editor.clickToolbar('image', function() {
console.log('Image button clicked');
});
```
```
--------------------------------
### count([mode])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Gets the character count of the editor's content. The count can be based on HTML or plain text.
```APIDOC
## count([mode])
### Description
Gets the character count of the editor's content. The count can be based on HTML or plain text.
### Method
`editor.count([mode])`
### Parameters
* **mode** (string) - Optional - The mode for counting. Defaults to 'html'. 'html' counts including HTML tags, 'text' counts only plain text, images, and embeds.
### Returns
* Int - The character count.
### Examples
```js
htmlCount = editor.count(); // Counts HTML content
textCount = editor.count('text'); // Counts plain text content
```
```
--------------------------------
### show([val])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Displays the element. Optionally, a specific display value can be provided.
```APIDOC
## show([val])
### Description
Displays the element.
### Method
`show`
### Parameters
#### Path Parameters
- **val** (string) - Optional - The display value to set. Defaults to 'block'.
### Request Example
```js
K('#id').show();
```
```
--------------------------------
### show([val])
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Makes all nodes in the KNode collection visible. Optionally sets a specific display value.
```APIDOC
## show([val])
### Description
Makes all nodes visible. Optionally sets a specific display value.
### Parameters
* **val** (string) - Optional. The display value (e.g., 'block', 'inline-block').
### Returns
KNode
```
--------------------------------
### K.cmd(doc)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Creates a KCmd object, which is used to operate on the DOM of the visual editing area. It accepts a document object or a KRange object as input.
```APIDOC
## K.cmd(doc)
### Description
Creates a KCmd object, which is used to operate on the DOM of the visual editing area.
### Parameters
#### Path Parameters
- **doc** (document|KRange) - Required - The document object or KRange object.
### Returns
- KCmd: The created KCmd object.
### Example
```js
var cmd = K.cmd(document);
cmd.bold();
cmd.wrap('');
cmd.remove({
span : '*',
div : 'class,border'
});
```
```
--------------------------------
### selectedHtml()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Gets the HTML content of the currently selected text within the KindEditor instance. This method is called on an editor object.
```APIDOC
## selectedHtml()
### Description
Gets the HTML content of the currently selected text within the KindEditor instance. This method is called on an editor object.
### Method
`editor.selectedHtml()`
### Returns
* string - The HTML content of the selected text.
### Examples
```js
var html = editor.selectedHtml();
```
```
--------------------------------
### moveToBookmark(bookmark)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/range.md
Moves the range to a previously created bookmark.
```APIDOC
## moveToBookmark(bookmark)
### Description
Moves the range to a previously created bookmark.
### Parameters
* **bookmark** (Object) - The bookmark object to move to.
### Returns
* KRange: The modified KRange object.
```
--------------------------------
### Get KindEditor HTML content
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Use the html() method without arguments to retrieve the current HTML content of the editor.
```javascript
var html = editor.html();
```
--------------------------------
### createMenu(options)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Creates a custom context menu for the KindEditor instance. This method is called on an editor object.
```APIDOC
## createMenu(options)
### Description
Creates a custom context menu for the KindEditor instance. This method is called on an editor object.
### Method
`editor.createMenu(options)`
### Parameters
* **options** (object) - Required - Configuration options for the menu.
### Returns
* KEditor - The KEditor instance (for chaining).
### Examples
```js
editor.createMenu({
items: [
{ text: 'Item 1', click: function() { alert('Clicked Item 1'); } },
{ text: 'Item 2', click: function() { alert('Clicked Item 2'); } }
]
});
```
```
--------------------------------
### Get Parent Node
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Use `parent()` to retrieve the parent node of the first element in the selection. This is fundamental for DOM traversal upwards.
```javascript
var parentNode = K('#id').parent();
```
--------------------------------
### copy()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Copies the selected content to the clipboard.
```APIDOC
## copy()
### Description
Copies the selected content to the clipboard.
### Returns
- KCmd: The KCmd object.
### Example
```js
cmd.copy();
```
```
--------------------------------
### css(obj)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/node.md
Sets multiple CSS properties for all nodes using a key-value object. Efficient for applying multiple styles.
```APIDOC
## css(obj)
### Description
Sets multiple CSS properties for all nodes using a key-value object.
### Method
`css`
### Parameters
#### Path Parameters
- **obj** (object) - Required - An object containing CSS property key-value pairs.
### Request Example
```js
K('#id div').css({
'width' : '100px',
'height' : '50px',
'padding' : '10px'
});
```
```
--------------------------------
### lang(name)
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/editor.md
Gets the language string for a given key from the editor's language pack. This method is called on an editor object.
```APIDOC
## lang(name)
### Description
Gets the language string for a given key from the editor's language pack. This method is called on an editor object.
### Method
`editor.lang(name)`
### Parameters
* **name** (string) - Required - The key of the language string to retrieve.
### Returns
* string - The language string.
### Examples
```js
var saveButtonText = editor.lang('save');
```
```
--------------------------------
### Get the active element
Source: https://github.com/kindsoft/kindeditor/blob/master/test/webdriver/php-webdriver/README.md
Retrieve the currently active element on the page using the `activeElement` method. Corresponds to POST /session/:sessionId/element/active.
```php
$session->activeElement();
```
--------------------------------
### Create and Initialize KindEditor Widget
Source: https://github.com/kindsoft/kindeditor/blob/master/test/widget.html
Initializes a KindEditor widget with specified z-index, width, height, HTML content, and CSS styles. The widget is made draggable after creation.
```javascript
var widget = null; K('#create').click(function() {
if (!widget) {
widget = K.widget({
z : 100,
width : 200,
height : 100,
html : 'abc123abcabc',
css : {
border : '1px solid #A0A0A0',
background : '#F0F0F0'
}
}).draggable();
}
});
```
--------------------------------
### print()
Source: https://github.com/kindsoft/kindeditor/blob/master/docs/cmd.md
Opens the print dialog for the editor content.
```APIDOC
## print()
### Description
Opens the print dialog for the editor content.
### Returns
- KCmd: The KCmd object.
### Example
```js
cmd.print();
```
```