/
hcard.js
210 lines (180 loc) · 5.28 KB
/
hcard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/* globals document */
import { Plugin } from '@ckeditor/ckeditor5-core';
import { UpcastWriter } from '@ckeditor/ckeditor5-engine';
import {
Widget,
toWidget,
viewToModelPositionOutsideModelElement
} from '@ckeditor/ckeditor5-widget';
export class HCardEditing extends Plugin {
static get requires() {
return [ Widget ];
}
init() {
this._defineSchema();
this._defineConverters();
this._defineClipboardInputOutput();
// View-to-model position mapping is needed because an h-card element in the model is represented by a single element,
// but in the view it is a more complex structure.
this.editor.editing.mapper.on(
'viewToModelPosition',
viewToModelPositionOutsideModelElement(
this.editor.model,
viewElement => viewElement.hasClass( 'h-card' )
)
);
}
_defineSchema() {
this.editor.model.schema.register( 'h-card', {
allowWhere: '$text',
isInline: true,
isObject: true,
allowAttributes: [ 'email', 'name', 'tel' ]
} );
}
_defineConverters() {
const conversion = this.editor.conversion;
// Data-to-model conversion.
conversion.for( 'upcast' ).elementToElement( {
view: {
name: 'span',
classes: [ 'h-card' ]
},
model: ( viewElement, { writer } ) => {
return writer.createElement(
'h-card',
getCardDataFromViewElement( viewElement )
);
}
} );
// Model-to-data conversion.
conversion.for( 'dataDowncast' ).elementToElement( {
model: 'h-card',
view: ( modelItem, { writer: viewWriter } ) =>
createCardView( modelItem, viewWriter )
} );
// Model-to-view conversion.
conversion.for( 'editingDowncast' ).elementToElement( {
model: 'h-card',
view: ( modelItem, { writer: viewWriter } ) =>
toWidget( createCardView( modelItem, viewWriter ), viewWriter )
} );
// Helper method for both downcast converters.
function createCardView( modelItem, viewWriter ) {
const email = modelItem.getAttribute( 'email' );
const name = modelItem.getAttribute( 'name' );
const tel = modelItem.getAttribute( 'tel' );
const cardView = viewWriter.createContainerElement( 'span', {
class: 'h-card'
} );
const linkView = viewWriter.createContainerElement( 'a', {
href: `mailto:${ email }`,
class: 'p-name u-email'
} );
const phoneView = viewWriter.createContainerElement( 'span', {
class: 'p-tel'
} );
viewWriter.insert(
viewWriter.createPositionAt( linkView, 0 ),
viewWriter.createText( name )
);
viewWriter.insert(
viewWriter.createPositionAt( phoneView, 0 ),
viewWriter.createText( tel )
);
viewWriter.insert(
viewWriter.createPositionAt( cardView, 0 ),
linkView
);
viewWriter.insert(
viewWriter.createPositionAt( cardView, 'end' ),
phoneView
);
return cardView;
}
}
// Integration with the clipboard pipeline.
_defineClipboardInputOutput() {
const view = this.editor.editing.view;
const viewDocument = view.document;
// Processing pasted or dropped content.
this.listenTo( viewDocument, 'clipboardInput', ( evt, data ) => {
// The clipboard content was already processed by the listener on the higher priority
// (for example while pasting into the code block).
if ( data.content ) {
return;
}
const contactData = data.dataTransfer.getData( 'contact' );
if ( !contactData ) {
return;
}
// Use JSON data encoded in the DataTransfer.
const contact = JSON.parse( contactData );
// Translate the h-card data to a view fragment.
const writer = new UpcastWriter( viewDocument );
const fragment = writer.createDocumentFragment();
writer.appendChild(
writer.createElement( 'span', { class: 'h-card' }, [
writer.createElement(
'a',
{
href: `mailto:${ contact.email }`,
class: 'p-name u-email'
},
contact.name
),
writer.createElement(
'span',
{ class: 'p-tel' },
contact.tel
)
] ),
fragment
);
// Provide the content to the clipboard pipeline for further processing.
data.content = fragment;
} );
// Processing copied, pasted or dragged content.
this.listenTo( document, 'clipboardOutput', ( evt, data ) => {
if ( data.content.childCount != 1 ) {
return;
}
const viewElement = data.content.getChild( 0 );
if (
viewElement.is( 'element', 'span' ) &&
viewElement.hasClass( 'h-card' )
) {
data.dataTransfer.setData(
'contact',
JSON.stringify( getCardDataFromViewElement( viewElement ) )
);
}
} );
}
}
//
// H-Card helper functions.
//
function getCardDataFromViewElement( viewElement ) {
const children = Array.from( viewElement.getChildren() );
const linkElement = children.find(
element => element.is( 'element', 'a' ) && element.hasClass( 'p-name' )
);
const telElement = children.find(
element => element.is( 'element', 'span' ) && element.hasClass( 'p-tel' )
);
return {
name: getText( linkElement ),
tel: getText( telElement ),
email: linkElement.getAttribute( 'href' ).replace( /^mailto:/i, '' )
};
}
function getText( viewElement ) {
return Array.from( viewElement.getChildren() )
.map( node => ( node.is( '$text' ) ? node.data : '' ) )
.join( '' );
}