ckeditor/plugins/crossreference/README.md
author indvd00m (gotoindvdum[at]gmail[dot]com)
Thu, 15 Dec 2016 18:10:20 +0300
changeset 0 44d330dccc59
child 2 467e24fbc60e
permissions -rw-r--r--
Init sample
indvd00m@0
     1
CrossReference Plugin for CKEditor 4
indvd00m@0
     2
=================================
indvd00m@0
     3
indvd00m@0
     4
Adds cross references links with optional auto-numeration for chapters, images, tables and references. Other types of references can be defined in config.
indvd00m@0
     5
indvd00m@0
     6
## Description
indvd00m@0
     7
indvd00m@0
     8
Two main conceptions - anchor and link to anchor. There are 4 type of references defined by default: chapter, image, table, reference. Example of anchor of type `image` in raw html:
indvd00m@0
     9
```html
indvd00m@0
    10
<a 
indvd00m@0
    11
	class="cross-reference cross-anchor" 
indvd00m@0
    12
	cross-reference="image" 
indvd00m@0
    13
	cross-anchor="" 
indvd00m@0
    14
	cross-guid="7d24373b-0756-481d-bf97-5a17ffdf3a28" 
indvd00m@0
    15
	cross-name="Experimental result" 
indvd00m@0
    16
	cross-number="1" 
indvd00m@0
    17
	name="image-7d24373b-0756-481d-bf97-5a17ffdf3a28"
indvd00m@0
    18
	>
indvd00m@0
    19
		Fig. 1. Experimental result.
indvd00m@0
    20
</a>
indvd00m@0
    21
```
indvd00m@0
    22
Example of link to this anchor in raw html:
indvd00m@0
    23
```html
indvd00m@0
    24
<a 
indvd00m@0
    25
	class="cross-reference cross-link" 
indvd00m@0
    26
	cross-reference="image" 
indvd00m@0
    27
	cross-link="" 
indvd00m@0
    28
	cross-guid="7d24373b-0756-481d-bf97-5a17ffdf3a28" 
indvd00m@0
    29
	cross-name="Experimental result" 
indvd00m@0
    30
	cross-number="1" 
indvd00m@0
    31
	href="#image-7d24373b-0756-481d-bf97-5a17ffdf3a28" 
indvd00m@0
    32
	title="Fig. 1. Experimental result."
indvd00m@0
    33
	>
indvd00m@0
    34
		1
indvd00m@0
    35
</a>
indvd00m@0
    36
```
indvd00m@0
    37
After every inserting of anchor or links to anchor all references will be updated to be a concerted. Or you can manually update cross-references by selecting option in menu (for example after deleting of anchors).
indvd00m@0
    38
indvd00m@0
    39
## Configuration
indvd00m@0
    40
indvd00m@0
    41
You can switch which types is active by config option `config.crossreference.activeTypes = ['type1', 'type2']`. You can define other types also.
indvd00m@0
    42
indvd00m@0
    43
### Default config:
indvd00m@0
    44
indvd00m@0
    45
