index.cjs 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. 'use strict';
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
  4. function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
  5. function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
  6. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
  7. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  8. function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  9. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
  10. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  11. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
  12. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  13. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  14. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  15. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  16. var prosemirrorModel = require('prosemirror-model');
  17. var lower16 = 0xffff;
  18. var factor16 = Math.pow(2, 16);
  19. function makeRecover(index, offset) {
  20. return index + offset * factor16;
  21. }
  22. function recoverIndex(value) {
  23. return value & lower16;
  24. }
  25. function recoverOffset(value) {
  26. return (value - (value & lower16)) / factor16;
  27. }
  28. var DEL_BEFORE = 1,
  29. DEL_AFTER = 2,
  30. DEL_ACROSS = 4,
  31. DEL_SIDE = 8;
  32. var MapResult = function () {
  33. function MapResult(pos, delInfo, recover) {
  34. _classCallCheck(this, MapResult);
  35. this.pos = pos;
  36. this.delInfo = delInfo;
  37. this.recover = recover;
  38. }
  39. _createClass(MapResult, [{
  40. key: "deleted",
  41. get: function get() {
  42. return (this.delInfo & DEL_SIDE) > 0;
  43. }
  44. }, {
  45. key: "deletedBefore",
  46. get: function get() {
  47. return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0;
  48. }
  49. }, {
  50. key: "deletedAfter",
  51. get: function get() {
  52. return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0;
  53. }
  54. }, {
  55. key: "deletedAcross",
  56. get: function get() {
  57. return (this.delInfo & DEL_ACROSS) > 0;
  58. }
  59. }]);
  60. return MapResult;
  61. }();
  62. var StepMap = function () {
  63. function StepMap(ranges) {
  64. var inverted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  65. _classCallCheck(this, StepMap);
  66. this.ranges = ranges;
  67. this.inverted = inverted;
  68. if (!ranges.length && StepMap.empty) return StepMap.empty;
  69. }
  70. _createClass(StepMap, [{
  71. key: "recover",
  72. value: function recover(value) {
  73. var diff = 0,
  74. index = recoverIndex(value);
  75. if (!this.inverted) for (var i = 0; i < index; i++) {
  76. diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
  77. }
  78. return this.ranges[index * 3] + diff + recoverOffset(value);
  79. }
  80. }, {
  81. key: "mapResult",
  82. value: function mapResult(pos) {
  83. var assoc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  84. return this._map(pos, assoc, false);
  85. }
  86. }, {
  87. key: "map",
  88. value: function map(pos) {
  89. var assoc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  90. return this._map(pos, assoc, true);
  91. }
  92. }, {
  93. key: "_map",
  94. value: function _map(pos, assoc, simple) {
  95. var diff = 0,
  96. oldIndex = this.inverted ? 2 : 1,
  97. newIndex = this.inverted ? 1 : 2;
  98. for (var i = 0; i < this.ranges.length; i += 3) {
  99. var start = this.ranges[i] - (this.inverted ? diff : 0);
  100. if (start > pos) break;
  101. var oldSize = this.ranges[i + oldIndex],
  102. newSize = this.ranges[i + newIndex],
  103. end = start + oldSize;
  104. if (pos <= end) {
  105. var side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
  106. var result = start + diff + (side < 0 ? 0 : newSize);
  107. if (simple) return result;
  108. var recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
  109. var del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
  110. if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE;
  111. return new MapResult(result, del, recover);
  112. }
  113. diff += newSize - oldSize;
  114. }
  115. return simple ? pos + diff : new MapResult(pos + diff, 0, null);
  116. }
  117. }, {
  118. key: "touches",
  119. value: function touches(pos, recover) {
  120. var diff = 0,
  121. index = recoverIndex(recover);
  122. var oldIndex = this.inverted ? 2 : 1,
  123. newIndex = this.inverted ? 1 : 2;
  124. for (var i = 0; i < this.ranges.length; i += 3) {
  125. var start = this.ranges[i] - (this.inverted ? diff : 0);
  126. if (start > pos) break;
  127. var oldSize = this.ranges[i + oldIndex],
  128. end = start + oldSize;
  129. if (pos <= end && i == index * 3) return true;
  130. diff += this.ranges[i + newIndex] - oldSize;
  131. }
  132. return false;
  133. }
  134. }, {
  135. key: "forEach",
  136. value: function forEach(f) {
  137. var oldIndex = this.inverted ? 2 : 1,
  138. newIndex = this.inverted ? 1 : 2;
  139. for (var i = 0, diff = 0; i < this.ranges.length; i += 3) {
  140. var start = this.ranges[i],
  141. oldStart = start - (this.inverted ? diff : 0),
  142. newStart = start + (this.inverted ? 0 : diff);
  143. var oldSize = this.ranges[i + oldIndex],
  144. newSize = this.ranges[i + newIndex];
  145. f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
  146. diff += newSize - oldSize;
  147. }
  148. }
  149. }, {
  150. key: "invert",
  151. value: function invert() {
  152. return new StepMap(this.ranges, !this.inverted);
  153. }
  154. }, {
  155. key: "toString",
  156. value: function toString() {
  157. return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
  158. }
  159. }], [{
  160. key: "offset",
  161. value: function offset(n) {
  162. return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
  163. }
  164. }]);
  165. return StepMap;
  166. }();
  167. StepMap.empty = new StepMap([]);
  168. var Mapping = function () {
  169. function Mapping() {
  170. var maps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  171. var mirror = arguments.length > 1 ? arguments[1] : undefined;
  172. var from = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  173. var to = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : maps.length;
  174. _classCallCheck(this, Mapping);
  175. this.maps = maps;
  176. this.mirror = mirror;
  177. this.from = from;
  178. this.to = to;
  179. }
  180. _createClass(Mapping, [{
  181. key: "slice",
  182. value: function slice() {
  183. var from = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  184. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.maps.length;
  185. return new Mapping(this.maps, this.mirror, from, to);
  186. }
  187. }, {
  188. key: "copy",
  189. value: function copy() {
  190. return new Mapping(this.maps.slice(), this.mirror && this.mirror.slice(), this.from, this.to);
  191. }
  192. }, {
  193. key: "appendMap",
  194. value: function appendMap(map, mirrors) {
  195. this.to = this.maps.push(map);
  196. if (mirrors != null) this.setMirror(this.maps.length - 1, mirrors);
  197. }
  198. }, {
  199. key: "appendMapping",
  200. value: function appendMapping(mapping) {
  201. for (var i = 0, startSize = this.maps.length; i < mapping.maps.length; i++) {
  202. var mirr = mapping.getMirror(i);
  203. this.appendMap(mapping.maps[i], mirr != null && mirr < i ? startSize + mirr : undefined);
  204. }
  205. }
  206. }, {
  207. key: "getMirror",
  208. value: function getMirror(n) {
  209. if (this.mirror) for (var i = 0; i < this.mirror.length; i++) {
  210. if (this.mirror[i] == n) return this.mirror[i + (i % 2 ? -1 : 1)];
  211. }
  212. }
  213. }, {
  214. key: "setMirror",
  215. value: function setMirror(n, m) {
  216. if (!this.mirror) this.mirror = [];
  217. this.mirror.push(n, m);
  218. }
  219. }, {
  220. key: "appendMappingInverted",
  221. value: function appendMappingInverted(mapping) {
  222. for (var i = mapping.maps.length - 1, totalSize = this.maps.length + mapping.maps.length; i >= 0; i--) {
  223. var mirr = mapping.getMirror(i);
  224. this.appendMap(mapping.maps[i].invert(), mirr != null && mirr > i ? totalSize - mirr - 1 : undefined);
  225. }
  226. }
  227. }, {
  228. key: "invert",
  229. value: function invert() {
  230. var inverse = new Mapping();
  231. inverse.appendMappingInverted(this);
  232. return inverse;
  233. }
  234. }, {
  235. key: "map",
  236. value: function map(pos) {
  237. var assoc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  238. if (this.mirror) return this._map(pos, assoc, true);
  239. for (var i = this.from; i < this.to; i++) {
  240. pos = this.maps[i].map(pos, assoc);
  241. }
  242. return pos;
  243. }
  244. }, {
  245. key: "mapResult",
  246. value: function mapResult(pos) {
  247. var assoc = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  248. return this._map(pos, assoc, false);
  249. }
  250. }, {
  251. key: "_map",
  252. value: function _map(pos, assoc, simple) {
  253. var delInfo = 0;
  254. for (var i = this.from; i < this.to; i++) {
  255. var map = this.maps[i],
  256. result = map.mapResult(pos, assoc);
  257. if (result.recover != null) {
  258. var corr = this.getMirror(i);
  259. if (corr != null && corr > i && corr < this.to) {
  260. i = corr;
  261. pos = this.maps[corr].recover(result.recover);
  262. continue;
  263. }
  264. }
  265. delInfo |= result.delInfo;
  266. pos = result.pos;
  267. }
  268. return simple ? pos : new MapResult(pos, delInfo, null);
  269. }
  270. }]);
  271. return Mapping;
  272. }();
  273. var stepsByID = Object.create(null);
  274. var Step = function () {
  275. function Step() {
  276. _classCallCheck(this, Step);
  277. }
  278. _createClass(Step, [{
  279. key: "getMap",
  280. value: function getMap() {
  281. return StepMap.empty;
  282. }
  283. }, {
  284. key: "merge",
  285. value: function merge(other) {
  286. return null;
  287. }
  288. }], [{
  289. key: "fromJSON",
  290. value: function fromJSON(schema, json) {
  291. if (!json || !json.stepType) throw new RangeError("Invalid input for Step.fromJSON");
  292. var type = stepsByID[json.stepType];
  293. if (!type) throw new RangeError("No step type ".concat(json.stepType, " defined"));
  294. return type.fromJSON(schema, json);
  295. }
  296. }, {
  297. key: "jsonID",
  298. value: function jsonID(id, stepClass) {
  299. if (id in stepsByID) throw new RangeError("Duplicate use of step JSON ID " + id);
  300. stepsByID[id] = stepClass;
  301. stepClass.prototype.jsonID = id;
  302. return stepClass;
  303. }
  304. }]);
  305. return Step;
  306. }();
  307. var StepResult = function () {
  308. function StepResult(doc, failed) {
  309. _classCallCheck(this, StepResult);
  310. this.doc = doc;
  311. this.failed = failed;
  312. }
  313. _createClass(StepResult, null, [{
  314. key: "ok",
  315. value: function ok(doc) {
  316. return new StepResult(doc, null);
  317. }
  318. }, {
  319. key: "fail",
  320. value: function fail(message) {
  321. return new StepResult(null, message);
  322. }
  323. }, {
  324. key: "fromReplace",
  325. value: function fromReplace(doc, from, to, slice) {
  326. try {
  327. return StepResult.ok(doc.replace(from, to, slice));
  328. } catch (e) {
  329. if (e instanceof prosemirrorModel.ReplaceError) return StepResult.fail(e.message);
  330. throw e;
  331. }
  332. }
  333. }]);
  334. return StepResult;
  335. }();
  336. function mapFragment(fragment, f, parent) {
  337. var mapped = [];
  338. for (var i = 0; i < fragment.childCount; i++) {
  339. var child = fragment.child(i);
  340. if (child.content.size) child = child.copy(mapFragment(child.content, f, child));
  341. if (child.isInline) child = f(child, parent, i);
  342. mapped.push(child);
  343. }
  344. return prosemirrorModel.Fragment.fromArray(mapped);
  345. }
  346. var AddMarkStep = function (_Step) {
  347. _inherits(AddMarkStep, _Step);
  348. var _super = _createSuper(AddMarkStep);
  349. function AddMarkStep(from, to, mark) {
  350. var _this;
  351. _classCallCheck(this, AddMarkStep);
  352. _this = _super.call(this);
  353. _this.from = from;
  354. _this.to = to;
  355. _this.mark = mark;
  356. return _this;
  357. }
  358. _createClass(AddMarkStep, [{
  359. key: "apply",
  360. value: function apply(doc) {
  361. var _this2 = this;
  362. var oldSlice = doc.slice(this.from, this.to),
  363. $from = doc.resolve(this.from);
  364. var parent = $from.node($from.sharedDepth(this.to));
  365. var slice = new prosemirrorModel.Slice(mapFragment(oldSlice.content, function (node, parent) {
  366. if (!node.isAtom || !parent.type.allowsMarkType(_this2.mark.type)) return node;
  367. return node.mark(_this2.mark.addToSet(node.marks));
  368. }, parent), oldSlice.openStart, oldSlice.openEnd);
  369. return StepResult.fromReplace(doc, this.from, this.to, slice);
  370. }
  371. }, {
  372. key: "invert",
  373. value: function invert() {
  374. return new RemoveMarkStep(this.from, this.to, this.mark);
  375. }
  376. }, {
  377. key: "map",
  378. value: function map(mapping) {
  379. var from = mapping.mapResult(this.from, 1),
  380. to = mapping.mapResult(this.to, -1);
  381. if (from.deleted && to.deleted || from.pos >= to.pos) return null;
  382. return new AddMarkStep(from.pos, to.pos, this.mark);
  383. }
  384. }, {
  385. key: "merge",
  386. value: function merge(other) {
  387. if (other instanceof AddMarkStep && other.mark.eq(this.mark) && this.from <= other.to && this.to >= other.from) return new AddMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
  388. return null;
  389. }
  390. }, {
  391. key: "toJSON",
  392. value: function toJSON() {
  393. return {
  394. stepType: "addMark",
  395. mark: this.mark.toJSON(),
  396. from: this.from,
  397. to: this.to
  398. };
  399. }
  400. }], [{
  401. key: "fromJSON",
  402. value: function fromJSON(schema, json) {
  403. if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for AddMarkStep.fromJSON");
  404. return new AddMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
  405. }
  406. }]);
  407. return AddMarkStep;
  408. }(Step);
  409. Step.jsonID("addMark", AddMarkStep);
  410. var RemoveMarkStep = function (_Step2) {
  411. _inherits(RemoveMarkStep, _Step2);
  412. var _super2 = _createSuper(RemoveMarkStep);
  413. function RemoveMarkStep(from, to, mark) {
  414. var _this3;
  415. _classCallCheck(this, RemoveMarkStep);
  416. _this3 = _super2.call(this);
  417. _this3.from = from;
  418. _this3.to = to;
  419. _this3.mark = mark;
  420. return _this3;
  421. }
  422. _createClass(RemoveMarkStep, [{
  423. key: "apply",
  424. value: function apply(doc) {
  425. var _this4 = this;
  426. var oldSlice = doc.slice(this.from, this.to);
  427. var slice = new prosemirrorModel.Slice(mapFragment(oldSlice.content, function (node) {
  428. return node.mark(_this4.mark.removeFromSet(node.marks));
  429. }, doc), oldSlice.openStart, oldSlice.openEnd);
  430. return StepResult.fromReplace(doc, this.from, this.to, slice);
  431. }
  432. }, {
  433. key: "invert",
  434. value: function invert() {
  435. return new AddMarkStep(this.from, this.to, this.mark);
  436. }
  437. }, {
  438. key: "map",
  439. value: function map(mapping) {
  440. var from = mapping.mapResult(this.from, 1),
  441. to = mapping.mapResult(this.to, -1);
  442. if (from.deleted && to.deleted || from.pos >= to.pos) return null;
  443. return new RemoveMarkStep(from.pos, to.pos, this.mark);
  444. }
  445. }, {
  446. key: "merge",
  447. value: function merge(other) {
  448. if (other instanceof RemoveMarkStep && other.mark.eq(this.mark) && this.from <= other.to && this.to >= other.from) return new RemoveMarkStep(Math.min(this.from, other.from), Math.max(this.to, other.to), this.mark);
  449. return null;
  450. }
  451. }, {
  452. key: "toJSON",
  453. value: function toJSON() {
  454. return {
  455. stepType: "removeMark",
  456. mark: this.mark.toJSON(),
  457. from: this.from,
  458. to: this.to
  459. };
  460. }
  461. }], [{
  462. key: "fromJSON",
  463. value: function fromJSON(schema, json) {
  464. if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for RemoveMarkStep.fromJSON");
  465. return new RemoveMarkStep(json.from, json.to, schema.markFromJSON(json.mark));
  466. }
  467. }]);
  468. return RemoveMarkStep;
  469. }(Step);
  470. Step.jsonID("removeMark", RemoveMarkStep);
  471. var AddNodeMarkStep = function (_Step3) {
  472. _inherits(AddNodeMarkStep, _Step3);
  473. var _super3 = _createSuper(AddNodeMarkStep);
  474. function AddNodeMarkStep(pos, mark) {
  475. var _this5;
  476. _classCallCheck(this, AddNodeMarkStep);
  477. _this5 = _super3.call(this);
  478. _this5.pos = pos;
  479. _this5.mark = mark;
  480. return _this5;
  481. }
  482. _createClass(AddNodeMarkStep, [{
  483. key: "apply",
  484. value: function apply(doc) {
  485. var node = doc.nodeAt(this.pos);
  486. if (!node) return StepResult.fail("No node at mark step's position");
  487. var updated = node.type.create(node.attrs, null, this.mark.addToSet(node.marks));
  488. return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
  489. }
  490. }, {
  491. key: "invert",
  492. value: function invert(doc) {
  493. var node = doc.nodeAt(this.pos);
  494. if (node) {
  495. var newSet = this.mark.addToSet(node.marks);
  496. if (newSet.length == node.marks.length) {
  497. for (var i = 0; i < node.marks.length; i++) {
  498. if (!node.marks[i].isInSet(newSet)) return new AddNodeMarkStep(this.pos, node.marks[i]);
  499. }
  500. return new AddNodeMarkStep(this.pos, this.mark);
  501. }
  502. }
  503. return new RemoveNodeMarkStep(this.pos, this.mark);
  504. }
  505. }, {
  506. key: "map",
  507. value: function map(mapping) {
  508. var pos = mapping.mapResult(this.pos, 1);
  509. return pos.deletedAfter ? null : new AddNodeMarkStep(pos.pos, this.mark);
  510. }
  511. }, {
  512. key: "toJSON",
  513. value: function toJSON() {
  514. return {
  515. stepType: "addNodeMark",
  516. pos: this.pos,
  517. mark: this.mark.toJSON()
  518. };
  519. }
  520. }], [{
  521. key: "fromJSON",
  522. value: function fromJSON(schema, json) {
  523. if (typeof json.pos != "number") throw new RangeError("Invalid input for AddNodeMarkStep.fromJSON");
  524. return new AddNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
  525. }
  526. }]);
  527. return AddNodeMarkStep;
  528. }(Step);
  529. Step.jsonID("addNodeMark", AddNodeMarkStep);
  530. var RemoveNodeMarkStep = function (_Step4) {
  531. _inherits(RemoveNodeMarkStep, _Step4);
  532. var _super4 = _createSuper(RemoveNodeMarkStep);
  533. function RemoveNodeMarkStep(pos, mark) {
  534. var _this6;
  535. _classCallCheck(this, RemoveNodeMarkStep);
  536. _this6 = _super4.call(this);
  537. _this6.pos = pos;
  538. _this6.mark = mark;
  539. return _this6;
  540. }
  541. _createClass(RemoveNodeMarkStep, [{
  542. key: "apply",
  543. value: function apply(doc) {
  544. var node = doc.nodeAt(this.pos);
  545. if (!node) return StepResult.fail("No node at mark step's position");
  546. var updated = node.type.create(node.attrs, null, this.mark.removeFromSet(node.marks));
  547. return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
  548. }
  549. }, {
  550. key: "invert",
  551. value: function invert(doc) {
  552. var node = doc.nodeAt(this.pos);
  553. if (!node || !this.mark.isInSet(node.marks)) return this;
  554. return new AddNodeMarkStep(this.pos, this.mark);
  555. }
  556. }, {
  557. key: "map",
  558. value: function map(mapping) {
  559. var pos = mapping.mapResult(this.pos, 1);
  560. return pos.deletedAfter ? null : new RemoveNodeMarkStep(pos.pos, this.mark);
  561. }
  562. }, {
  563. key: "toJSON",
  564. value: function toJSON() {
  565. return {
  566. stepType: "removeNodeMark",
  567. pos: this.pos,
  568. mark: this.mark.toJSON()
  569. };
  570. }
  571. }], [{
  572. key: "fromJSON",
  573. value: function fromJSON(schema, json) {
  574. if (typeof json.pos != "number") throw new RangeError("Invalid input for RemoveNodeMarkStep.fromJSON");
  575. return new RemoveNodeMarkStep(json.pos, schema.markFromJSON(json.mark));
  576. }
  577. }]);
  578. return RemoveNodeMarkStep;
  579. }(Step);
  580. Step.jsonID("removeNodeMark", RemoveNodeMarkStep);
  581. var ReplaceStep = function (_Step5) {
  582. _inherits(ReplaceStep, _Step5);
  583. var _super5 = _createSuper(ReplaceStep);
  584. function ReplaceStep(from, to, slice) {
  585. var _this7;
  586. var structure = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  587. _classCallCheck(this, ReplaceStep);
  588. _this7 = _super5.call(this);
  589. _this7.from = from;
  590. _this7.to = to;
  591. _this7.slice = slice;
  592. _this7.structure = structure;
  593. return _this7;
  594. }
  595. _createClass(ReplaceStep, [{
  596. key: "apply",
  597. value: function apply(doc) {
  598. if (this.structure && contentBetween(doc, this.from, this.to)) return StepResult.fail("Structure replace would overwrite content");
  599. return StepResult.fromReplace(doc, this.from, this.to, this.slice);
  600. }
  601. }, {
  602. key: "getMap",
  603. value: function getMap() {
  604. return new StepMap([this.from, this.to - this.from, this.slice.size]);
  605. }
  606. }, {
  607. key: "invert",
  608. value: function invert(doc) {
  609. return new ReplaceStep(this.from, this.from + this.slice.size, doc.slice(this.from, this.to));
  610. }
  611. }, {
  612. key: "map",
  613. value: function map(mapping) {
  614. var from = mapping.mapResult(this.from, 1),
  615. to = mapping.mapResult(this.to, -1);
  616. if (from.deletedAcross && to.deletedAcross) return null;
  617. return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
  618. }
  619. }, {
  620. key: "merge",
  621. value: function merge(other) {
  622. if (!(other instanceof ReplaceStep) || other.structure || this.structure) return null;
  623. if (this.from + this.slice.size == other.from && !this.slice.openEnd && !other.slice.openStart) {
  624. var slice = this.slice.size + other.slice.size == 0 ? prosemirrorModel.Slice.empty : new prosemirrorModel.Slice(this.slice.content.append(other.slice.content), this.slice.openStart, other.slice.openEnd);
  625. return new ReplaceStep(this.from, this.to + (other.to - other.from), slice, this.structure);
  626. } else if (other.to == this.from && !this.slice.openStart && !other.slice.openEnd) {
  627. var _slice = this.slice.size + other.slice.size == 0 ? prosemirrorModel.Slice.empty : new prosemirrorModel.Slice(other.slice.content.append(this.slice.content), other.slice.openStart, this.slice.openEnd);
  628. return new ReplaceStep(other.from, this.to, _slice, this.structure);
  629. } else {
  630. return null;
  631. }
  632. }
  633. }, {
  634. key: "toJSON",
  635. value: function toJSON() {
  636. var json = {
  637. stepType: "replace",
  638. from: this.from,
  639. to: this.to
  640. };
  641. if (this.slice.size) json.slice = this.slice.toJSON();
  642. if (this.structure) json.structure = true;
  643. return json;
  644. }
  645. }], [{
  646. key: "fromJSON",
  647. value: function fromJSON(schema, json) {
  648. if (typeof json.from != "number" || typeof json.to != "number") throw new RangeError("Invalid input for ReplaceStep.fromJSON");
  649. return new ReplaceStep(json.from, json.to, prosemirrorModel.Slice.fromJSON(schema, json.slice), !!json.structure);
  650. }
  651. }]);
  652. return ReplaceStep;
  653. }(Step);
  654. Step.jsonID("replace", ReplaceStep);
  655. var ReplaceAroundStep = function (_Step6) {
  656. _inherits(ReplaceAroundStep, _Step6);
  657. var _super6 = _createSuper(ReplaceAroundStep);
  658. function ReplaceAroundStep(from, to, gapFrom, gapTo, slice, insert) {
  659. var _this8;
  660. var structure = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
  661. _classCallCheck(this, ReplaceAroundStep);
  662. _this8 = _super6.call(this);
  663. _this8.from = from;
  664. _this8.to = to;
  665. _this8.gapFrom = gapFrom;
  666. _this8.gapTo = gapTo;
  667. _this8.slice = slice;
  668. _this8.insert = insert;
  669. _this8.structure = structure;
  670. return _this8;
  671. }
  672. _createClass(ReplaceAroundStep, [{
  673. key: "apply",
  674. value: function apply(doc) {
  675. if (this.structure && (contentBetween(doc, this.from, this.gapFrom) || contentBetween(doc, this.gapTo, this.to))) return StepResult.fail("Structure gap-replace would overwrite content");
  676. var gap = doc.slice(this.gapFrom, this.gapTo);
  677. if (gap.openStart || gap.openEnd) return StepResult.fail("Gap is not a flat range");
  678. var inserted = this.slice.insertAt(this.insert, gap.content);
  679. if (!inserted) return StepResult.fail("Content does not fit in gap");
  680. return StepResult.fromReplace(doc, this.from, this.to, inserted);
  681. }
  682. }, {
  683. key: "getMap",
  684. value: function getMap() {
  685. return new StepMap([this.from, this.gapFrom - this.from, this.insert, this.gapTo, this.to - this.gapTo, this.slice.size - this.insert]);
  686. }
  687. }, {
  688. key: "invert",
  689. value: function invert(doc) {
  690. var gap = this.gapTo - this.gapFrom;
  691. return new ReplaceAroundStep(this.from, this.from + this.slice.size + gap, this.from + this.insert, this.from + this.insert + gap, doc.slice(this.from, this.to).removeBetween(this.gapFrom - this.from, this.gapTo - this.from), this.gapFrom - this.from, this.structure);
  692. }
  693. }, {
  694. key: "map",
  695. value: function map(mapping) {
  696. var from = mapping.mapResult(this.from, 1),
  697. to = mapping.mapResult(this.to, -1);
  698. var gapFrom = mapping.map(this.gapFrom, -1),
  699. gapTo = mapping.map(this.gapTo, 1);
  700. if (from.deletedAcross && to.deletedAcross || gapFrom < from.pos || gapTo > to.pos) return null;
  701. return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
  702. }
  703. }, {
  704. key: "toJSON",
  705. value: function toJSON() {
  706. var json = {
  707. stepType: "replaceAround",
  708. from: this.from,
  709. to: this.to,
  710. gapFrom: this.gapFrom,
  711. gapTo: this.gapTo,
  712. insert: this.insert
  713. };
  714. if (this.slice.size) json.slice = this.slice.toJSON();
  715. if (this.structure) json.structure = true;
  716. return json;
  717. }
  718. }], [{
  719. key: "fromJSON",
  720. value: function fromJSON(schema, json) {
  721. if (typeof json.from != "number" || typeof json.to != "number" || typeof json.gapFrom != "number" || typeof json.gapTo != "number" || typeof json.insert != "number") throw new RangeError("Invalid input for ReplaceAroundStep.fromJSON");
  722. return new ReplaceAroundStep(json.from, json.to, json.gapFrom, json.gapTo, prosemirrorModel.Slice.fromJSON(schema, json.slice), json.insert, !!json.structure);
  723. }
  724. }]);
  725. return ReplaceAroundStep;
  726. }(Step);
  727. Step.jsonID("replaceAround", ReplaceAroundStep);
  728. function contentBetween(doc, from, to) {
  729. var $from = doc.resolve(from),
  730. dist = to - from,
  731. depth = $from.depth;
  732. while (dist > 0 && depth > 0 && $from.indexAfter(depth) == $from.node(depth).childCount) {
  733. depth--;
  734. dist--;
  735. }
  736. if (dist > 0) {
  737. var next = $from.node(depth).maybeChild($from.indexAfter(depth));
  738. while (dist > 0) {
  739. if (!next || next.isLeaf) return true;
  740. next = next.firstChild;
  741. dist--;
  742. }
  743. }
  744. return false;
  745. }
  746. function _addMark(tr, from, to, mark) {
  747. var removed = [],
  748. added = [];
  749. var removing, adding;
  750. tr.doc.nodesBetween(from, to, function (node, pos, parent) {
  751. if (!node.isInline) return;
  752. var marks = node.marks;
  753. if (!mark.isInSet(marks) && parent.type.allowsMarkType(mark.type)) {
  754. var start = Math.max(pos, from),
  755. end = Math.min(pos + node.nodeSize, to);
  756. var newSet = mark.addToSet(marks);
  757. for (var i = 0; i < marks.length; i++) {
  758. if (!marks[i].isInSet(newSet)) {
  759. if (removing && removing.to == start && removing.mark.eq(marks[i])) removing.to = end;else removed.push(removing = new RemoveMarkStep(start, end, marks[i]));
  760. }
  761. }
  762. if (adding && adding.to == start) adding.to = end;else added.push(adding = new AddMarkStep(start, end, mark));
  763. }
  764. });
  765. removed.forEach(function (s) {
  766. return tr.step(s);
  767. });
  768. added.forEach(function (s) {
  769. return tr.step(s);
  770. });
  771. }
  772. function _removeMark(tr, from, to, mark) {
  773. var matched = [],
  774. step = 0;
  775. tr.doc.nodesBetween(from, to, function (node, pos) {
  776. if (!node.isInline) return;
  777. step++;
  778. var toRemove = null;
  779. if (mark instanceof prosemirrorModel.MarkType) {
  780. var set = node.marks,
  781. found;
  782. while (found = mark.isInSet(set)) {
  783. (toRemove || (toRemove = [])).push(found);
  784. set = found.removeFromSet(set);
  785. }
  786. } else if (mark) {
  787. if (mark.isInSet(node.marks)) toRemove = [mark];
  788. } else {
  789. toRemove = node.marks;
  790. }
  791. if (toRemove && toRemove.length) {
  792. var end = Math.min(pos + node.nodeSize, to);
  793. for (var i = 0; i < toRemove.length; i++) {
  794. var style = toRemove[i],
  795. _found = void 0;
  796. for (var j = 0; j < matched.length; j++) {
  797. var m = matched[j];
  798. if (m.step == step - 1 && style.eq(matched[j].style)) _found = m;
  799. }
  800. if (_found) {
  801. _found.to = end;
  802. _found.step = step;
  803. } else {
  804. matched.push({
  805. style: style,
  806. from: Math.max(pos, from),
  807. to: end,
  808. step: step
  809. });
  810. }
  811. }
  812. }
  813. });
  814. matched.forEach(function (m) {
  815. return tr.step(new RemoveMarkStep(m.from, m.to, m.style));
  816. });
  817. }
  818. function _clearIncompatible(tr, pos, parentType) {
  819. var match = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : parentType.contentMatch;
  820. var node = tr.doc.nodeAt(pos);
  821. var replSteps = [],
  822. cur = pos + 1;
  823. for (var i = 0; i < node.childCount; i++) {
  824. var child = node.child(i),
  825. end = cur + child.nodeSize;
  826. var allowed = match.matchType(child.type);
  827. if (!allowed) {
  828. replSteps.push(new ReplaceStep(cur, end, prosemirrorModel.Slice.empty));
  829. } else {
  830. match = allowed;
  831. for (var j = 0; j < child.marks.length; j++) {
  832. if (!parentType.allowsMarkType(child.marks[j].type)) tr.step(new RemoveMarkStep(cur, end, child.marks[j]));
  833. }
  834. if (child.isText && !parentType.spec.code) {
  835. var m = void 0,
  836. newline = /\r?\n|\r/g,
  837. slice = void 0;
  838. while (m = newline.exec(child.text)) {
  839. if (!slice) slice = new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(parentType.schema.text(" ", parentType.allowedMarks(child.marks))), 0, 0);
  840. replSteps.push(new ReplaceStep(cur + m.index, cur + m.index + m[0].length, slice));
  841. }
  842. }
  843. }
  844. cur = end;
  845. }
  846. if (!match.validEnd) {
  847. var fill = match.fillBefore(prosemirrorModel.Fragment.empty, true);
  848. tr.replace(cur, cur, new prosemirrorModel.Slice(fill, 0, 0));
  849. }
  850. for (var _i = replSteps.length - 1; _i >= 0; _i--) {
  851. tr.step(replSteps[_i]);
  852. }
  853. }
  854. function canCut(node, start, end) {
  855. return (start == 0 || node.canReplace(start, node.childCount)) && (end == node.childCount || node.canReplace(0, end));
  856. }
  857. function liftTarget(range) {
  858. var parent = range.parent;
  859. var content = parent.content.cutByIndex(range.startIndex, range.endIndex);
  860. for (var depth = range.depth;; --depth) {
  861. var node = range.$from.node(depth);
  862. var index = range.$from.index(depth),
  863. endIndex = range.$to.indexAfter(depth);
  864. if (depth < range.depth && node.canReplace(index, endIndex, content)) return depth;
  865. if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex)) break;
  866. }
  867. return null;
  868. }
  869. function _lift(tr, range, target) {
  870. var $from = range.$from,
  871. $to = range.$to,
  872. depth = range.depth;
  873. var gapStart = $from.before(depth + 1),
  874. gapEnd = $to.after(depth + 1);
  875. var start = gapStart,
  876. end = gapEnd;
  877. var before = prosemirrorModel.Fragment.empty,
  878. openStart = 0;
  879. for (var d = depth, splitting = false; d > target; d--) {
  880. if (splitting || $from.index(d) > 0) {
  881. splitting = true;
  882. before = prosemirrorModel.Fragment.from($from.node(d).copy(before));
  883. openStart++;
  884. } else {
  885. start--;
  886. }
  887. }
  888. var after = prosemirrorModel.Fragment.empty,
  889. openEnd = 0;
  890. for (var _d = depth, _splitting = false; _d > target; _d--) {
  891. if (_splitting || $to.after(_d + 1) < $to.end(_d)) {
  892. _splitting = true;
  893. after = prosemirrorModel.Fragment.from($to.node(_d).copy(after));
  894. openEnd++;
  895. } else {
  896. end++;
  897. }
  898. }
  899. tr.step(new ReplaceAroundStep(start, end, gapStart, gapEnd, new prosemirrorModel.Slice(before.append(after), openStart, openEnd), before.size - openStart, true));
  900. }
  901. function findWrapping(range, nodeType) {
  902. var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  903. var innerRange = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : range;
  904. var around = findWrappingOutside(range, nodeType);
  905. var inner = around && findWrappingInside(innerRange, nodeType);
  906. if (!inner) return null;
  907. return around.map(withAttrs).concat({
  908. type: nodeType,
  909. attrs: attrs
  910. }).concat(inner.map(withAttrs));
  911. }
  912. function withAttrs(type) {
  913. return {
  914. type: type,
  915. attrs: null
  916. };
  917. }
  918. function findWrappingOutside(range, type) {
  919. var parent = range.parent,
  920. startIndex = range.startIndex,
  921. endIndex = range.endIndex;
  922. var around = parent.contentMatchAt(startIndex).findWrapping(type);
  923. if (!around) return null;
  924. var outer = around.length ? around[0] : type;
  925. return parent.canReplaceWith(startIndex, endIndex, outer) ? around : null;
  926. }
  927. function findWrappingInside(range, type) {
  928. var parent = range.parent,
  929. startIndex = range.startIndex,
  930. endIndex = range.endIndex;
  931. var inner = parent.child(startIndex);
  932. var inside = type.contentMatch.findWrapping(inner.type);
  933. if (!inside) return null;
  934. var lastType = inside.length ? inside[inside.length - 1] : type;
  935. var innerMatch = lastType.contentMatch;
  936. for (var i = startIndex; innerMatch && i < endIndex; i++) {
  937. innerMatch = innerMatch.matchType(parent.child(i).type);
  938. }
  939. if (!innerMatch || !innerMatch.validEnd) return null;
  940. return inside;
  941. }
  942. function _wrap2(tr, range, wrappers) {
  943. var content = prosemirrorModel.Fragment.empty;
  944. for (var i = wrappers.length - 1; i >= 0; i--) {
  945. if (content.size) {
  946. var match = wrappers[i].type.contentMatch.matchFragment(content);
  947. if (!match || !match.validEnd) throw new RangeError("Wrapper type given to Transform.wrap does not form valid content of its parent wrapper");
  948. }
  949. content = prosemirrorModel.Fragment.from(wrappers[i].type.create(wrappers[i].attrs, content));
  950. }
  951. var start = range.start,
  952. end = range.end;
  953. tr.step(new ReplaceAroundStep(start, end, start, end, new prosemirrorModel.Slice(content, 0, 0), wrappers.length, true));
  954. }
  955. function _setBlockType(tr, from, to, type, attrs) {
  956. if (!type.isTextblock) throw new RangeError("Type given to setBlockType should be a textblock");
  957. var mapFrom = tr.steps.length;
  958. tr.doc.nodesBetween(from, to, function (node, pos) {
  959. if (node.isTextblock && !node.hasMarkup(type, attrs) && canChangeType(tr.doc, tr.mapping.slice(mapFrom).map(pos), type)) {
  960. tr.clearIncompatible(tr.mapping.slice(mapFrom).map(pos, 1), type);
  961. var mapping = tr.mapping.slice(mapFrom);
  962. var startM = mapping.map(pos, 1),
  963. endM = mapping.map(pos + node.nodeSize, 1);
  964. tr.step(new ReplaceAroundStep(startM, endM, startM + 1, endM - 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(type.create(attrs, null, node.marks)), 0, 0), 1, true));
  965. return false;
  966. }
  967. });
  968. }
  969. function canChangeType(doc, pos, type) {
  970. var $pos = doc.resolve(pos),
  971. index = $pos.index();
  972. return $pos.parent.canReplaceWith(index, index + 1, type);
  973. }
  974. function _setNodeMarkup(tr, pos, type, attrs, marks) {
  975. var node = tr.doc.nodeAt(pos);
  976. if (!node) throw new RangeError("No node at given position");
  977. if (!type) type = node.type;
  978. var newNode = type.create(attrs, null, marks || node.marks);
  979. if (node.isLeaf) return tr.replaceWith(pos, pos + node.nodeSize, newNode);
  980. if (!type.validContent(node.content)) throw new RangeError("Invalid content for node type " + type.name);
  981. tr.step(new ReplaceAroundStep(pos, pos + node.nodeSize, pos + 1, pos + node.nodeSize - 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(newNode), 0, 0), 1, true));
  982. }
  983. function canSplit(doc, pos) {
  984. var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  985. var typesAfter = arguments.length > 3 ? arguments[3] : undefined;
  986. var $pos = doc.resolve(pos),
  987. base = $pos.depth - depth;
  988. var innerType = typesAfter && typesAfter[typesAfter.length - 1] || $pos.parent;
  989. if (base < 0 || $pos.parent.type.spec.isolating || !$pos.parent.canReplace($pos.index(), $pos.parent.childCount) || !innerType.type.validContent($pos.parent.content.cutByIndex($pos.index(), $pos.parent.childCount))) return false;
  990. for (var d = $pos.depth - 1, i = depth - 2; d > base; d--, i--) {
  991. var node = $pos.node(d),
  992. _index = $pos.index(d);
  993. if (node.type.spec.isolating) return false;
  994. var rest = node.content.cutByIndex(_index, node.childCount);
  995. var overrideChild = typesAfter && typesAfter[i + 1];
  996. if (overrideChild) rest = rest.replaceChild(0, overrideChild.type.create(overrideChild.attrs));
  997. var after = typesAfter && typesAfter[i] || node;
  998. if (!node.canReplace(_index + 1, node.childCount) || !after.type.validContent(rest)) return false;
  999. }
  1000. var index = $pos.indexAfter(base);
  1001. var baseType = typesAfter && typesAfter[0];
  1002. return $pos.node(base).canReplaceWith(index, index, baseType ? baseType.type : $pos.node(base + 1).type);
  1003. }
  1004. function _split(tr, pos) {
  1005. var depth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1006. var typesAfter = arguments.length > 3 ? arguments[3] : undefined;
  1007. var $pos = tr.doc.resolve(pos),
  1008. before = prosemirrorModel.Fragment.empty,
  1009. after = prosemirrorModel.Fragment.empty;
  1010. for (var d = $pos.depth, e = $pos.depth - depth, i = depth - 1; d > e; d--, i--) {
  1011. before = prosemirrorModel.Fragment.from($pos.node(d).copy(before));
  1012. var typeAfter = typesAfter && typesAfter[i];
  1013. after = prosemirrorModel.Fragment.from(typeAfter ? typeAfter.type.create(typeAfter.attrs, after) : $pos.node(d).copy(after));
  1014. }
  1015. tr.step(new ReplaceStep(pos, pos, new prosemirrorModel.Slice(before.append(after), depth, depth), true));
  1016. }
  1017. function canJoin(doc, pos) {
  1018. var $pos = doc.resolve(pos),
  1019. index = $pos.index();
  1020. return joinable($pos.nodeBefore, $pos.nodeAfter) && $pos.parent.canReplace(index, index + 1);
  1021. }
  1022. function joinable(a, b) {
  1023. return !!(a && b && !a.isLeaf && a.canAppend(b));
  1024. }
  1025. function joinPoint(doc, pos) {
  1026. var dir = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
  1027. var $pos = doc.resolve(pos);
  1028. for (var d = $pos.depth;; d--) {
  1029. var before = void 0,
  1030. after = void 0,
  1031. index = $pos.index(d);
  1032. if (d == $pos.depth) {
  1033. before = $pos.nodeBefore;
  1034. after = $pos.nodeAfter;
  1035. } else if (dir > 0) {
  1036. before = $pos.node(d + 1);
  1037. index++;
  1038. after = $pos.node(d).maybeChild(index);
  1039. } else {
  1040. before = $pos.node(d).maybeChild(index - 1);
  1041. after = $pos.node(d + 1);
  1042. }
  1043. if (before && !before.isTextblock && joinable(before, after) && $pos.node(d).canReplace(index, index + 1)) return pos;
  1044. if (d == 0) break;
  1045. pos = dir < 0 ? $pos.before(d) : $pos.after(d);
  1046. }
  1047. }
  1048. function _join(tr, pos, depth) {
  1049. var step = new ReplaceStep(pos - depth, pos + depth, prosemirrorModel.Slice.empty, true);
  1050. tr.step(step);
  1051. }
  1052. function insertPoint(doc, pos, nodeType) {
  1053. var $pos = doc.resolve(pos);
  1054. if ($pos.parent.canReplaceWith($pos.index(), $pos.index(), nodeType)) return pos;
  1055. if ($pos.parentOffset == 0) for (var d = $pos.depth - 1; d >= 0; d--) {
  1056. var index = $pos.index(d);
  1057. if ($pos.node(d).canReplaceWith(index, index, nodeType)) return $pos.before(d + 1);
  1058. if (index > 0) return null;
  1059. }
  1060. if ($pos.parentOffset == $pos.parent.content.size) for (var _d2 = $pos.depth - 1; _d2 >= 0; _d2--) {
  1061. var _index2 = $pos.indexAfter(_d2);
  1062. if ($pos.node(_d2).canReplaceWith(_index2, _index2, nodeType)) return $pos.after(_d2 + 1);
  1063. if (_index2 < $pos.node(_d2).childCount) return null;
  1064. }
  1065. return null;
  1066. }
  1067. function dropPoint(doc, pos, slice) {
  1068. var $pos = doc.resolve(pos);
  1069. if (!slice.content.size) return pos;
  1070. var content = slice.content;
  1071. for (var i = 0; i < slice.openStart; i++) {
  1072. content = content.firstChild.content;
  1073. }
  1074. for (var pass = 1; pass <= (slice.openStart == 0 && slice.size ? 2 : 1); pass++) {
  1075. for (var d = $pos.depth; d >= 0; d--) {
  1076. var bias = d == $pos.depth ? 0 : $pos.pos <= ($pos.start(d + 1) + $pos.end(d + 1)) / 2 ? -1 : 1;
  1077. var insertPos = $pos.index(d) + (bias > 0 ? 1 : 0);
  1078. var parent = $pos.node(d),
  1079. fits = false;
  1080. if (pass == 1) {
  1081. fits = parent.canReplace(insertPos, insertPos, content);
  1082. } else {
  1083. var wrapping = parent.contentMatchAt(insertPos).findWrapping(content.firstChild.type);
  1084. fits = wrapping && parent.canReplaceWith(insertPos, insertPos, wrapping[0]);
  1085. }
  1086. if (fits) return bias == 0 ? $pos.pos : bias < 0 ? $pos.before(d + 1) : $pos.after(d + 1);
  1087. }
  1088. }
  1089. return null;
  1090. }
  1091. function replaceStep(doc, from) {
  1092. var to = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : from;
  1093. var slice = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : prosemirrorModel.Slice.empty;
  1094. if (from == to && !slice.size) return null;
  1095. var $from = doc.resolve(from),
  1096. $to = doc.resolve(to);
  1097. if (fitsTrivially($from, $to, slice)) return new ReplaceStep(from, to, slice);
  1098. return new Fitter($from, $to, slice).fit();
  1099. }
  1100. function fitsTrivially($from, $to, slice) {
  1101. return !slice.openStart && !slice.openEnd && $from.start() == $to.start() && $from.parent.canReplace($from.index(), $to.index(), slice.content);
  1102. }
  1103. var Fitter = function () {
  1104. function Fitter($from, $to, unplaced) {
  1105. _classCallCheck(this, Fitter);
  1106. this.$from = $from;
  1107. this.$to = $to;
  1108. this.unplaced = unplaced;
  1109. this.frontier = [];
  1110. this.placed = prosemirrorModel.Fragment.empty;
  1111. for (var i = 0; i <= $from.depth; i++) {
  1112. var node = $from.node(i);
  1113. this.frontier.push({
  1114. type: node.type,
  1115. match: node.contentMatchAt($from.indexAfter(i))
  1116. });
  1117. }
  1118. for (var _i2 = $from.depth; _i2 > 0; _i2--) {
  1119. this.placed = prosemirrorModel.Fragment.from($from.node(_i2).copy(this.placed));
  1120. }
  1121. }
  1122. _createClass(Fitter, [{
  1123. key: "depth",
  1124. get: function get() {
  1125. return this.frontier.length - 1;
  1126. }
  1127. }, {
  1128. key: "fit",
  1129. value: function fit() {
  1130. while (this.unplaced.size) {
  1131. var fit = this.findFittable();
  1132. if (fit) this.placeNodes(fit);else this.openMore() || this.dropNode();
  1133. }
  1134. var moveInline = this.mustMoveInline(),
  1135. placedSize = this.placed.size - this.depth - this.$from.depth;
  1136. var $from = this.$from,
  1137. $to = this.close(moveInline < 0 ? this.$to : $from.doc.resolve(moveInline));
  1138. if (!$to) return null;
  1139. var content = this.placed,
  1140. openStart = $from.depth,
  1141. openEnd = $to.depth;
  1142. while (openStart && openEnd && content.childCount == 1) {
  1143. content = content.firstChild.content;
  1144. openStart--;
  1145. openEnd--;
  1146. }
  1147. var slice = new prosemirrorModel.Slice(content, openStart, openEnd);
  1148. if (moveInline > -1) return new ReplaceAroundStep($from.pos, moveInline, this.$to.pos, this.$to.end(), slice, placedSize);
  1149. if (slice.size || $from.pos != this.$to.pos) return new ReplaceStep($from.pos, $to.pos, slice);
  1150. return null;
  1151. }
  1152. }, {
  1153. key: "findFittable",
  1154. value: function findFittable() {
  1155. var startDepth = this.unplaced.openStart;
  1156. for (var cur = this.unplaced.content, d = 0, openEnd = this.unplaced.openEnd; d < startDepth; d++) {
  1157. var node = cur.firstChild;
  1158. if (cur.childCount > 1) openEnd = 0;
  1159. if (node.type.spec.isolating && openEnd <= d) {
  1160. startDepth = d;
  1161. break;
  1162. }
  1163. cur = node.content;
  1164. }
  1165. for (var pass = 1; pass <= 2; pass++) {
  1166. for (var sliceDepth = pass == 1 ? startDepth : this.unplaced.openStart; sliceDepth >= 0; sliceDepth--) {
  1167. var fragment = void 0,
  1168. parent = null;
  1169. if (sliceDepth) {
  1170. parent = contentAt(this.unplaced.content, sliceDepth - 1).firstChild;
  1171. fragment = parent.content;
  1172. } else {
  1173. fragment = this.unplaced.content;
  1174. }
  1175. var first = fragment.firstChild;
  1176. for (var frontierDepth = this.depth; frontierDepth >= 0; frontierDepth--) {
  1177. var _this$frontier$fronti = this.frontier[frontierDepth],
  1178. type = _this$frontier$fronti.type,
  1179. match = _this$frontier$fronti.match,
  1180. _wrap = void 0,
  1181. inject = null;
  1182. if (pass == 1 && (first ? match.matchType(first.type) || (inject = match.fillBefore(prosemirrorModel.Fragment.from(first), false)) : parent && type.compatibleContent(parent.type))) return {
  1183. sliceDepth: sliceDepth,
  1184. frontierDepth: frontierDepth,
  1185. parent: parent,
  1186. inject: inject
  1187. };else if (pass == 2 && first && (_wrap = match.findWrapping(first.type))) return {
  1188. sliceDepth: sliceDepth,
  1189. frontierDepth: frontierDepth,
  1190. parent: parent,
  1191. wrap: _wrap
  1192. };
  1193. if (parent && match.matchType(parent.type)) break;
  1194. }
  1195. }
  1196. }
  1197. }
  1198. }, {
  1199. key: "openMore",
  1200. value: function openMore() {
  1201. var _this$unplaced = this.unplaced,
  1202. content = _this$unplaced.content,
  1203. openStart = _this$unplaced.openStart,
  1204. openEnd = _this$unplaced.openEnd;
  1205. var inner = contentAt(content, openStart);
  1206. if (!inner.childCount || inner.firstChild.isLeaf) return false;
  1207. this.unplaced = new prosemirrorModel.Slice(content, openStart + 1, Math.max(openEnd, inner.size + openStart >= content.size - openEnd ? openStart + 1 : 0));
  1208. return true;
  1209. }
  1210. }, {
  1211. key: "dropNode",
  1212. value: function dropNode() {
  1213. var _this$unplaced2 = this.unplaced,
  1214. content = _this$unplaced2.content,
  1215. openStart = _this$unplaced2.openStart,
  1216. openEnd = _this$unplaced2.openEnd;
  1217. var inner = contentAt(content, openStart);
  1218. if (inner.childCount <= 1 && openStart > 0) {
  1219. var openAtEnd = content.size - openStart <= openStart + inner.size;
  1220. this.unplaced = new prosemirrorModel.Slice(dropFromFragment(content, openStart - 1, 1), openStart - 1, openAtEnd ? openStart - 1 : openEnd);
  1221. } else {
  1222. this.unplaced = new prosemirrorModel.Slice(dropFromFragment(content, openStart, 1), openStart, openEnd);
  1223. }
  1224. }
  1225. }, {
  1226. key: "placeNodes",
  1227. value: function placeNodes(_ref) {
  1228. var sliceDepth = _ref.sliceDepth,
  1229. frontierDepth = _ref.frontierDepth,
  1230. parent = _ref.parent,
  1231. inject = _ref.inject,
  1232. wrap = _ref.wrap;
  1233. while (this.depth > frontierDepth) {
  1234. this.closeFrontierNode();
  1235. }
  1236. if (wrap) for (var i = 0; i < wrap.length; i++) {
  1237. this.openFrontierNode(wrap[i]);
  1238. }
  1239. var slice = this.unplaced,
  1240. fragment = parent ? parent.content : slice.content;
  1241. var openStart = slice.openStart - sliceDepth;
  1242. var taken = 0,
  1243. add = [];
  1244. var _this$frontier$fronti2 = this.frontier[frontierDepth],
  1245. match = _this$frontier$fronti2.match,
  1246. type = _this$frontier$fronti2.type;
  1247. if (inject) {
  1248. for (var i = 0; i < inject.childCount; i++) {
  1249. add.push(inject.child(i));
  1250. }
  1251. match = match.matchFragment(inject);
  1252. }
  1253. var openEndCount = fragment.size + sliceDepth - (slice.content.size - slice.openEnd);
  1254. while (taken < fragment.childCount) {
  1255. var next = fragment.child(taken),
  1256. matches = match.matchType(next.type);
  1257. if (!matches) break;
  1258. taken++;
  1259. if (taken > 1 || openStart == 0 || next.content.size) {
  1260. match = matches;
  1261. add.push(closeNodeStart(next.mark(type.allowedMarks(next.marks)), taken == 1 ? openStart : 0, taken == fragment.childCount ? openEndCount : -1));
  1262. }
  1263. }
  1264. var toEnd = taken == fragment.childCount;
  1265. if (!toEnd) openEndCount = -1;
  1266. this.placed = addToFragment(this.placed, frontierDepth, prosemirrorModel.Fragment.from(add));
  1267. this.frontier[frontierDepth].match = match;
  1268. if (toEnd && openEndCount < 0 && parent && parent.type == this.frontier[this.depth].type && this.frontier.length > 1) this.closeFrontierNode();
  1269. for (var _i3 = 0, cur = fragment; _i3 < openEndCount; _i3++) {
  1270. var node = cur.lastChild;
  1271. this.frontier.push({
  1272. type: node.type,
  1273. match: node.contentMatchAt(node.childCount)
  1274. });
  1275. cur = node.content;
  1276. }
  1277. this.unplaced = !toEnd ? new prosemirrorModel.Slice(dropFromFragment(slice.content, sliceDepth, taken), slice.openStart, slice.openEnd) : sliceDepth == 0 ? prosemirrorModel.Slice.empty : new prosemirrorModel.Slice(dropFromFragment(slice.content, sliceDepth - 1, 1), sliceDepth - 1, openEndCount < 0 ? slice.openEnd : sliceDepth - 1);
  1278. }
  1279. }, {
  1280. key: "mustMoveInline",
  1281. value: function mustMoveInline() {
  1282. if (!this.$to.parent.isTextblock) return -1;
  1283. var top = this.frontier[this.depth],
  1284. level;
  1285. if (!top.type.isTextblock || !contentAfterFits(this.$to, this.$to.depth, top.type, top.match, false) || this.$to.depth == this.depth && (level = this.findCloseLevel(this.$to)) && level.depth == this.depth) return -1;
  1286. var depth = this.$to.depth,
  1287. after = this.$to.after(depth);
  1288. while (depth > 1 && after == this.$to.end(--depth)) {
  1289. ++after;
  1290. }
  1291. return after;
  1292. }
  1293. }, {
  1294. key: "findCloseLevel",
  1295. value: function findCloseLevel($to) {
  1296. scan: for (var i = Math.min(this.depth, $to.depth); i >= 0; i--) {
  1297. var _this$frontier$i = this.frontier[i],
  1298. match = _this$frontier$i.match,
  1299. type = _this$frontier$i.type;
  1300. var dropInner = i < $to.depth && $to.end(i + 1) == $to.pos + ($to.depth - (i + 1));
  1301. var fit = contentAfterFits($to, i, type, match, dropInner);
  1302. if (!fit) continue;
  1303. for (var d = i - 1; d >= 0; d--) {
  1304. var _this$frontier$d = this.frontier[d],
  1305. _match = _this$frontier$d.match,
  1306. _type = _this$frontier$d.type;
  1307. var matches = contentAfterFits($to, d, _type, _match, true);
  1308. if (!matches || matches.childCount) continue scan;
  1309. }
  1310. return {
  1311. depth: i,
  1312. fit: fit,
  1313. move: dropInner ? $to.doc.resolve($to.after(i + 1)) : $to
  1314. };
  1315. }
  1316. }
  1317. }, {
  1318. key: "close",
  1319. value: function close($to) {
  1320. var close = this.findCloseLevel($to);
  1321. if (!close) return null;
  1322. while (this.depth > close.depth) {
  1323. this.closeFrontierNode();
  1324. }
  1325. if (close.fit.childCount) this.placed = addToFragment(this.placed, close.depth, close.fit);
  1326. $to = close.move;
  1327. for (var d = close.depth + 1; d <= $to.depth; d++) {
  1328. var node = $to.node(d),
  1329. add = node.type.contentMatch.fillBefore(node.content, true, $to.index(d));
  1330. this.openFrontierNode(node.type, node.attrs, add);
  1331. }
  1332. return $to;
  1333. }
  1334. }, {
  1335. key: "openFrontierNode",
  1336. value: function openFrontierNode(type) {
  1337. var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  1338. var content = arguments.length > 2 ? arguments[2] : undefined;
  1339. var top = this.frontier[this.depth];
  1340. top.match = top.match.matchType(type);
  1341. this.placed = addToFragment(this.placed, this.depth, prosemirrorModel.Fragment.from(type.create(attrs, content)));
  1342. this.frontier.push({
  1343. type: type,
  1344. match: type.contentMatch
  1345. });
  1346. }
  1347. }, {
  1348. key: "closeFrontierNode",
  1349. value: function closeFrontierNode() {
  1350. var open = this.frontier.pop();
  1351. var add = open.match.fillBefore(prosemirrorModel.Fragment.empty, true);
  1352. if (add.childCount) this.placed = addToFragment(this.placed, this.frontier.length, add);
  1353. }
  1354. }]);
  1355. return Fitter;
  1356. }();
  1357. function dropFromFragment(fragment, depth, count) {
  1358. if (depth == 0) return fragment.cutByIndex(count, fragment.childCount);
  1359. return fragment.replaceChild(0, fragment.firstChild.copy(dropFromFragment(fragment.firstChild.content, depth - 1, count)));
  1360. }
  1361. function addToFragment(fragment, depth, content) {
  1362. if (depth == 0) return fragment.append(content);
  1363. return fragment.replaceChild(fragment.childCount - 1, fragment.lastChild.copy(addToFragment(fragment.lastChild.content, depth - 1, content)));
  1364. }
  1365. function contentAt(fragment, depth) {
  1366. for (var i = 0; i < depth; i++) {
  1367. fragment = fragment.firstChild.content;
  1368. }
  1369. return fragment;
  1370. }
  1371. function closeNodeStart(node, openStart, openEnd) {
  1372. if (openStart <= 0) return node;
  1373. var frag = node.content;
  1374. if (openStart > 1) frag = frag.replaceChild(0, closeNodeStart(frag.firstChild, openStart - 1, frag.childCount == 1 ? openEnd - 1 : 0));
  1375. if (openStart > 0) {
  1376. frag = node.type.contentMatch.fillBefore(frag).append(frag);
  1377. if (openEnd <= 0) frag = frag.append(node.type.contentMatch.matchFragment(frag).fillBefore(prosemirrorModel.Fragment.empty, true));
  1378. }
  1379. return node.copy(frag);
  1380. }
  1381. function contentAfterFits($to, depth, type, match, open) {
  1382. var node = $to.node(depth),
  1383. index = open ? $to.indexAfter(depth) : $to.index(depth);
  1384. if (index == node.childCount && !type.compatibleContent(node.type)) return null;
  1385. var fit = match.fillBefore(node.content, true, index);
  1386. return fit && !invalidMarks(type, node.content, index) ? fit : null;
  1387. }
  1388. function invalidMarks(type, fragment, start) {
  1389. for (var i = start; i < fragment.childCount; i++) {
  1390. if (!type.allowsMarks(fragment.child(i).marks)) return true;
  1391. }
  1392. return false;
  1393. }
  1394. function definesContent(type) {
  1395. return type.spec.defining || type.spec.definingForContent;
  1396. }
  1397. function _replaceRange(tr, from, to, slice) {
  1398. if (!slice.size) return tr.deleteRange(from, to);
  1399. var $from = tr.doc.resolve(from),
  1400. $to = tr.doc.resolve(to);
  1401. if (fitsTrivially($from, $to, slice)) return tr.step(new ReplaceStep(from, to, slice));
  1402. var targetDepths = coveredDepths($from, tr.doc.resolve(to));
  1403. if (targetDepths[targetDepths.length - 1] == 0) targetDepths.pop();
  1404. var preferredTarget = -($from.depth + 1);
  1405. targetDepths.unshift(preferredTarget);
  1406. for (var d = $from.depth, pos = $from.pos - 1; d > 0; d--, pos--) {
  1407. var spec = $from.node(d).type.spec;
  1408. if (spec.defining || spec.definingAsContext || spec.isolating) break;
  1409. if (targetDepths.indexOf(d) > -1) preferredTarget = d;else if ($from.before(d) == pos) targetDepths.splice(1, 0, -d);
  1410. }
  1411. var preferredTargetIndex = targetDepths.indexOf(preferredTarget);
  1412. var leftNodes = [],
  1413. preferredDepth = slice.openStart;
  1414. for (var content = slice.content, i = 0;; i++) {
  1415. var node = content.firstChild;
  1416. leftNodes.push(node);
  1417. if (i == slice.openStart) break;
  1418. content = node.content;
  1419. }
  1420. for (var _d3 = preferredDepth - 1; _d3 >= 0; _d3--) {
  1421. var leftNode = leftNodes[_d3],
  1422. def = definesContent(leftNode.type);
  1423. if (def && !leftNode.sameMarkup($from.node(Math.abs(preferredTarget) - 1))) preferredDepth = _d3;else if (def || !leftNode.type.isTextblock) break;
  1424. }
  1425. for (var j = slice.openStart; j >= 0; j--) {
  1426. var openDepth = (j + preferredDepth + 1) % (slice.openStart + 1);
  1427. var insert = leftNodes[openDepth];
  1428. if (!insert) continue;
  1429. for (var _i4 = 0; _i4 < targetDepths.length; _i4++) {
  1430. var targetDepth = targetDepths[(_i4 + preferredTargetIndex) % targetDepths.length],
  1431. expand = true;
  1432. if (targetDepth < 0) {
  1433. expand = false;
  1434. targetDepth = -targetDepth;
  1435. }
  1436. var parent = $from.node(targetDepth - 1),
  1437. index = $from.index(targetDepth - 1);
  1438. if (parent.canReplaceWith(index, index, insert.type, insert.marks)) return tr.replace($from.before(targetDepth), expand ? $to.after(targetDepth) : to, new prosemirrorModel.Slice(closeFragment(slice.content, 0, slice.openStart, openDepth), openDepth, slice.openEnd));
  1439. }
  1440. }
  1441. var startSteps = tr.steps.length;
  1442. for (var _i5 = targetDepths.length - 1; _i5 >= 0; _i5--) {
  1443. tr.replace(from, to, slice);
  1444. if (tr.steps.length > startSteps) break;
  1445. var depth = targetDepths[_i5];
  1446. if (depth < 0) continue;
  1447. from = $from.before(depth);
  1448. to = $to.after(depth);
  1449. }
  1450. }
  1451. function closeFragment(fragment, depth, oldOpen, newOpen, parent) {
  1452. if (depth < oldOpen) {
  1453. var first = fragment.firstChild;
  1454. fragment = fragment.replaceChild(0, first.copy(closeFragment(first.content, depth + 1, oldOpen, newOpen, first)));
  1455. }
  1456. if (depth > newOpen) {
  1457. var match = parent.contentMatchAt(0);
  1458. var start = match.fillBefore(fragment).append(fragment);
  1459. fragment = start.append(match.matchFragment(start).fillBefore(prosemirrorModel.Fragment.empty, true));
  1460. }
  1461. return fragment;
  1462. }
  1463. function _replaceRangeWith(tr, from, to, node) {
  1464. if (!node.isInline && from == to && tr.doc.resolve(from).parent.content.size) {
  1465. var point = insertPoint(tr.doc, from, node.type);
  1466. if (point != null) from = to = point;
  1467. }
  1468. tr.replaceRange(from, to, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(node), 0, 0));
  1469. }
  1470. function _deleteRange(tr, from, to) {
  1471. var $from = tr.doc.resolve(from),
  1472. $to = tr.doc.resolve(to);
  1473. var covered = coveredDepths($from, $to);
  1474. for (var i = 0; i < covered.length; i++) {
  1475. var depth = covered[i],
  1476. last = i == covered.length - 1;
  1477. if (last && depth == 0 || $from.node(depth).type.contentMatch.validEnd) return tr["delete"]($from.start(depth), $to.end(depth));
  1478. if (depth > 0 && (last || $from.node(depth - 1).canReplace($from.index(depth - 1), $to.indexAfter(depth - 1)))) return tr["delete"]($from.before(depth), $to.after(depth));
  1479. }
  1480. for (var d = 1; d <= $from.depth && d <= $to.depth; d++) {
  1481. if (from - $from.start(d) == $from.depth - d && to > $from.end(d) && $to.end(d) - to != $to.depth - d) return tr["delete"]($from.before(d), to);
  1482. }
  1483. tr["delete"](from, to);
  1484. }
  1485. function coveredDepths($from, $to) {
  1486. var result = [],
  1487. minDepth = Math.min($from.depth, $to.depth);
  1488. for (var d = minDepth; d >= 0; d--) {
  1489. var start = $from.start(d);
  1490. if (start < $from.pos - ($from.depth - d) || $to.end(d) > $to.pos + ($to.depth - d) || $from.node(d).type.spec.isolating || $to.node(d).type.spec.isolating) break;
  1491. if (start == $to.start(d) || d == $from.depth && d == $to.depth && $from.parent.inlineContent && $to.parent.inlineContent && d && $to.start(d - 1) == start - 1) result.push(d);
  1492. }
  1493. return result;
  1494. }
  1495. var AttrStep = function (_Step7) {
  1496. _inherits(AttrStep, _Step7);
  1497. var _super7 = _createSuper(AttrStep);
  1498. function AttrStep(pos, attr, value) {
  1499. var _this9;
  1500. _classCallCheck(this, AttrStep);
  1501. _this9 = _super7.call(this);
  1502. _this9.pos = pos;
  1503. _this9.attr = attr;
  1504. _this9.value = value;
  1505. return _this9;
  1506. }
  1507. _createClass(AttrStep, [{
  1508. key: "apply",
  1509. value: function apply(doc) {
  1510. var node = doc.nodeAt(this.pos);
  1511. if (!node) return StepResult.fail("No node at attribute step's position");
  1512. var attrs = Object.create(null);
  1513. for (var name in node.attrs) {
  1514. attrs[name] = node.attrs[name];
  1515. }
  1516. attrs[this.attr] = this.value;
  1517. var updated = node.type.create(attrs, null, node.marks);
  1518. return StepResult.fromReplace(doc, this.pos, this.pos + 1, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(updated), 0, node.isLeaf ? 0 : 1));
  1519. }
  1520. }, {
  1521. key: "getMap",
  1522. value: function getMap() {
  1523. return StepMap.empty;
  1524. }
  1525. }, {
  1526. key: "invert",
  1527. value: function invert(doc) {
  1528. return new AttrStep(this.pos, this.attr, doc.nodeAt(this.pos).attrs[this.attr]);
  1529. }
  1530. }, {
  1531. key: "map",
  1532. value: function map(mapping) {
  1533. var pos = mapping.mapResult(this.pos, 1);
  1534. return pos.deletedAfter ? null : new AttrStep(pos.pos, this.attr, this.value);
  1535. }
  1536. }, {
  1537. key: "toJSON",
  1538. value: function toJSON() {
  1539. return {
  1540. stepType: "attr",
  1541. pos: this.pos,
  1542. attr: this.attr,
  1543. value: this.value
  1544. };
  1545. }
  1546. }], [{
  1547. key: "fromJSON",
  1548. value: function fromJSON(schema, json) {
  1549. if (typeof json.pos != "number" || typeof json.attr != "string") throw new RangeError("Invalid input for AttrStep.fromJSON");
  1550. return new AttrStep(json.pos, json.attr, json.value);
  1551. }
  1552. }]);
  1553. return AttrStep;
  1554. }(Step);
  1555. Step.jsonID("attr", AttrStep);
  1556. var DocAttrStep = function (_Step8) {
  1557. _inherits(DocAttrStep, _Step8);
  1558. var _super8 = _createSuper(DocAttrStep);
  1559. function DocAttrStep(attr, value) {
  1560. var _this10;
  1561. _classCallCheck(this, DocAttrStep);
  1562. _this10 = _super8.call(this);
  1563. _this10.attr = attr;
  1564. _this10.value = value;
  1565. return _this10;
  1566. }
  1567. _createClass(DocAttrStep, [{
  1568. key: "apply",
  1569. value: function apply(doc) {
  1570. var attrs = Object.create(null);
  1571. for (var name in doc.attrs) {
  1572. attrs[name] = doc.attrs[name];
  1573. }
  1574. attrs[this.attr] = this.value;
  1575. var updated = doc.type.create(attrs, doc.content, doc.marks);
  1576. return StepResult.ok(updated);
  1577. }
  1578. }, {
  1579. key: "getMap",
  1580. value: function getMap() {
  1581. return StepMap.empty;
  1582. }
  1583. }, {
  1584. key: "invert",
  1585. value: function invert(doc) {
  1586. return new DocAttrStep(this.attr, doc.attrs[this.attr]);
  1587. }
  1588. }, {
  1589. key: "map",
  1590. value: function map(mapping) {
  1591. return this;
  1592. }
  1593. }, {
  1594. key: "toJSON",
  1595. value: function toJSON() {
  1596. return {
  1597. stepType: "docAttr",
  1598. attr: this.attr,
  1599. value: this.value
  1600. };
  1601. }
  1602. }], [{
  1603. key: "fromJSON",
  1604. value: function fromJSON(schema, json) {
  1605. if (typeof json.attr != "string") throw new RangeError("Invalid input for DocAttrStep.fromJSON");
  1606. return new DocAttrStep(json.attr, json.value);
  1607. }
  1608. }]);
  1609. return DocAttrStep;
  1610. }(Step);
  1611. Step.jsonID("docAttr", DocAttrStep);
  1612. exports.TransformError = function (_Error) {
  1613. _inherits(TransformError, _Error);
  1614. var _super9 = _createSuper(TransformError);
  1615. function TransformError() {
  1616. _classCallCheck(this, TransformError);
  1617. return _super9.apply(this, arguments);
  1618. }
  1619. return _createClass(TransformError);
  1620. }(_wrapNativeSuper(Error));
  1621. exports.TransformError = function TransformError(message) {
  1622. var err = Error.call(this, message);
  1623. err.__proto__ = TransformError.prototype;
  1624. return err;
  1625. };
  1626. exports.TransformError.prototype = Object.create(Error.prototype);
  1627. exports.TransformError.prototype.constructor = exports.TransformError;
  1628. exports.TransformError.prototype.name = "TransformError";
  1629. var Transform = function () {
  1630. function Transform(doc) {
  1631. _classCallCheck(this, Transform);
  1632. this.doc = doc;
  1633. this.steps = [];
  1634. this.docs = [];
  1635. this.mapping = new Mapping();
  1636. }
  1637. _createClass(Transform, [{
  1638. key: "before",
  1639. get: function get() {
  1640. return this.docs.length ? this.docs[0] : this.doc;
  1641. }
  1642. }, {
  1643. key: "step",
  1644. value: function step(_step) {
  1645. var result = this.maybeStep(_step);
  1646. if (result.failed) throw new exports.TransformError(result.failed);
  1647. return this;
  1648. }
  1649. }, {
  1650. key: "maybeStep",
  1651. value: function maybeStep(step) {
  1652. var result = step.apply(this.doc);
  1653. if (!result.failed) this.addStep(step, result.doc);
  1654. return result;
  1655. }
  1656. }, {
  1657. key: "docChanged",
  1658. get: function get() {
  1659. return this.steps.length > 0;
  1660. }
  1661. }, {
  1662. key: "addStep",
  1663. value: function addStep(step, doc) {
  1664. this.docs.push(this.doc);
  1665. this.steps.push(step);
  1666. this.mapping.appendMap(step.getMap());
  1667. this.doc = doc;
  1668. }
  1669. }, {
  1670. key: "replace",
  1671. value: function replace(from) {
  1672. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : from;
  1673. var slice = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : prosemirrorModel.Slice.empty;
  1674. var step = replaceStep(this.doc, from, to, slice);
  1675. if (step) this.step(step);
  1676. return this;
  1677. }
  1678. }, {
  1679. key: "replaceWith",
  1680. value: function replaceWith(from, to, content) {
  1681. return this.replace(from, to, new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(content), 0, 0));
  1682. }
  1683. }, {
  1684. key: "delete",
  1685. value: function _delete(from, to) {
  1686. return this.replace(from, to, prosemirrorModel.Slice.empty);
  1687. }
  1688. }, {
  1689. key: "insert",
  1690. value: function insert(pos, content) {
  1691. return this.replaceWith(pos, pos, content);
  1692. }
  1693. }, {
  1694. key: "replaceRange",
  1695. value: function replaceRange(from, to, slice) {
  1696. _replaceRange(this, from, to, slice);
  1697. return this;
  1698. }
  1699. }, {
  1700. key: "replaceRangeWith",
  1701. value: function replaceRangeWith(from, to, node) {
  1702. _replaceRangeWith(this, from, to, node);
  1703. return this;
  1704. }
  1705. }, {
  1706. key: "deleteRange",
  1707. value: function deleteRange(from, to) {
  1708. _deleteRange(this, from, to);
  1709. return this;
  1710. }
  1711. }, {
  1712. key: "lift",
  1713. value: function lift(range, target) {
  1714. _lift(this, range, target);
  1715. return this;
  1716. }
  1717. }, {
  1718. key: "join",
  1719. value: function join(pos) {
  1720. var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1721. _join(this, pos, depth);
  1722. return this;
  1723. }
  1724. }, {
  1725. key: "wrap",
  1726. value: function wrap(range, wrappers) {
  1727. _wrap2(this, range, wrappers);
  1728. return this;
  1729. }
  1730. }, {
  1731. key: "setBlockType",
  1732. value: function setBlockType(from) {
  1733. var to = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : from;
  1734. var type = arguments.length > 2 ? arguments[2] : undefined;
  1735. var attrs = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
  1736. _setBlockType(this, from, to, type, attrs);
  1737. return this;
  1738. }
  1739. }, {
  1740. key: "setNodeMarkup",
  1741. value: function setNodeMarkup(pos, type) {
  1742. var attrs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1743. var marks = arguments.length > 3 ? arguments[3] : undefined;
  1744. _setNodeMarkup(this, pos, type, attrs, marks);
  1745. return this;
  1746. }
  1747. }, {
  1748. key: "setNodeAttribute",
  1749. value: function setNodeAttribute(pos, attr, value) {
  1750. this.step(new AttrStep(pos, attr, value));
  1751. return this;
  1752. }
  1753. }, {
  1754. key: "setDocAttribute",
  1755. value: function setDocAttribute(attr, value) {
  1756. this.step(new DocAttrStep(attr, value));
  1757. return this;
  1758. }
  1759. }, {
  1760. key: "addNodeMark",
  1761. value: function addNodeMark(pos, mark) {
  1762. this.step(new AddNodeMarkStep(pos, mark));
  1763. return this;
  1764. }
  1765. }, {
  1766. key: "removeNodeMark",
  1767. value: function removeNodeMark(pos, mark) {
  1768. if (!(mark instanceof prosemirrorModel.Mark)) {
  1769. var node = this.doc.nodeAt(pos);
  1770. if (!node) throw new RangeError("No node at position " + pos);
  1771. mark = mark.isInSet(node.marks);
  1772. if (!mark) return this;
  1773. }
  1774. this.step(new RemoveNodeMarkStep(pos, mark));
  1775. return this;
  1776. }
  1777. }, {
  1778. key: "split",
  1779. value: function split(pos) {
  1780. var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  1781. var typesAfter = arguments.length > 2 ? arguments[2] : undefined;
  1782. _split(this, pos, depth, typesAfter);
  1783. return this;
  1784. }
  1785. }, {
  1786. key: "addMark",
  1787. value: function addMark(from, to, mark) {
  1788. _addMark(this, from, to, mark);
  1789. return this;
  1790. }
  1791. }, {
  1792. key: "removeMark",
  1793. value: function removeMark(from, to, mark) {
  1794. _removeMark(this, from, to, mark);
  1795. return this;
  1796. }
  1797. }, {
  1798. key: "clearIncompatible",
  1799. value: function clearIncompatible(pos, parentType, match) {
  1800. _clearIncompatible(this, pos, parentType, match);
  1801. return this;
  1802. }
  1803. }]);
  1804. return Transform;
  1805. }();
  1806. exports.AddMarkStep = AddMarkStep;
  1807. exports.AddNodeMarkStep = AddNodeMarkStep;
  1808. exports.AttrStep = AttrStep;
  1809. exports.DocAttrStep = DocAttrStep;
  1810. exports.MapResult = MapResult;
  1811. exports.Mapping = Mapping;
  1812. exports.RemoveMarkStep = RemoveMarkStep;
  1813. exports.RemoveNodeMarkStep = RemoveNodeMarkStep;
  1814. exports.ReplaceAroundStep = ReplaceAroundStep;
  1815. exports.ReplaceStep = ReplaceStep;
  1816. exports.Step = Step;
  1817. exports.StepMap = StepMap;
  1818. exports.StepResult = StepResult;
  1819. exports.Transform = Transform;
  1820. exports.canJoin = canJoin;
  1821. exports.canSplit = canSplit;
  1822. exports.dropPoint = dropPoint;
  1823. exports.findWrapping = findWrapping;
  1824. exports.insertPoint = insertPoint;
  1825. exports.joinPoint = joinPoint;
  1826. exports.liftTarget = liftTarget;
  1827. exports.replaceStep = replaceStep;