Annotation of modules/damieng/graphical_editor/loncapa_daxe/web/loncapa_daxe.dart, revision 1.21
1.1 damieng 1: /*
2: This file is part of LON-CAPA.
3:
4: LON-CAPA is free software: you can redistribute it and/or modify
5: it under the terms of the GNU General Public License as published by
6: the Free Software Foundation, either version 3 of the License, or
7: (at your option) any later version.
8:
9: LON-CAPA is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with LON-CAPA. If not, see <http://www.gnu.org/licenses/>.
16: */
17:
18: library loncapa_daxe;
19:
20: import 'dart:async';
21: import 'dart:collection';
22: import 'dart:html' as h;
23: import 'package:daxe/daxe.dart';
24: import 'package:daxe/src/xmldom/xmldom.dart' as x;
25: import 'package:daxe/src/strings.dart';
1.14 damieng 26: import 'package:daxe/src/nodes/nodes.dart' show DNCData, DNText, SimpleTypeControl, ParentUpdatingDNText, DNHiddenP, DNString;
1.1 damieng 27: import 'dart:js' as js;
28:
29: import 'lcd_strings.dart';
30: part 'nodes/lcd_block.dart';
31: part 'nodes/lcd_parameter.dart';
32: part 'nodes/tex_mathjax.dart';
33: part 'nodes/lm.dart';
1.14 damieng 34: part 'nodes/chem.dart';
1.2 damieng 35: part 'nodes/numerical_response.dart';
1.3 damieng 36: part 'nodes/formula_response.dart';
37: part 'nodes/string_response.dart';
1.11 damieng 38: part 'nodes/essay_response.dart';
39: part 'nodes/textfield.dart';
1.1 damieng 40: part 'nodes/radio_response.dart';
41: part 'nodes/radio_foilgroup.dart';
42: part 'nodes/radio_foil.dart';
43: part 'nodes/option_response.dart';
44: part 'nodes/option_foilgroup.dart';
45: part 'nodes/option_foil.dart';
1.6 damieng 46: part 'nodes/match_response.dart';
47: part 'nodes/match_foilgroup.dart';
48: part 'nodes/match_foil.dart';
49: part 'nodes/itemgroup.dart';
50: part 'nodes/match_item.dart';
1.1 damieng 51: part 'nodes/rank_response.dart';
52: part 'nodes/rank_foilgroup.dart';
53: part 'nodes/rank_foil.dart';
1.10 damieng 54: part 'nodes/math_response.dart';
55: part 'nodes/math_answer.dart';
1.13 damieng 56: part 'nodes/organic_response.dart';
57: part 'nodes/jsme_dialog.dart';
58: part 'nodes/reaction_response.dart';
1.5 damieng 59: part 'nodes/textline.dart';
1.1 damieng 60: part 'nodes/hintgroup.dart';
61: part 'nodes/simple_ui_text.dart';
62: part 'nodes/script_block.dart';
1.2 damieng 63: part 'nodes/simple_ui_exception.dart';
1.7 damieng 64: part 'nodes/gnuplot.dart';
65: part 'nodes/fake_attribute_element.dart';
66: part 'nodes/plot_axis.dart';
67: part 'nodes/plot_curve.dart';
1.17 damieng 68: part 'nodes/section.dart';
69: part 'nodes/h1.dart';
1.1 damieng 70: part 'lcd_button.dart';
71:
1.18 damieng 72: List<String> knownSectionRoles = ['activity', 'advice', 'bibliography', 'citation',
73: 'conclusion', 'definition', 'demonstration', 'example',
74: 'explanation', 'introduction', 'method', 'more_information',
75: 'objectives', 'prerequisites', 'remark', 'reminder',
76: 'summary', 'syntax', 'warning'];
77:
1.1 damieng 78:
79: void main() {
80: NodeFactory.addCoreDisplayTypes();
81:
82: addDisplayType('lcdblock',
83: (x.Element ref) => new LCDBlock.fromRef(ref),
84: (x.Node node, DaxeNode parent) => new LCDBlock.fromNode(node, parent)
85: );
86:
87: addDisplayType('texmathjax',
88: (x.Element ref) => new TeXMathJax.fromRef(ref),
89: (x.Node node, DaxeNode parent) => new TeXMathJax.fromNode(node, parent)
90: );
91:
92: addDisplayType('lm',
93: (x.Element ref) => new Lm.fromRef(ref),
94: (x.Node node, DaxeNode parent) => new Lm.fromNode(node, parent)
95: );
96:
1.14 damieng 97: addDisplayType('chem',
98: (x.Element ref) => new Chem.fromRef(ref),
99: (x.Node node, DaxeNode parent) => new Chem.fromNode(node, parent)
100: );
101:
1.1 damieng 102: addDisplayType('script',
103: (x.Element ref) => new ScriptBlock.fromRef(ref),
104: (x.Node node, DaxeNode parent) => new ScriptBlock.fromNode(node, parent)
105: );
106:
107: addDisplayType('parameter',
108: (x.Element ref) => new LCDParameter.fromRef(ref),
109: (x.Node node, DaxeNode parent) => new LCDParameter.fromNode(node, parent)
110: );
111:
1.2 damieng 112: addDisplayType('numericalresponse',
113: (x.Element ref) => new NumericalResponse.fromRef(ref),
114: (x.Node node, DaxeNode parent) => new NumericalResponse.fromNode(node, parent)
115: );
116:
1.3 damieng 117: addDisplayType('formularesponse',
118: (x.Element ref) => new FormulaResponse.fromRef(ref),
119: (x.Node node, DaxeNode parent) => new FormulaResponse.fromNode(node, parent)
120: );
121:
122: addDisplayType('stringresponse',
123: (x.Element ref) => new StringResponse.fromRef(ref),
124: (x.Node node, DaxeNode parent) => new StringResponse.fromNode(node, parent)
125: );
126:
1.11 damieng 127: addDisplayType('essayresponse',
128: (x.Element ref) => new EssayResponse.fromRef(ref),
129: (x.Node node, DaxeNode parent) => new EssayResponse.fromNode(node, parent)
130: );
131:
1.1 damieng 132: addDisplayType('radioresponse',
133: (x.Element ref) => new RadioResponse.fromRef(ref),
134: (x.Node node, DaxeNode parent) => new RadioResponse.fromNode(node, parent)
135: );
136:
137: addDisplayType('optionresponse',
138: (x.Element ref) => new OptionResponse.fromRef(ref),
139: (x.Node node, DaxeNode parent) => new OptionResponse.fromNode(node, parent)
140: );
141:
1.6 damieng 142: addDisplayType('matchresponse',
143: (x.Element ref) => new MatchResponse.fromRef(ref),
144: (x.Node node, DaxeNode parent) => new MatchResponse.fromNode(node, parent)
145: );
146:
1.1 damieng 147: addDisplayType('rankresponse',
148: (x.Element ref) => new RankResponse.fromRef(ref),
149: (x.Node node, DaxeNode parent) => new RankResponse.fromNode(node, parent)
150: );
151:
1.10 damieng 152: addDisplayType('mathresponse',
153: (x.Element ref) => new MathResponse.fromRef(ref),
154: (x.Node node, DaxeNode parent) => new MathResponse.fromNode(node, parent)
155: );
156:
1.13 damieng 157: addDisplayType('organicresponse',
158: (x.Element ref) => new OrganicResponse.fromRef(ref),
159: (x.Node node, DaxeNode parent) => new OrganicResponse.fromNode(node, parent)
160: );
161:
162: addDisplayType('reactionresponse',
163: (x.Element ref) => new ReactionResponse.fromRef(ref),
164: (x.Node node, DaxeNode parent) => new ReactionResponse.fromNode(node, parent)
165: );
166:
1.1 damieng 167: addDisplayType('foilgroup',
168: (x.Element ref) {
169: if (ref.getAttribute('type') == 'radiobuttonresponse--foilgroup')
170: return new RadioFoilgroup.fromRef(ref);
171: else if (ref.getAttribute('type') == 'optionresponse--foilgroup')
172: return new OptionFoilgroup.fromRef(ref);
1.6 damieng 173: else if (ref.getAttribute('type') == 'matchresponse--foilgroup')
174: return new MatchFoilgroup.fromRef(ref);
1.1 damieng 175: else if (ref.getAttribute('type') == 'rankresponse--foilgroup')
176: return new RankFoilgroup.fromRef(ref);
177: return new LCDBlock.fromRef(ref);
178: },
179: (x.Node node, DaxeNode parent) {
180: if (parent is RadioResponse)
181: return new RadioFoilgroup.fromNode(node, parent);
182: else if (parent is OptionResponse)
183: return new OptionFoilgroup.fromNode(node, parent);
1.6 damieng 184: else if (parent is MatchResponse)
185: return new MatchFoilgroup.fromNode(node, parent);
1.1 damieng 186: else if (parent is RankResponse)
187: return new RankFoilgroup.fromNode(node, parent);
188: return new LCDBlock.fromNode(node, parent);
189: }
190: );
191:
192: addDisplayType('foil',
193: (x.Element ref) {
194: if (ref.getAttribute('type') == 'radiobuttonresponse--foil')
195: return new RadioFoil.fromRef(ref);
196: else if (ref.getAttribute('type') == 'optionresponse--foil')
197: return new OptionFoil.fromRef(ref);
1.6 damieng 198: else if (ref.getAttribute('type') == 'matchresponse--foil')
199: return new MatchFoil.fromRef(ref);
1.1 damieng 200: else if (ref.getAttribute('type') == 'rankresponse--foil')
201: return new RankFoil.fromRef(ref);
202: return new LCDBlock.fromRef(ref);
203: },
204: (x.Node node, DaxeNode parent) {
205: if (parent is RadioFoilgroup)
206: return new RadioFoil.fromNode(node, parent);
1.19 damieng 207: else if (parent is OptionFoilgroup ||
208: (parent.parent != null && parent.parent is OptionFoilgroup))
1.1 damieng 209: return new OptionFoil.fromNode(node, parent);
1.19 damieng 210: else if (parent is MatchFoilgroup ||
211: (parent.parent != null && parent.parent is MatchFoilgroup))
1.6 damieng 212: return new MatchFoil.fromNode(node, parent);
1.1 damieng 213: else if (parent is RankFoilgroup)
214: return new RankFoil.fromNode(node, parent);
215: return new LCDBlock.fromNode(node, parent);
216: }
217: );
218:
1.6 damieng 219: addDisplayType('itemgroup',
220: (x.Element ref) => new Itemgroup.fromRef(ref),
221: (x.Node node, DaxeNode parent) => new Itemgroup.fromNode(node, parent)
222: );
223:
224: addDisplayType('matchitem',
225: (x.Element ref) => new MatchItem.fromRef(ref),
226: (x.Node node, DaxeNode parent) => new MatchItem.fromNode(node, parent)
227: );
228:
1.5 damieng 229: addDisplayType('textline',
230: (x.Element ref) => new Textline.fromRef(ref),
231: (x.Node node, DaxeNode parent) => new Textline.fromNode(node, parent)
232: );
233:
1.11 damieng 234: addDisplayType('textfield',
235: (x.Element ref) => new Textfield.fromRef(ref),
236: (x.Node node, DaxeNode parent) => new Textfield.fromNode(node, parent)
237: );
238:
1.1 damieng 239: addDisplayType('hintgroup',
240: (x.Element ref) => new Hintgroup.fromRef(ref),
241: (x.Node node, DaxeNode parent) => new Hintgroup.fromNode(node, parent)
242: );
243:
1.10 damieng 244: addDisplayType('answer',
245: (x.Element ref) {
246: if (ref.getAttribute('type') == 'mathresponse--answer')
247: return new MathAnswer.fromRef(ref);
248: return new LCDBlock.fromRef(ref);
249: },
250: (x.Node node, DaxeNode parent) {
251: if (parent is MathResponse)
252: return new MathAnswer.fromNode(node, parent);
253: return new LCDBlock.fromNode(node, parent);
254: }
255: );
256:
1.7 damieng 257: addDisplayType('gnuplot',
258: (x.Element ref) => new Gnuplot.fromRef(ref),
259: (x.Node node, DaxeNode parent) => new Gnuplot.fromNode(node, parent)
260: );
261:
262: addDisplayType('fakeattribute',
263: (x.Element ref) {
264: if (doc.cfg.elementName(ref) != 'title' || ref.getAttribute('type') == 'lonplot--title')
265: return new FakeAttributeElement.fromRef(ref);
266: return new LCDBlock.fromRef(ref);
267: },
268: (x.Node node, DaxeNode parent) {
269: if (parent is Gnuplot || parent is PlotCurve)
270: return new FakeAttributeElement.fromNode(node, parent);
271: return new LCDBlock.fromNode(node, parent);
272: }
273: );
274:
275: addDisplayType('plotaxis',
276: (x.Element ref) => new PlotAxis.fromRef(ref),
277: (x.Node node, DaxeNode parent) => new PlotAxis.fromNode(node, parent)
278: );
279:
280: addDisplayType('plotcurve',
281: (x.Element ref) => new PlotCurve.fromRef(ref),
282: (x.Node node, DaxeNode parent) => new PlotCurve.fromNode(node, parent)
283: );
284:
1.17 damieng 285: addDisplayType('section',
286: (x.Element ref) => new Section.fromRef(ref),
287: (x.Node node, DaxeNode parent) => new Section.fromNode(node, parent)
288: );
289:
290: addDisplayType('h1',
291: (x.Element ref) => new H1.fromRef(ref),
292: (x.Node node, DaxeNode parent) => new H1.fromNode(node, parent)
293: );
294:
1.1 damieng 295: Future.wait([Strings.load(), LCDStrings.load(), _readTemplates('templates.xml')]).then((List responses) {
1.16 damieng 296: initDaxe().then((v) {
1.1 damieng 297: // add things to the toolbar
298: ToolbarMenu sectionMenu = _makeSectionMenu();
299: if (sectionMenu != null)
300: page.toolbar.add(sectionMenu);
301: x.Element texRef = doc.cfg.elementReference('m');
302: if (texRef != null) {
303: ToolbarBox insertBox = new ToolbarBox();
304: ToolbarButton texButton = new ToolbarButton(
305: LCDStrings.get('tex_equation'), 'images/tex.png',
306: () => doc.insertNewNode(texRef, 'element'), Toolbar.insertButtonUpdate,
307: data:new ToolbarStyleInfo([texRef], null, null));
308: insertBox.add(texButton);
309: page.toolbar.add(insertBox);
310: }
311: h.Element tbh = h.querySelector('.toolbar');
312: tbh.replaceWith(page.toolbar.html());
313: page.adjustPositionsUnderToolbar();
314: page.updateAfterPathChange();
315: // add things to the menubar
316: if (responses[2] is x.Document) {
317: // at this point the menubar html is already in the document, so we have to fix the HTML
318: h.Element menubarDiv = h.document.getElementsByClassName('menubar')[0];
1.4 damieng 319: if (doc.filePath != null && doc.filePath.indexOf('&url=') != -1) { // otherwise we are not on LON-CAPA
1.1 damieng 320: MenuItem item = new MenuItem(Strings.get('menu.save'), () => save(), shortcut: 'S');
321: Menu fileMenu = page.mbar.menus[0];
322: fileMenu.add(item);
323: menubarDiv.firstChild.replaceWith(page.mbar.createMenuDiv(fileMenu));
1.21 ! damieng 324: h.window.onBeforeUnload.listen((h.BeforeUnloadEvent e) {
! 325: if (doc.changed()) {
! 326: String message = Strings.get('save.document_not_saved');
! 327: e.returnValue = message;
! 328: return message;
! 329: }
! 330: });
1.1 damieng 331: }
332: Menu m = _makeTemplatesMenu(responses[2]);
333: page.mbar.add(m);
334: menubarDiv.append(page.mbar.createMenuDiv(m));
335: page.updateAfterPathChange();
336: } else
337: print("Error reading templates file, could not build the menu.");
1.15 damieng 338: }).catchError((e) {
339: h.document.body.appendText('Initialization error: ' + e);
1.1 damieng 340: });
1.15 damieng 341: }).catchError((e) {
1.20 damieng 342: h.document.body.appendText('Initialization error: ' + (e is String ? e : e.toString()));
1.1 damieng 343: });
344: }
345:
346: void save() {
347: saveOnLONCAPA().then((_) {
348: h.window.alert(Strings.get('save.success'));
349: }, onError: (DaxeException ex) {
350: h.window.alert(Strings.get('save.error') + ': ' + ex.message);
351: });
352: }
353:
354: /**
355: * Send the document with a POST request to LON-CAPA.
1.21 ! damieng 356: * Note: is this needed ? It depends on the server implementation...
1.1 damieng 357: */
358: Future saveOnLONCAPA() {
359: int ind = doc.filePath.indexOf('&url=');
360: if (ind == -1)
361: return(new Future.error(new DaxeException('bad URL')));
362: String path = doc.filePath.substring(ind+5);
363: path = Uri.decodeQueryComponent(path);
364: ind = path.lastIndexOf('/');
365: String filename;
366: if (ind == -1)
367: filename = path;
368: else {
369: filename = path.substring(ind+1);
370: path = path.substring(0, ind+1);
371: }
372: Completer completer = new Completer();
373: String bound = 'AaB03x';
374: h.HttpRequest request = new h.HttpRequest();
375: request.onLoad.listen((h.ProgressEvent event) {
1.21 ! damieng 376: // TODO: check for something, status is sometimes wrongly OK
! 377: if (doc.undoPosition >= 0)
! 378: doc.lastSavedEdit = doc.edits[doc.undoPosition];
! 379: else
! 380: doc.lastSavedEdit = null;
! 381: completer.complete();
1.1 damieng 382: });
383: request.onError.listen((h.ProgressEvent event) {
384: completer.completeError(new DaxeException(request.status.toString()));
385: });
386: request.open('POST', '/upload_file');
387: request.setRequestHeader('Content-Type', "multipart/form-data; boundary=$bound");
388:
389: StringBuffer sb = new StringBuffer();
390: sb.write("--$bound\r\n");
391: sb.write('Content-Disposition: form-data; name="uploads_path"\r\n');
392: sb.write('Content-type: text/plain; charset=UTF-8\r\n');
393: sb.write('Content-transfer-encoding: 8bit\r\n\r\n');
394: sb.write(path);
395: sb.write("\r\n--$bound\r\n");
396: sb.write('Content-Disposition: form-data; name="uploads"; filename="$filename"\r\n');
397: sb.write('Content-Type: application/octet-stream\r\n\r\n');
398: doc.dndoc.xmlEncoding = 'UTF-8'; // the document is forced to use UTF-8
399: sb.write(doc.toString());
400: sb.write('\r\n--$bound--\r\n\r\n');
401: request.send(sb.toString());
402: return(completer.future);
403: }
404:
405: ToolbarMenu _makeSectionMenu() {
1.18 damieng 406: // sort section roles by title
407: knownSectionRoles.sort((String role1, String role2) => LCDStrings.get(role1).compareTo(LCDStrings.get(role2)));
408:
1.1 damieng 409: Menu menu = new Menu(LCDStrings.get('Section'));
410: List<x.Element> sectionRefs = doc.cfg.elementReferences('section');
411: if (sectionRefs == null || sectionRefs.length == 0)
412: return(null);
413: x.Element h1Ref = doc.cfg.elementReference('h1');
1.18 damieng 414: for (String role in knownSectionRoles) {
1.1 damieng 415: MenuItem menuItem = new MenuItem(LCDStrings.get(role), null,
416: data:new ToolbarStyleInfo(sectionRefs, null, null));
417: menuItem.action = () {
418: ToolbarStyleInfo info = menuItem.data;
419: x.Element sectionRef = info.validRef;
1.18 damieng 420: Section section = new Section.fromRef(sectionRef);
1.1 damieng 421: section.state = 1;
422: section.setAttribute('class', 'role-' + role);
1.17 damieng 423: H1 h1 = NodeFactory.create(h1Ref);
1.1 damieng 424: h1.state = 1;
1.18 damieng 425: h1.appendChild(new DNText(LCDStrings.get(role)));
1.9 damieng 426: section.appendChild(h1);
427: x.Element hiddenp = doc.cfg.findSubElement(sectionRef, doc.hiddenParaRefs);
1.18 damieng 428: DNHiddenP p = new DNHiddenP.fromRef(hiddenp);
1.9 damieng 429: section.appendChild(p);
1.17 damieng 430: section.setupSimpleUI();
1.1 damieng 431: if (doc.insert2(section, page.getSelectionStart())) {
1.18 damieng 432: page.cursor.moveTo(new Position(p, 0));
1.1 damieng 433: page.updateAfterPathChange();
434: }
435: };
436: menu.add(menuItem);
437: }
438: ToolbarMenu tbmenu = new ToolbarMenu(menu, Toolbar.insertMenuUpdate, page.toolbar);
439: return(tbmenu);
440: }
441:
442: Future<x.Document> _readTemplates(String templatesPath) {
443: x.DOMParser dp = new x.DOMParser();
444: return(dp.parseFromURL(templatesPath));
445: }
446:
447: Menu _makeTemplatesMenu(x.Document templatesDoc) {
448: Menu menu = new Menu(LCDStrings.get('Templates'));
449: x.Element templates = templatesDoc.documentElement;
450: for (x.Node child in templates.childNodes) {
451: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'menu') {
452: menu.add(_makeMenu(child));
453: }
454: }
455: return(menu);
456: }
457:
458: Menu _makeMenu(x.Element el) {
459: String locale = LCDStrings.systemLocale;
460: String defaultLocale = LCDStrings.defaultLocale;
461: String title;
462: for (x.Node child in el.childNodes) {
463: if (child.nodeType == x.Node.ELEMENT_NODE && child.nodeName == 'title') {
464: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
465: if ((child as x.Element).getAttribute('lang') == locale) {
466: title = child.firstChild.nodeValue;
467: break;
468: } else if ((child as x.Element).getAttribute('lang') == defaultLocale) {
469: title = child.firstChild.nodeValue;
470: }
471: }
472: }
473: }
474: if (title == null)
475: title = '?';
476: Menu menu = new Menu(title);
477: for (x.Node child in el.childNodes) {
478: if (child.nodeType == x.Node.ELEMENT_NODE) {
479: if (child.nodeName == 'menu') {
480: menu.add(_makeMenu(child));
481: } else if (child.nodeName == 'item') {
482: menu.add(_makeItem(child));
483: }
484: }
485: }
486: return(menu);
487: }
488:
489: MenuItem _makeItem(x.Element item) {
490: String locale = LCDStrings.systemLocale;
491: String defaultLocale = LCDStrings.defaultLocale;
492: String path, type, title, help;
493: for (x.Node child in item.childNodes) {
494: if (child.nodeType == x.Node.ELEMENT_NODE) {
495: if (child.nodeName == 'title') {
496: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
497: if ((child as x.Element).getAttribute('lang') == locale) {
498: title = child.firstChild.nodeValue;
499: } else if (title == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
500: title = child.firstChild.nodeValue;
501: }
502: }
503: } else if (child.nodeName == 'path' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
504: path = child.firstChild.nodeValue;
505: } else if (child.nodeName == 'type' && child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
506: type = child.firstChild.nodeValue;
507: } else if (child.nodeName == 'help') {
508: if (child.firstChild != null && child.firstChild.nodeType == x.Node.TEXT_NODE) {
509: if ((child as x.Element).getAttribute('lang') == locale) {
510: help = child.firstChild.nodeValue;
511: } else if (help == null && (child as x.Element).getAttribute('lang') == defaultLocale) {
512: help = child.firstChild.nodeValue;
513: }
514: }
515: }
516: }
517: }
518: if (type == null) {
519: print("Warning: missing type for template $title\n");
520: type = 'problem';
521: }
522: x.Element refElement = doc.cfg.elementReference(type);
523: MenuItem menuItem = new MenuItem(title, () => _insertTemplate(path), data: refElement);
524: if (help != null)
525: menuItem.toolTipText = help;
526: return menuItem;
527: }
528:
529: void _insertTemplate(String filePath) {
530: try {
531: x.DOMParser dp = new x.DOMParser();
532: dp.parseFromURL(filePath).then((x.Document templateDoc) {
533: x.Element root = templateDoc.documentElement;
534: if (root == null)
535: return;
536: doc.removeWhitespace(root);
537: DaxeNode dnRoot = NodeFactory.createFromNode(root, null);
538: UndoableEdit edit;
539: Position pos = page.getSelectionStart();
540: if (dnRoot.nodeName == 'loncapa' && doc.getRootElement() != null)
541: edit = doc.insertChildrenEdit(dnRoot, pos, checkValidity:true);
1.12 damieng 542: else {
543: DaxeNode parent = pos.dn;
544: if (parent is DNText)
545: parent = parent.parent;
546: if (parent is DNHiddenP && !doc.cfg.isSubElement(parent.ref, dnRoot.ref)) {
547: // The node must be inserted outside of the paragraph.
548: // If there is something in the paragraph to the right of the cursor, it must be
549: // moved into a new paragraph after the inserted node.
550: // (as in DaxeDocument.insert2)
551: DNHiddenP p = parent;
552: edit = new UndoableEdit.compound(Strings.get('undo.insert_element'));
553: Position pend = new Position(p, p.offsetLength);
554: pend.moveInsideTextNodeIfPossible();
555: if (pos < pend) {
556: DaxeNode clone = doc.cloneBetween(pos, pend);
557: edit.addSubEdit(doc.removeBetweenEdit(pos, pend));
558: DNHiddenP newp = NodeFactory.create(p.ref);
559: edit.addSubEdit(new UndoableEdit.insertNode(
560: new Position(p.parent, p.parent.offsetOf(p) + 1), newp));
561: edit.addSubEdit(doc.insertChildrenEdit(clone, new Position(newp, 0)));
562: }
563: edit.addSubEdit(new UndoableEdit.insertNode(
564: new Position(p.parent, p.parent.offsetOf(p)+1), dnRoot));
565: } else
566: edit = new UndoableEdit.insertNode(pos, dnRoot);
567: }
1.1 damieng 568: doc.doNewEdit(edit);
569: page.updateAfterPathChange();
1.8 damieng 570: if (dnRoot.nodeName != 'loncapa')
571: page.scrollToNode(dnRoot);
1.1 damieng 572: });
573: } on x.DOMException catch(ex) {
574: h.window.alert(ex.toString());
575: }
576: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>