```javascript
indvd00m@0
    46
{
indvd00m@0
    47
	activeTypes: ['chapter', 'image', 'table', 'reference'],
indvd00m@0
    48
	overrideTypes: false,
indvd00m@0
    49
	types: {
indvd00m@0
    50
		chapter: {
indvd00m@0
    51
			name: 'Chapter',
indvd00m@0
    52
			anchorTextTemplate: '${number}. ${name}.',
indvd00m@0
    53
			linkTextTemplate: '${number}',
indvd00m@0
    54
			numeration: {
indvd00m@0
    55
				enabled: true,
indvd00m@0
    56
				firstNumber: '1',
indvd00m@0
    57
				increase: function(number) {
indvd00m@0
    58
					var n = parseInt(number);
indvd00m@0
    59
					return ++n;
indvd00m@0
    60
				}
indvd00m@0
    61
			},
indvd00m@0
    62
			anchorsProvider: 'default',
indvd00m@0
    63
			allowCreateAnchors: true,
indvd00m@0
    64
			groupAnchors: false
indvd00m@0
    65
		},
indvd00m@0
    66
		image: {
indvd00m@0
    67
			name: 'Figure',
indvd00m@0
    68
			anchorTextTemplate: 'Fig. ${number}. ${name}.',
indvd00m@0
    69
			linkTextTemplate: '${number}',
indvd00m@0
    70
			numeration: {
indvd00m@0
    71
				enabled: true,
indvd00m@0
    72
				firstNumber: '1',
indvd00m@0
    73
				increase: function(number) {
indvd00m@0
    74
					var n = parseInt(number);
indvd00m@0
    75
					return ++n;
indvd00m@0
    76
				}
indvd00m@0
    77
			},
indvd00m@0
    78
			anchorsProvider: 'default',
indvd00m@0
    79
			allowCreateAnchors: true,
indvd00m@0
    80
			groupAnchors: false
indvd00m@0
    81
		},
indvd00m@0
    82
		table: {
indvd00m@0
    83
			name: 'Table',
indvd00m@0
    84
			anchorTextTemplate: 'Table ${number}. ${name}.',
indvd00m@0
    85
			linkTextTemplate: '${number}',
indvd00m@0
    86
			numeration: {
indvd00m@0
    87
				enabled: true,
indvd00m@0
    88
				firstNumber: '1',
indvd00m@0
    89
				increase: function(number) {
indvd00m@0
    90
					var n = parseInt(number);
indvd00m@0
    91
					return ++n;
indvd00m@0
    92
				}
indvd00m@0
    93
			},
indvd00m@0
    94
			anchorsProvider: 'default',
indvd00m@0
    95
			allowCreateAnchors: true,
indvd00m@0
    96
			groupAnchors: false
indvd00m@0
    97
		},
indvd00m@0
    98
		reference: {
indvd00m@0
    99
			name: 'Reference',
indvd00m@0
   100
			anchorTextTemplate: '[${number}] ${name}.',
indvd00m@0
   101
			linkTextTemplate: '[${number}]',
indvd00m@0
   102
			numeration: {
indvd00m@0
   103
				enabled: true,
indvd00m@0
   104
				firstNumber: '1',
indvd00m@0
   105
				increase: function(number) {
indvd00m@0
   106
					var n = parseInt(number);
indvd00m@0
   107
					return ++n;
indvd00m@0
   108
				}
indvd00m@0
   109
			},
indvd00m@0
   110
			anchorsProvider: 'default',
indvd00m@0
   111
			allowCreateAnchors: true,
indvd00m@0
   112
			groupAnchors: false
indvd00m@0
   113
		}
indvd00m@0
   114
	}
indvd00m@0
   115
}
indvd00m@0
   116
```
indvd00m@0
   117
| Property | Description | Type | Default value |
indvd00m@0
   118
| --- | --- | --- | --- |
indvd00m@0
   119
| `activeTypes` | Which type of anchors would be activated. | Array | `['chapter', 'image', 'table', 'reference']` |
indvd00m@0
   120
| `overrideTypes` | If you define your own types, enabling this option lead to mixing of your types with types from default config which not yet defined in your config. | Boolean | false |
indvd00m@0
   121
| `types` | Types definition. | Object | see [Default config](https://github.com/indvd00m/crossreference#default-config) section|
indvd00m@0
   122
indvd00m@0
   123
### Example of type definition
indvd00m@0
   124
indvd00m@0
   125
```javascript
indvd00m@0
   126
image: {
indvd00m@0
   127
	name: 'Figure',
indvd00m@0
   128
	anchorTextTemplate: 'Fig. ${number}. ${name}.',
indvd00m@0
   129
	linkTextTemplate: '${number}',
indvd00m@0
   130
	numeration: {
indvd00m@0
   131
		enabled: true,
indvd00m@0
   132
		firstNumber: '1',
indvd00m@0
   133
		increase: function(number) {
indvd00m@0
   134
			var n = parseInt(number);
indvd00m@0
   135
			return ++n;
indvd00m@0
   136
		}
indvd00m@0
   137
	},
indvd00m@0
   138
	anchorsProvider: 'default',
indvd00m@0
   139
	allowCreateAnchors: true,
indvd00m@0
   140
	groupAnchors: false
indvd00m@0
   141
}
indvd00m@0
   142
```
indvd00m@0
   143
| Property | Description | Type | Required |
indvd00m@0
   144
| --- | --- | --- | --- |
indvd00m@0
   145
| `name` | Type name. | String | Yes |
indvd00m@0
   146
| `anchorTextTemplate` | Template for anchor text. This text will be put in `a` tag. You can use variables in format `${variableName}`. Variables is a properties of an anchor object (see [Example of an anchor object](https://github.com/indvd00m/crossreference#example-of-an-anchor-object) section). | String | No |
indvd00m@0
   147
| `linkTextTemplate` | Template for link text. This text will be put in `a` tag. You can use variables in format `${variableName}`. Variables is a properties of an anchor object (see [Example of an anchor object](https://github.com/indvd00m/crossreference#example-of-an-anchor-object) section). | String | No |
indvd00m@0
   148
| `numeration` | Definition of type numeration. See [Example of a numeration config](https://github.com/indvd00m/crossreference#example-of-a-numeration-config) section. | Object | No |
indvd00m@0
   149
| `anchorsProvider` | See [Example of type with anchors provider](https://github.com/indvd00m/crossreference#example-of-type-with-anchors-provider) section. | String 'default' or function | No |
indvd00m@0
   150
| `allowCreateAnchors` | Can user create anchors of this type in anchors dialog. | Boolean | No |
indvd00m@0
   151
| `groupAnchors` | If `true`, anchors can be filtered by group in link dialog. | Boolean | No |
indvd00m@0
   152
indvd00m@0
   153
### Example of a numeration definition
indvd00m@0
   154
indvd00m@0
   155
```javascript
indvd00m@0
   156
numeration: {
indvd00m@0
   157
	enabled: true,
indvd00m@0
   158
	firstNumber: '1',
indvd00m@0
   159
	increase: function(number) {
indvd00m@0
   160
		var n = parseInt(number);
indvd00m@0
   161
		return ++n;
indvd00m@0
   162
	}
indvd00m@0
   163
}
indvd00m@0
   164
```
indvd00m@0
   165
| Property | Description | Type |
indvd00m@0
   166
| --- | --- | --- |
indvd00m@0
   167
| `enabled` | Enabling/disabling numeration of anchors. | Boolean |
indvd00m@0
   168
| `firstNumber` | First number. For example you may define `firstNumber` as `I` for Roman numerals. | String |
indvd00m@0
   169
| `increase` | Function which must return number (as string) which is next after `number` argument (string). | Function |
indvd00m@0
   170
indvd00m@0
   171
### Example of an anchor object
indvd00m@0
   172
indvd00m@0
   173
JSON object:
indvd00m@0
   174
indvd00m@0
   175
```javascript
indvd00m@0
   176
{
indvd00m@0
   177
	type: 'image',
indvd00m@0
   178
	guid: '7d24373b-0756-481d-bf97-5a17ffdf3a28',
indvd00m@0
   179
	name: 'Experimental result',
indvd00m@0
   180
	number: '1',
indvd00m@0
   181
	text: 'Fig. 1. Experimental result.',
indvd00m@0
   182
	groupName: 'Group name',
indvd00m@0
   183
	groupGuid: '6c848dff-cde3-421f-b926-695c8de37d80'
indvd00m@0
   184
}
indvd00m@0
   185
```
indvd00m@0
   186
| Property | Description | Type | Required |
indvd00m@0
   187
| --- | --- | --- | --- |
indvd00m@0
   188
| `type` | Type name of this anchor (`config.types.typeName`). | String | Yes |
indvd00m@0
   189
| `guid` | Unique guid of this anchor. | String | Yes |
indvd00m@0
   190
| `name` | Name of this anchor. | String | Yes |
indvd00m@0
   191
| `number` | Number of this anchor (if type contains numeration definition). | String | Depends of type |
indvd00m@0
   192
| `text` | Text of this anchor. Optional property because of text will generated by `type.anchorTextTemplate` template. | String | No |
indvd00m@0
   193
| `groupName` | Name of anchor group. | String | No |
indvd00m@0
   194
| `groupGuid` | Unique guid of anchor group. | String | No |
indvd00m@0
   195
indvd00m@0
   196
### Example of type with anchors provider
indvd00m@0
   197
indvd00m@0
   198
You can define your own anchors provider. By default plugin search anchors in content of editor and use this anchors for links. But if you want refer to anchors outside of editor you can define another type of anchor with `anchorsProvider` function.
indvd00m@0
   199
indvd00m@0
   200
```javascript
indvd00m@0
   201
myType: {
indvd00m@0
   202
	name: 'My type',
indvd00m@0
   203
	anchorTextTemplate: '${name}',
indvd00m@0
   204
	linkTextTemplate: '${name}',
indvd00m@0
   205
	anchorsProvider: function(callback, editorAnchors, type, editor) {
indvd00m@0
   206
		var anchors = [];
indvd00m@0
   207
		anchors.push({
indvd00m@0
   208
			type: 'myType',
indvd00m@0
   209
			guid: '7d24373b-0756-481d-bf97-5a17ffdf3a28',
indvd00m@0
   210
			name: 'Anchor name',
indvd00m@0
   211
			number: '1'
indvd00m@0
   212
		});
indvd00m@0
   213
		callback(anchors);
indvd00m@0
   214
	},
indvd00m@0
   215
	allowCreateAnchors: false,
indvd00m@0
   216
	groupAnchors: false
indvd00m@0
   217
},
indvd00m@0
   218
```
indvd00m@0
   219
indvd00m@0
   220
`anchorsProvider` method attributes:
indvd00m@0
   221
indvd00m@0
   222
| Name | Description | Type |
indvd00m@0
   223
| --- | --- | --- |
indvd00m@0
   224
| `callback` | Callback method which must be called with arrays of anchors as argument. | Function |
indvd00m@0
   225
| `editorAnchors` | Anchors of this type (`myType` in this case) which already contains in editor. You can merge this anchors with your own anchors if need. | Array |
indvd00m@0
   226
| `type` | Type definition (Object `myType` in this case). | Object |
indvd00m@0
   227
| `editor` | Instance of ckeditor. | Object |
indvd00m@0
   228
indvd00m@0
   229
indvd00m@0
   230
## Requirements
indvd00m@0
   231
indvd00m@0
   232
CrossReference Plugin require CKEditor 4.5+ version and dependent from plugins: dialog, notification.
indvd00m@0
   233
indvd00m@0
   234
## Installation
indvd00m@0
   235
indvd00m@0
   236
 1. Download the plugin.
indvd00m@0
   237
 
indvd00m@0
   238
 2. Extract (decompress) the downloaded file into the plugins folder of your
indvd00m@0
   239
	CKEditor installation.
indvd00m@0
   240
	Example: http://example.com/ckeditor/plugins/crossreference
indvd00m@0
   241
	
indvd00m@0
   242
 3. Enable the plugin by using the extraPlugins configuration setting.
indvd00m@0
   243
	Example: CKEDITOR.config.extraPlugins = 'crossreference';
indvd00m@0
   244
indvd00m@0
   245
## Roadmap
indvd00m@0
   246
indvd00m@0
   247
See https://github.com/indvd00m/crossreference/issues.
indvd00m@0
   248
indvd00m@0
   249
## Icons:
indvd00m@0
   250
 
indvd00m@0
   251
https://icons8.com/web-app/15117/anchor
indvd00m@0
   252
indvd00m@0
   253
https://icons8.com/web-app/38051/link
indvd00m@0
   254
indvd00m@0
   255
https://icons8.com/web-app/21100/refresh
indvd00m@0
   256
indvd00m@0
   257
## License & Author
indvd00m@0
   258
indvd00m@0
   259
CrossReference Plugin is distributed under GPL/LGPL/MPL. For license terms, see LICENSE.md.
indvd00m@0
   260
indvd00m@0
   261
CrossReference Plugin is written by David E. Veliev.