index.cjs 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077
  1. 'use strict';
  2. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  5. function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
  6. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  7. 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); }
  8. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  9. 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); }; }
  10. 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); }
  11. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  12. 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; } }
  13. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  14. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  16. 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, _toPropertyKey(descriptor.key), descriptor); } }
  17. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  18. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  19. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  20. var prosemirrorState = require('prosemirror-state');
  21. var prosemirrorModel = require('prosemirror-model');
  22. var prosemirrorTransform = require('prosemirror-transform');
  23. var domIndex = function domIndex(node) {
  24. for (var index = 0;; index++) {
  25. node = node.previousSibling;
  26. if (!node) return index;
  27. }
  28. };
  29. var parentNode = function parentNode(node) {
  30. var parent = node.assignedSlot || node.parentNode;
  31. return parent && parent.nodeType == 11 ? parent.host : parent;
  32. };
  33. var reusedRange = null;
  34. var textRange = function textRange(node, from, to) {
  35. var range = reusedRange || (reusedRange = document.createRange());
  36. range.setEnd(node, to == null ? node.nodeValue.length : to);
  37. range.setStart(node, from || 0);
  38. return range;
  39. };
  40. var isEquivalentPosition = function isEquivalentPosition(node, off, targetNode, targetOff) {
  41. return targetNode && (scanFor(node, off, targetNode, targetOff, -1) || scanFor(node, off, targetNode, targetOff, 1));
  42. };
  43. var atomElements = /^(img|br|input|textarea|hr)$/i;
  44. function scanFor(node, off, targetNode, targetOff, dir) {
  45. for (;;) {
  46. if (node == targetNode && off == targetOff) return true;
  47. if (off == (dir < 0 ? 0 : nodeSize(node))) {
  48. var parent = node.parentNode;
  49. if (!parent || parent.nodeType != 1 || hasBlockDesc(node) || atomElements.test(node.nodeName) || node.contentEditable == "false") return false;
  50. off = domIndex(node) + (dir < 0 ? 0 : 1);
  51. node = parent;
  52. } else if (node.nodeType == 1) {
  53. node = node.childNodes[off + (dir < 0 ? -1 : 0)];
  54. if (node.contentEditable == "false") return false;
  55. off = dir < 0 ? nodeSize(node) : 0;
  56. } else {
  57. return false;
  58. }
  59. }
  60. }
  61. function nodeSize(node) {
  62. return node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length;
  63. }
  64. function isOnEdge(node, offset, parent) {
  65. for (var atStart = offset == 0, atEnd = offset == nodeSize(node); atStart || atEnd;) {
  66. if (node == parent) return true;
  67. var index = domIndex(node);
  68. node = node.parentNode;
  69. if (!node) return false;
  70. atStart = atStart && index == 0;
  71. atEnd = atEnd && index == nodeSize(node);
  72. }
  73. }
  74. function hasBlockDesc(dom) {
  75. var desc;
  76. for (var cur = dom; cur; cur = cur.parentNode) if (desc = cur.pmViewDesc) break;
  77. return desc && desc.node && desc.node.isBlock && (desc.dom == dom || desc.contentDOM == dom);
  78. }
  79. var selectionCollapsed = function selectionCollapsed(domSel) {
  80. return domSel.focusNode && isEquivalentPosition(domSel.focusNode, domSel.focusOffset, domSel.anchorNode, domSel.anchorOffset);
  81. };
  82. function keyEvent(keyCode, key) {
  83. var event = document.createEvent("Event");
  84. event.initEvent("keydown", true, true);
  85. event.keyCode = keyCode;
  86. event.key = event.code = key;
  87. return event;
  88. }
  89. function deepActiveElement(doc) {
  90. var elt = doc.activeElement;
  91. while (elt && elt.shadowRoot) elt = elt.shadowRoot.activeElement;
  92. return elt;
  93. }
  94. function caretFromPoint(doc, x, y) {
  95. if (doc.caretPositionFromPoint) {
  96. try {
  97. var pos = doc.caretPositionFromPoint(x, y);
  98. if (pos) return {
  99. node: pos.offsetNode,
  100. offset: pos.offset
  101. };
  102. } catch (_) {}
  103. }
  104. if (doc.caretRangeFromPoint) {
  105. var range = doc.caretRangeFromPoint(x, y);
  106. if (range) return {
  107. node: range.startContainer,
  108. offset: range.startOffset
  109. };
  110. }
  111. }
  112. var nav = typeof navigator != "undefined" ? navigator : null;
  113. var doc = typeof document != "undefined" ? document : null;
  114. var agent = nav && nav.userAgent || "";
  115. var ie_edge = /Edge\/(\d+)/.exec(agent);
  116. var ie_upto10 = /MSIE \d/.exec(agent);
  117. var ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(agent);
  118. var ie = !!(ie_upto10 || ie_11up || ie_edge);
  119. var ie_version = ie_upto10 ? document.documentMode : ie_11up ? +ie_11up[1] : ie_edge ? +ie_edge[1] : 0;
  120. var gecko = !ie && /gecko\/(\d+)/i.test(agent);
  121. gecko && +(/Firefox\/(\d+)/.exec(agent) || [0, 0])[1];
  122. var _chrome = !ie && /Chrome\/(\d+)/.exec(agent);
  123. var chrome = !!_chrome;
  124. var chrome_version = _chrome ? +_chrome[1] : 0;
  125. var safari = !ie && !!nav && /Apple Computer/.test(nav.vendor);
  126. var ios = safari && (/Mobile\/\w+/.test(agent) || !!nav && nav.maxTouchPoints > 2);
  127. var mac = ios || (nav ? /Mac/.test(nav.platform) : false);
  128. var windows = nav ? /Win/.test(nav.platform) : false;
  129. var android = /Android \d/.test(agent);
  130. var webkit = !!doc && "webkitFontSmoothing" in doc.documentElement.style;
  131. var webkit_version = webkit ? +(/\bAppleWebKit\/(\d+)/.exec(navigator.userAgent) || [0, 0])[1] : 0;
  132. function windowRect(doc) {
  133. return {
  134. left: 0,
  135. right: doc.documentElement.clientWidth,
  136. top: 0,
  137. bottom: doc.documentElement.clientHeight
  138. };
  139. }
  140. function getSide(value, side) {
  141. return typeof value == "number" ? value : value[side];
  142. }
  143. function clientRect(node) {
  144. var rect = node.getBoundingClientRect();
  145. var scaleX = rect.width / node.offsetWidth || 1;
  146. var scaleY = rect.height / node.offsetHeight || 1;
  147. return {
  148. left: rect.left,
  149. right: rect.left + node.clientWidth * scaleX,
  150. top: rect.top,
  151. bottom: rect.top + node.clientHeight * scaleY
  152. };
  153. }
  154. function scrollRectIntoView(view, rect, startDOM) {
  155. var scrollThreshold = view.someProp("scrollThreshold") || 0,
  156. scrollMargin = view.someProp("scrollMargin") || 5;
  157. var doc = view.dom.ownerDocument;
  158. for (var parent = startDOM || view.dom;; parent = parentNode(parent)) {
  159. if (!parent) break;
  160. if (parent.nodeType != 1) continue;
  161. var elt = parent;
  162. var atTop = elt == doc.body;
  163. var bounding = atTop ? windowRect(doc) : clientRect(elt);
  164. var moveX = 0,
  165. moveY = 0;
  166. if (rect.top < bounding.top + getSide(scrollThreshold, "top")) moveY = -(bounding.top - rect.top + getSide(scrollMargin, "top"));else if (rect.bottom > bounding.bottom - getSide(scrollThreshold, "bottom")) moveY = rect.bottom - rect.top > bounding.bottom - bounding.top ? rect.top + getSide(scrollMargin, "top") - bounding.top : rect.bottom - bounding.bottom + getSide(scrollMargin, "bottom");
  167. if (rect.left < bounding.left + getSide(scrollThreshold, "left")) moveX = -(bounding.left - rect.left + getSide(scrollMargin, "left"));else if (rect.right > bounding.right - getSide(scrollThreshold, "right")) moveX = rect.right - bounding.right + getSide(scrollMargin, "right");
  168. if (moveX || moveY) {
  169. if (atTop) {
  170. doc.defaultView.scrollBy(moveX, moveY);
  171. } else {
  172. var startX = elt.scrollLeft,
  173. startY = elt.scrollTop;
  174. if (moveY) elt.scrollTop += moveY;
  175. if (moveX) elt.scrollLeft += moveX;
  176. var dX = elt.scrollLeft - startX,
  177. dY = elt.scrollTop - startY;
  178. rect = {
  179. left: rect.left - dX,
  180. top: rect.top - dY,
  181. right: rect.right - dX,
  182. bottom: rect.bottom - dY
  183. };
  184. }
  185. }
  186. if (atTop || /^(fixed|sticky)$/.test(getComputedStyle(parent).position)) break;
  187. }
  188. }
  189. function storeScrollPos(view) {
  190. var rect = view.dom.getBoundingClientRect(),
  191. startY = Math.max(0, rect.top);
  192. var refDOM, refTop;
  193. for (var x = (rect.left + rect.right) / 2, y = startY + 1; y < Math.min(innerHeight, rect.bottom); y += 5) {
  194. var dom = view.root.elementFromPoint(x, y);
  195. if (!dom || dom == view.dom || !view.dom.contains(dom)) continue;
  196. var localRect = dom.getBoundingClientRect();
  197. if (localRect.top >= startY - 20) {
  198. refDOM = dom;
  199. refTop = localRect.top;
  200. break;
  201. }
  202. }
  203. return {
  204. refDOM: refDOM,
  205. refTop: refTop,
  206. stack: scrollStack(view.dom)
  207. };
  208. }
  209. function scrollStack(dom) {
  210. var stack = [],
  211. doc = dom.ownerDocument;
  212. for (var cur = dom; cur; cur = parentNode(cur)) {
  213. stack.push({
  214. dom: cur,
  215. top: cur.scrollTop,
  216. left: cur.scrollLeft
  217. });
  218. if (dom == doc) break;
  219. }
  220. return stack;
  221. }
  222. function resetScrollPos(_ref) {
  223. var refDOM = _ref.refDOM,
  224. refTop = _ref.refTop,
  225. stack = _ref.stack;
  226. var newRefTop = refDOM ? refDOM.getBoundingClientRect().top : 0;
  227. restoreScrollStack(stack, newRefTop == 0 ? 0 : newRefTop - refTop);
  228. }
  229. function restoreScrollStack(stack, dTop) {
  230. for (var i = 0; i < stack.length; i++) {
  231. var _stack$i = stack[i],
  232. dom = _stack$i.dom,
  233. top = _stack$i.top,
  234. left = _stack$i.left;
  235. if (dom.scrollTop != top + dTop) dom.scrollTop = top + dTop;
  236. if (dom.scrollLeft != left) dom.scrollLeft = left;
  237. }
  238. }
  239. var preventScrollSupported = null;
  240. function focusPreventScroll(dom) {
  241. if (dom.setActive) return dom.setActive();
  242. if (preventScrollSupported) return dom.focus(preventScrollSupported);
  243. var stored = scrollStack(dom);
  244. dom.focus(preventScrollSupported == null ? {
  245. get preventScroll() {
  246. preventScrollSupported = {
  247. preventScroll: true
  248. };
  249. return true;
  250. }
  251. } : undefined);
  252. if (!preventScrollSupported) {
  253. preventScrollSupported = false;
  254. restoreScrollStack(stored, 0);
  255. }
  256. }
  257. function findOffsetInNode(node, coords) {
  258. var closest,
  259. dxClosest = 2e8,
  260. coordsClosest,
  261. offset = 0;
  262. var rowBot = coords.top,
  263. rowTop = coords.top;
  264. var firstBelow, coordsBelow;
  265. for (var child = node.firstChild, childIndex = 0; child; child = child.nextSibling, childIndex++) {
  266. var rects = void 0;
  267. if (child.nodeType == 1) rects = child.getClientRects();else if (child.nodeType == 3) rects = textRange(child).getClientRects();else continue;
  268. for (var i = 0; i < rects.length; i++) {
  269. var rect = rects[i];
  270. if (rect.top <= rowBot && rect.bottom >= rowTop) {
  271. rowBot = Math.max(rect.bottom, rowBot);
  272. rowTop = Math.min(rect.top, rowTop);
  273. var dx = rect.left > coords.left ? rect.left - coords.left : rect.right < coords.left ? coords.left - rect.right : 0;
  274. if (dx < dxClosest) {
  275. closest = child;
  276. dxClosest = dx;
  277. coordsClosest = dx && closest.nodeType == 3 ? {
  278. left: rect.right < coords.left ? rect.right : rect.left,
  279. top: coords.top
  280. } : coords;
  281. if (child.nodeType == 1 && dx) offset = childIndex + (coords.left >= (rect.left + rect.right) / 2 ? 1 : 0);
  282. continue;
  283. }
  284. } else if (rect.top > coords.top && !firstBelow && rect.left <= coords.left && rect.right >= coords.left) {
  285. firstBelow = child;
  286. coordsBelow = {
  287. left: Math.max(rect.left, Math.min(rect.right, coords.left)),
  288. top: rect.top
  289. };
  290. }
  291. if (!closest && (coords.left >= rect.right && coords.top >= rect.top || coords.left >= rect.left && coords.top >= rect.bottom)) offset = childIndex + 1;
  292. }
  293. }
  294. if (!closest && firstBelow) {
  295. closest = firstBelow;
  296. coordsClosest = coordsBelow;
  297. dxClosest = 0;
  298. }
  299. if (closest && closest.nodeType == 3) return findOffsetInText(closest, coordsClosest);
  300. if (!closest || dxClosest && closest.nodeType == 1) return {
  301. node: node,
  302. offset: offset
  303. };
  304. return findOffsetInNode(closest, coordsClosest);
  305. }
  306. function findOffsetInText(node, coords) {
  307. var len = node.nodeValue.length;
  308. var range = document.createRange();
  309. for (var i = 0; i < len; i++) {
  310. range.setEnd(node, i + 1);
  311. range.setStart(node, i);
  312. var rect = singleRect(range, 1);
  313. if (rect.top == rect.bottom) continue;
  314. if (inRect(coords, rect)) return {
  315. node: node,
  316. offset: i + (coords.left >= (rect.left + rect.right) / 2 ? 1 : 0)
  317. };
  318. }
  319. return {
  320. node: node,
  321. offset: 0
  322. };
  323. }
  324. function inRect(coords, rect) {
  325. return coords.left >= rect.left - 1 && coords.left <= rect.right + 1 && coords.top >= rect.top - 1 && coords.top <= rect.bottom + 1;
  326. }
  327. function targetKludge(dom, coords) {
  328. var parent = dom.parentNode;
  329. if (parent && /^li$/i.test(parent.nodeName) && coords.left < dom.getBoundingClientRect().left) return parent;
  330. return dom;
  331. }
  332. function posFromElement(view, elt, coords) {
  333. var _findOffsetInNode = findOffsetInNode(elt, coords),
  334. node = _findOffsetInNode.node,
  335. offset = _findOffsetInNode.offset,
  336. bias = -1;
  337. if (node.nodeType == 1 && !node.firstChild) {
  338. var rect = node.getBoundingClientRect();
  339. bias = rect.left != rect.right && coords.left > (rect.left + rect.right) / 2 ? 1 : -1;
  340. }
  341. return view.docView.posFromDOM(node, offset, bias);
  342. }
  343. function posFromCaret(view, node, offset, coords) {
  344. var outsideBlock = -1;
  345. for (var cur = node, sawBlock = false;;) {
  346. if (cur == view.dom) break;
  347. var desc = view.docView.nearestDesc(cur, true);
  348. if (!desc) return null;
  349. if (desc.dom.nodeType == 1 && (desc.node.isBlock && desc.parent && !sawBlock || !desc.contentDOM)) {
  350. var rect = desc.dom.getBoundingClientRect();
  351. if (desc.node.isBlock && desc.parent && !sawBlock) {
  352. sawBlock = true;
  353. if (rect.left > coords.left || rect.top > coords.top) outsideBlock = desc.posBefore;else if (rect.right < coords.left || rect.bottom < coords.top) outsideBlock = desc.posAfter;
  354. }
  355. if (!desc.contentDOM && outsideBlock < 0 && !desc.node.isText) {
  356. var before = desc.node.isBlock ? coords.top < (rect.top + rect.bottom) / 2 : coords.left < (rect.left + rect.right) / 2;
  357. return before ? desc.posBefore : desc.posAfter;
  358. }
  359. }
  360. cur = desc.dom.parentNode;
  361. }
  362. return outsideBlock > -1 ? outsideBlock : view.docView.posFromDOM(node, offset, -1);
  363. }
  364. function elementFromPoint(element, coords, box) {
  365. var len = element.childNodes.length;
  366. if (len && box.top < box.bottom) {
  367. for (var startI = Math.max(0, Math.min(len - 1, Math.floor(len * (coords.top - box.top) / (box.bottom - box.top)) - 2)), i = startI;;) {
  368. var child = element.childNodes[i];
  369. if (child.nodeType == 1) {
  370. var rects = child.getClientRects();
  371. for (var j = 0; j < rects.length; j++) {
  372. var rect = rects[j];
  373. if (inRect(coords, rect)) return elementFromPoint(child, coords, rect);
  374. }
  375. }
  376. if ((i = (i + 1) % len) == startI) break;
  377. }
  378. }
  379. return element;
  380. }
  381. function _posAtCoords(view, coords) {
  382. var doc = view.dom.ownerDocument,
  383. node,
  384. offset = 0;
  385. var caret = caretFromPoint(doc, coords.left, coords.top);
  386. if (caret) {
  387. node = caret.node;
  388. offset = caret.offset;
  389. }
  390. var elt = (view.root.elementFromPoint ? view.root : doc).elementFromPoint(coords.left, coords.top);
  391. var pos;
  392. if (!elt || !view.dom.contains(elt.nodeType != 1 ? elt.parentNode : elt)) {
  393. var box = view.dom.getBoundingClientRect();
  394. if (!inRect(coords, box)) return null;
  395. elt = elementFromPoint(view.dom, coords, box);
  396. if (!elt) return null;
  397. }
  398. if (safari) {
  399. for (var p = elt; node && p; p = parentNode(p)) if (p.draggable) node = undefined;
  400. }
  401. elt = targetKludge(elt, coords);
  402. if (node) {
  403. if (gecko && node.nodeType == 1) {
  404. offset = Math.min(offset, node.childNodes.length);
  405. if (offset < node.childNodes.length) {
  406. var next = node.childNodes[offset],
  407. _box;
  408. if (next.nodeName == "IMG" && (_box = next.getBoundingClientRect()).right <= coords.left && _box.bottom > coords.top) offset++;
  409. }
  410. }
  411. var prev;
  412. if (webkit && offset && node.nodeType == 1 && (prev = node.childNodes[offset - 1]).nodeType == 1 && prev.contentEditable == "false" && prev.getBoundingClientRect().top >= coords.top) offset--;
  413. if (node == view.dom && offset == node.childNodes.length - 1 && node.lastChild.nodeType == 1 && coords.top > node.lastChild.getBoundingClientRect().bottom) pos = view.state.doc.content.size;else if (offset == 0 || node.nodeType != 1 || node.childNodes[offset - 1].nodeName != "BR") pos = posFromCaret(view, node, offset, coords);
  414. }
  415. if (pos == null) pos = posFromElement(view, elt, coords);
  416. var desc = view.docView.nearestDesc(elt, true);
  417. return {
  418. pos: pos,
  419. inside: desc ? desc.posAtStart - desc.border : -1
  420. };
  421. }
  422. function nonZero(rect) {
  423. return rect.top < rect.bottom || rect.left < rect.right;
  424. }
  425. function singleRect(target, bias) {
  426. var rects = target.getClientRects();
  427. if (rects.length) {
  428. var first = rects[bias < 0 ? 0 : rects.length - 1];
  429. if (nonZero(first)) return first;
  430. }
  431. return Array.prototype.find.call(rects, nonZero) || target.getBoundingClientRect();
  432. }
  433. var BIDI = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac]/;
  434. function _coordsAtPos(view, pos, side) {
  435. var _view$docView$domFrom = view.docView.domFromPos(pos, side < 0 ? -1 : 1),
  436. node = _view$docView$domFrom.node,
  437. offset = _view$docView$domFrom.offset,
  438. atom = _view$docView$domFrom.atom;
  439. var supportEmptyRange = webkit || gecko;
  440. if (node.nodeType == 3) {
  441. if (supportEmptyRange && (BIDI.test(node.nodeValue) || (side < 0 ? !offset : offset == node.nodeValue.length))) {
  442. var rect = singleRect(textRange(node, offset, offset), side);
  443. if (gecko && offset && /\s/.test(node.nodeValue[offset - 1]) && offset < node.nodeValue.length) {
  444. var rectBefore = singleRect(textRange(node, offset - 1, offset - 1), -1);
  445. if (rectBefore.top == rect.top) {
  446. var rectAfter = singleRect(textRange(node, offset, offset + 1), -1);
  447. if (rectAfter.top != rect.top) return flattenV(rectAfter, rectAfter.left < rectBefore.left);
  448. }
  449. }
  450. return rect;
  451. } else {
  452. var from = offset,
  453. to = offset,
  454. takeSide = side < 0 ? 1 : -1;
  455. if (side < 0 && !offset) {
  456. to++;
  457. takeSide = -1;
  458. } else if (side >= 0 && offset == node.nodeValue.length) {
  459. from--;
  460. takeSide = 1;
  461. } else if (side < 0) {
  462. from--;
  463. } else {
  464. to++;
  465. }
  466. return flattenV(singleRect(textRange(node, from, to), takeSide), takeSide < 0);
  467. }
  468. }
  469. var $dom = view.state.doc.resolve(pos - (atom || 0));
  470. if (!$dom.parent.inlineContent) {
  471. if (atom == null && offset && (side < 0 || offset == nodeSize(node))) {
  472. var before = node.childNodes[offset - 1];
  473. if (before.nodeType == 1) return flattenH(before.getBoundingClientRect(), false);
  474. }
  475. if (atom == null && offset < nodeSize(node)) {
  476. var after = node.childNodes[offset];
  477. if (after.nodeType == 1) return flattenH(after.getBoundingClientRect(), true);
  478. }
  479. return flattenH(node.getBoundingClientRect(), side >= 0);
  480. }
  481. if (atom == null && offset && (side < 0 || offset == nodeSize(node))) {
  482. var _before = node.childNodes[offset - 1];
  483. var target = _before.nodeType == 3 ? textRange(_before, nodeSize(_before) - (supportEmptyRange ? 0 : 1)) : _before.nodeType == 1 && (_before.nodeName != "BR" || !_before.nextSibling) ? _before : null;
  484. if (target) return flattenV(singleRect(target, 1), false);
  485. }
  486. if (atom == null && offset < nodeSize(node)) {
  487. var _after = node.childNodes[offset];
  488. while (_after.pmViewDesc && _after.pmViewDesc.ignoreForCoords) _after = _after.nextSibling;
  489. var _target = !_after ? null : _after.nodeType == 3 ? textRange(_after, 0, supportEmptyRange ? 0 : 1) : _after.nodeType == 1 ? _after : null;
  490. if (_target) return flattenV(singleRect(_target, -1), true);
  491. }
  492. return flattenV(singleRect(node.nodeType == 3 ? textRange(node) : node, -side), side >= 0);
  493. }
  494. function flattenV(rect, left) {
  495. if (rect.width == 0) return rect;
  496. var x = left ? rect.left : rect.right;
  497. return {
  498. top: rect.top,
  499. bottom: rect.bottom,
  500. left: x,
  501. right: x
  502. };
  503. }
  504. function flattenH(rect, top) {
  505. if (rect.height == 0) return rect;
  506. var y = top ? rect.top : rect.bottom;
  507. return {
  508. top: y,
  509. bottom: y,
  510. left: rect.left,
  511. right: rect.right
  512. };
  513. }
  514. function withFlushedState(view, state, f) {
  515. var viewState = view.state,
  516. active = view.root.activeElement;
  517. if (viewState != state) view.updateState(state);
  518. if (active != view.dom) view.focus();
  519. try {
  520. return f();
  521. } finally {
  522. if (viewState != state) view.updateState(viewState);
  523. if (active != view.dom && active) active.focus();
  524. }
  525. }
  526. function endOfTextblockVertical(view, state, dir) {
  527. var sel = state.selection;
  528. var $pos = dir == "up" ? sel.$from : sel.$to;
  529. return withFlushedState(view, state, function () {
  530. var _view$docView$domFrom2 = view.docView.domFromPos($pos.pos, dir == "up" ? -1 : 1),
  531. dom = _view$docView$domFrom2.node;
  532. for (;;) {
  533. var nearest = view.docView.nearestDesc(dom, true);
  534. if (!nearest) break;
  535. if (nearest.node.isBlock) {
  536. dom = nearest.contentDOM || nearest.dom;
  537. break;
  538. }
  539. dom = nearest.dom.parentNode;
  540. }
  541. var coords = _coordsAtPos(view, $pos.pos, 1);
  542. for (var child = dom.firstChild; child; child = child.nextSibling) {
  543. var boxes = void 0;
  544. if (child.nodeType == 1) boxes = child.getClientRects();else if (child.nodeType == 3) boxes = textRange(child, 0, child.nodeValue.length).getClientRects();else continue;
  545. for (var i = 0; i < boxes.length; i++) {
  546. var box = boxes[i];
  547. if (box.bottom > box.top + 1 && (dir == "up" ? coords.top - box.top > (box.bottom - coords.top) * 2 : box.bottom - coords.bottom > (coords.bottom - box.top) * 2)) return false;
  548. }
  549. }
  550. return true;
  551. });
  552. }
  553. var maybeRTL = /[\u0590-\u08ac]/;
  554. function endOfTextblockHorizontal(view, state, dir) {
  555. var $head = state.selection.$head;
  556. if (!$head.parent.isTextblock) return false;
  557. var offset = $head.parentOffset,
  558. atStart = !offset,
  559. atEnd = offset == $head.parent.content.size;
  560. var sel = view.domSelection();
  561. if (!maybeRTL.test($head.parent.textContent) || !sel.modify) return dir == "left" || dir == "backward" ? atStart : atEnd;
  562. return withFlushedState(view, state, function () {
  563. var _view$domSelectionRan = view.domSelectionRange(),
  564. oldNode = _view$domSelectionRan.focusNode,
  565. oldOff = _view$domSelectionRan.focusOffset,
  566. anchorNode = _view$domSelectionRan.anchorNode,
  567. anchorOffset = _view$domSelectionRan.anchorOffset;
  568. var oldBidiLevel = sel.caretBidiLevel;
  569. sel.modify("move", dir, "character");
  570. var parentDOM = $head.depth ? view.docView.domAfterPos($head.before()) : view.dom;
  571. var _view$domSelectionRan2 = view.domSelectionRange(),
  572. newNode = _view$domSelectionRan2.focusNode,
  573. newOff = _view$domSelectionRan2.focusOffset;
  574. var result = newNode && !parentDOM.contains(newNode.nodeType == 1 ? newNode : newNode.parentNode) || oldNode == newNode && oldOff == newOff;
  575. try {
  576. sel.collapse(anchorNode, anchorOffset);
  577. if (oldNode && (oldNode != anchorNode || oldOff != anchorOffset) && sel.extend) sel.extend(oldNode, oldOff);
  578. } catch (_) {}
  579. if (oldBidiLevel != null) sel.caretBidiLevel = oldBidiLevel;
  580. return result;
  581. });
  582. }
  583. var cachedState = null;
  584. var cachedDir = null;
  585. var cachedResult = false;
  586. function _endOfTextblock(view, state, dir) {
  587. if (cachedState == state && cachedDir == dir) return cachedResult;
  588. cachedState = state;
  589. cachedDir = dir;
  590. return cachedResult = dir == "up" || dir == "down" ? endOfTextblockVertical(view, state, dir) : endOfTextblockHorizontal(view, state, dir);
  591. }
  592. var NOT_DIRTY = 0,
  593. CHILD_DIRTY = 1,
  594. CONTENT_DIRTY = 2,
  595. NODE_DIRTY = 3;
  596. var ViewDesc = function () {
  597. function ViewDesc(parent, children, dom, contentDOM) {
  598. _classCallCheck(this, ViewDesc);
  599. this.parent = parent;
  600. this.children = children;
  601. this.dom = dom;
  602. this.contentDOM = contentDOM;
  603. this.dirty = NOT_DIRTY;
  604. dom.pmViewDesc = this;
  605. }
  606. _createClass(ViewDesc, [{
  607. key: "matchesWidget",
  608. value: function matchesWidget(widget) {
  609. return false;
  610. }
  611. }, {
  612. key: "matchesMark",
  613. value: function matchesMark(mark) {
  614. return false;
  615. }
  616. }, {
  617. key: "matchesNode",
  618. value: function matchesNode(node, outerDeco, innerDeco) {
  619. return false;
  620. }
  621. }, {
  622. key: "matchesHack",
  623. value: function matchesHack(nodeName) {
  624. return false;
  625. }
  626. }, {
  627. key: "parseRule",
  628. value: function parseRule() {
  629. return null;
  630. }
  631. }, {
  632. key: "stopEvent",
  633. value: function stopEvent(event) {
  634. return false;
  635. }
  636. }, {
  637. key: "size",
  638. get: function get() {
  639. var size = 0;
  640. for (var i = 0; i < this.children.length; i++) size += this.children[i].size;
  641. return size;
  642. }
  643. }, {
  644. key: "border",
  645. get: function get() {
  646. return 0;
  647. }
  648. }, {
  649. key: "destroy",
  650. value: function destroy() {
  651. this.parent = undefined;
  652. if (this.dom.pmViewDesc == this) this.dom.pmViewDesc = undefined;
  653. for (var i = 0; i < this.children.length; i++) this.children[i].destroy();
  654. }
  655. }, {
  656. key: "posBeforeChild",
  657. value: function posBeforeChild(child) {
  658. for (var i = 0, pos = this.posAtStart;; i++) {
  659. var cur = this.children[i];
  660. if (cur == child) return pos;
  661. pos += cur.size;
  662. }
  663. }
  664. }, {
  665. key: "posBefore",
  666. get: function get() {
  667. return this.parent.posBeforeChild(this);
  668. }
  669. }, {
  670. key: "posAtStart",
  671. get: function get() {
  672. return this.parent ? this.parent.posBeforeChild(this) + this.border : 0;
  673. }
  674. }, {
  675. key: "posAfter",
  676. get: function get() {
  677. return this.posBefore + this.size;
  678. }
  679. }, {
  680. key: "posAtEnd",
  681. get: function get() {
  682. return this.posAtStart + this.size - 2 * this.border;
  683. }
  684. }, {
  685. key: "localPosFromDOM",
  686. value: function localPosFromDOM(dom, offset, bias) {
  687. if (this.contentDOM && this.contentDOM.contains(dom.nodeType == 1 ? dom : dom.parentNode)) {
  688. if (bias < 0) {
  689. var domBefore, desc;
  690. if (dom == this.contentDOM) {
  691. domBefore = dom.childNodes[offset - 1];
  692. } else {
  693. while (dom.parentNode != this.contentDOM) dom = dom.parentNode;
  694. domBefore = dom.previousSibling;
  695. }
  696. while (domBefore && !((desc = domBefore.pmViewDesc) && desc.parent == this)) domBefore = domBefore.previousSibling;
  697. return domBefore ? this.posBeforeChild(desc) + desc.size : this.posAtStart;
  698. } else {
  699. var domAfter, _desc;
  700. if (dom == this.contentDOM) {
  701. domAfter = dom.childNodes[offset];
  702. } else {
  703. while (dom.parentNode != this.contentDOM) dom = dom.parentNode;
  704. domAfter = dom.nextSibling;
  705. }
  706. while (domAfter && !((_desc = domAfter.pmViewDesc) && _desc.parent == this)) domAfter = domAfter.nextSibling;
  707. return domAfter ? this.posBeforeChild(_desc) : this.posAtEnd;
  708. }
  709. }
  710. var atEnd;
  711. if (dom == this.dom && this.contentDOM) {
  712. atEnd = offset > domIndex(this.contentDOM);
  713. } else if (this.contentDOM && this.contentDOM != this.dom && this.dom.contains(this.contentDOM)) {
  714. atEnd = dom.compareDocumentPosition(this.contentDOM) & 2;
  715. } else if (this.dom.firstChild) {
  716. if (offset == 0) for (var search = dom;; search = search.parentNode) {
  717. if (search == this.dom) {
  718. atEnd = false;
  719. break;
  720. }
  721. if (search.previousSibling) break;
  722. }
  723. if (atEnd == null && offset == dom.childNodes.length) for (var _search = dom;; _search = _search.parentNode) {
  724. if (_search == this.dom) {
  725. atEnd = true;
  726. break;
  727. }
  728. if (_search.nextSibling) break;
  729. }
  730. }
  731. return (atEnd == null ? bias > 0 : atEnd) ? this.posAtEnd : this.posAtStart;
  732. }
  733. }, {
  734. key: "nearestDesc",
  735. value: function nearestDesc(dom) {
  736. var onlyNodes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  737. for (var first = true, cur = dom; cur; cur = cur.parentNode) {
  738. var desc = this.getDesc(cur),
  739. nodeDOM = void 0;
  740. if (desc && (!onlyNodes || desc.node)) {
  741. if (first && (nodeDOM = desc.nodeDOM) && !(nodeDOM.nodeType == 1 ? nodeDOM.contains(dom.nodeType == 1 ? dom : dom.parentNode) : nodeDOM == dom)) first = false;else return desc;
  742. }
  743. }
  744. }
  745. }, {
  746. key: "getDesc",
  747. value: function getDesc(dom) {
  748. var desc = dom.pmViewDesc;
  749. for (var cur = desc; cur; cur = cur.parent) if (cur == this) return desc;
  750. }
  751. }, {
  752. key: "posFromDOM",
  753. value: function posFromDOM(dom, offset, bias) {
  754. for (var scan = dom; scan; scan = scan.parentNode) {
  755. var desc = this.getDesc(scan);
  756. if (desc) return desc.localPosFromDOM(dom, offset, bias);
  757. }
  758. return -1;
  759. }
  760. }, {
  761. key: "descAt",
  762. value: function descAt(pos) {
  763. for (var i = 0, offset = 0; i < this.children.length; i++) {
  764. var child = this.children[i],
  765. end = offset + child.size;
  766. if (offset == pos && end != offset) {
  767. while (!child.border && child.children.length) child = child.children[0];
  768. return child;
  769. }
  770. if (pos < end) return child.descAt(pos - offset - child.border);
  771. offset = end;
  772. }
  773. }
  774. }, {
  775. key: "domFromPos",
  776. value: function domFromPos(pos, side) {
  777. if (!this.contentDOM) return {
  778. node: this.dom,
  779. offset: 0,
  780. atom: pos + 1
  781. };
  782. var i = 0,
  783. offset = 0;
  784. for (var curPos = 0; i < this.children.length; i++) {
  785. var child = this.children[i],
  786. end = curPos + child.size;
  787. if (end > pos || child instanceof TrailingHackViewDesc) {
  788. offset = pos - curPos;
  789. break;
  790. }
  791. curPos = end;
  792. }
  793. if (offset) return this.children[i].domFromPos(offset - this.children[i].border, side);
  794. for (var prev; i && !(prev = this.children[i - 1]).size && prev instanceof WidgetViewDesc && prev.side >= 0; i--) {}
  795. if (side <= 0) {
  796. var _prev,
  797. enter = true;
  798. for (;; i--, enter = false) {
  799. _prev = i ? this.children[i - 1] : null;
  800. if (!_prev || _prev.dom.parentNode == this.contentDOM) break;
  801. }
  802. if (_prev && side && enter && !_prev.border && !_prev.domAtom) return _prev.domFromPos(_prev.size, side);
  803. return {
  804. node: this.contentDOM,
  805. offset: _prev ? domIndex(_prev.dom) + 1 : 0
  806. };
  807. } else {
  808. var next,
  809. _enter = true;
  810. for (;; i++, _enter = false) {
  811. next = i < this.children.length ? this.children[i] : null;
  812. if (!next || next.dom.parentNode == this.contentDOM) break;
  813. }
  814. if (next && _enter && !next.border && !next.domAtom) return next.domFromPos(0, side);
  815. return {
  816. node: this.contentDOM,
  817. offset: next ? domIndex(next.dom) : this.contentDOM.childNodes.length
  818. };
  819. }
  820. }
  821. }, {
  822. key: "parseRange",
  823. value: function parseRange(from, to) {
  824. var base = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  825. if (this.children.length == 0) return {
  826. node: this.contentDOM,
  827. from: from,
  828. to: to,
  829. fromOffset: 0,
  830. toOffset: this.contentDOM.childNodes.length
  831. };
  832. var fromOffset = -1,
  833. toOffset = -1;
  834. for (var offset = base, i = 0;; i++) {
  835. var child = this.children[i],
  836. end = offset + child.size;
  837. if (fromOffset == -1 && from <= end) {
  838. var childBase = offset + child.border;
  839. if (from >= childBase && to <= end - child.border && child.node && child.contentDOM && this.contentDOM.contains(child.contentDOM)) return child.parseRange(from, to, childBase);
  840. from = offset;
  841. for (var j = i; j > 0; j--) {
  842. var prev = this.children[j - 1];
  843. if (prev.size && prev.dom.parentNode == this.contentDOM && !prev.emptyChildAt(1)) {
  844. fromOffset = domIndex(prev.dom) + 1;
  845. break;
  846. }
  847. from -= prev.size;
  848. }
  849. if (fromOffset == -1) fromOffset = 0;
  850. }
  851. if (fromOffset > -1 && (end > to || i == this.children.length - 1)) {
  852. to = end;
  853. for (var _j = i + 1; _j < this.children.length; _j++) {
  854. var next = this.children[_j];
  855. if (next.size && next.dom.parentNode == this.contentDOM && !next.emptyChildAt(-1)) {
  856. toOffset = domIndex(next.dom);
  857. break;
  858. }
  859. to += next.size;
  860. }
  861. if (toOffset == -1) toOffset = this.contentDOM.childNodes.length;
  862. break;
  863. }
  864. offset = end;
  865. }
  866. return {
  867. node: this.contentDOM,
  868. from: from,
  869. to: to,
  870. fromOffset: fromOffset,
  871. toOffset: toOffset
  872. };
  873. }
  874. }, {
  875. key: "emptyChildAt",
  876. value: function emptyChildAt(side) {
  877. if (this.border || !this.contentDOM || !this.children.length) return false;
  878. var child = this.children[side < 0 ? 0 : this.children.length - 1];
  879. return child.size == 0 || child.emptyChildAt(side);
  880. }
  881. }, {
  882. key: "domAfterPos",
  883. value: function domAfterPos(pos) {
  884. var _this$domFromPos = this.domFromPos(pos, 0),
  885. node = _this$domFromPos.node,
  886. offset = _this$domFromPos.offset;
  887. if (node.nodeType != 1 || offset == node.childNodes.length) throw new RangeError("No node after pos " + pos);
  888. return node.childNodes[offset];
  889. }
  890. }, {
  891. key: "setSelection",
  892. value: function setSelection(anchor, head, root) {
  893. var force = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  894. var from = Math.min(anchor, head),
  895. to = Math.max(anchor, head);
  896. for (var i = 0, offset = 0; i < this.children.length; i++) {
  897. var child = this.children[i],
  898. end = offset + child.size;
  899. if (from > offset && to < end) return child.setSelection(anchor - offset - child.border, head - offset - child.border, root, force);
  900. offset = end;
  901. }
  902. var anchorDOM = this.domFromPos(anchor, anchor ? -1 : 1);
  903. var headDOM = head == anchor ? anchorDOM : this.domFromPos(head, head ? -1 : 1);
  904. var domSel = root.getSelection();
  905. var brKludge = false;
  906. if ((gecko || safari) && anchor == head) {
  907. var _anchorDOM = anchorDOM,
  908. node = _anchorDOM.node,
  909. _offset = _anchorDOM.offset;
  910. if (node.nodeType == 3) {
  911. brKludge = !!(_offset && node.nodeValue[_offset - 1] == "\n");
  912. if (brKludge && _offset == node.nodeValue.length) {
  913. for (var scan = node, after; scan; scan = scan.parentNode) {
  914. if (after = scan.nextSibling) {
  915. if (after.nodeName == "BR") anchorDOM = headDOM = {
  916. node: after.parentNode,
  917. offset: domIndex(after) + 1
  918. };
  919. break;
  920. }
  921. var desc = scan.pmViewDesc;
  922. if (desc && desc.node && desc.node.isBlock) break;
  923. }
  924. }
  925. } else {
  926. var prev = node.childNodes[_offset - 1];
  927. brKludge = prev && (prev.nodeName == "BR" || prev.contentEditable == "false");
  928. }
  929. }
  930. if (gecko && domSel.focusNode && domSel.focusNode != headDOM.node && domSel.focusNode.nodeType == 1) {
  931. var _after2 = domSel.focusNode.childNodes[domSel.focusOffset];
  932. if (_after2 && _after2.contentEditable == "false") force = true;
  933. }
  934. if (!(force || brKludge && safari) && isEquivalentPosition(anchorDOM.node, anchorDOM.offset, domSel.anchorNode, domSel.anchorOffset) && isEquivalentPosition(headDOM.node, headDOM.offset, domSel.focusNode, domSel.focusOffset)) return;
  935. var domSelExtended = false;
  936. if ((domSel.extend || anchor == head) && !brKludge) {
  937. domSel.collapse(anchorDOM.node, anchorDOM.offset);
  938. try {
  939. if (anchor != head) domSel.extend(headDOM.node, headDOM.offset);
  940. domSelExtended = true;
  941. } catch (_) {}
  942. }
  943. if (!domSelExtended) {
  944. if (anchor > head) {
  945. var tmp = anchorDOM;
  946. anchorDOM = headDOM;
  947. headDOM = tmp;
  948. }
  949. var range = document.createRange();
  950. range.setEnd(headDOM.node, headDOM.offset);
  951. range.setStart(anchorDOM.node, anchorDOM.offset);
  952. domSel.removeAllRanges();
  953. domSel.addRange(range);
  954. }
  955. }
  956. }, {
  957. key: "ignoreMutation",
  958. value: function ignoreMutation(mutation) {
  959. return !this.contentDOM && mutation.type != "selection";
  960. }
  961. }, {
  962. key: "contentLost",
  963. get: function get() {
  964. return this.contentDOM && this.contentDOM != this.dom && !this.dom.contains(this.contentDOM);
  965. }
  966. }, {
  967. key: "markDirty",
  968. value: function markDirty(from, to) {
  969. for (var offset = 0, i = 0; i < this.children.length; i++) {
  970. var child = this.children[i],
  971. end = offset + child.size;
  972. if (offset == end ? from <= end && to >= offset : from < end && to > offset) {
  973. var startInside = offset + child.border,
  974. endInside = end - child.border;
  975. if (from >= startInside && to <= endInside) {
  976. this.dirty = from == offset || to == end ? CONTENT_DIRTY : CHILD_DIRTY;
  977. if (from == startInside && to == endInside && (child.contentLost || child.dom.parentNode != this.contentDOM)) child.dirty = NODE_DIRTY;else child.markDirty(from - startInside, to - startInside);
  978. return;
  979. } else {
  980. child.dirty = child.dom == child.contentDOM && child.dom.parentNode == this.contentDOM && !child.children.length ? CONTENT_DIRTY : NODE_DIRTY;
  981. }
  982. }
  983. offset = end;
  984. }
  985. this.dirty = CONTENT_DIRTY;
  986. }
  987. }, {
  988. key: "markParentsDirty",
  989. value: function markParentsDirty() {
  990. var level = 1;
  991. for (var node = this.parent; node; node = node.parent, level++) {
  992. var dirty = level == 1 ? CONTENT_DIRTY : CHILD_DIRTY;
  993. if (node.dirty < dirty) node.dirty = dirty;
  994. }
  995. }
  996. }, {
  997. key: "domAtom",
  998. get: function get() {
  999. return false;
  1000. }
  1001. }, {
  1002. key: "ignoreForCoords",
  1003. get: function get() {
  1004. return false;
  1005. }
  1006. }]);
  1007. return ViewDesc;
  1008. }();
  1009. var WidgetViewDesc = function (_ViewDesc) {
  1010. _inherits(WidgetViewDesc, _ViewDesc);
  1011. var _super = _createSuper(WidgetViewDesc);
  1012. function WidgetViewDesc(parent, widget, view, pos) {
  1013. var _this;
  1014. _classCallCheck(this, WidgetViewDesc);
  1015. var self,
  1016. dom = widget.type.toDOM;
  1017. if (typeof dom == "function") dom = dom(view, function () {
  1018. if (!self) return pos;
  1019. if (self.parent) return self.parent.posBeforeChild(self);
  1020. });
  1021. if (!widget.type.spec.raw) {
  1022. if (dom.nodeType != 1) {
  1023. var wrap = document.createElement("span");
  1024. wrap.appendChild(dom);
  1025. dom = wrap;
  1026. }
  1027. dom.contentEditable = "false";
  1028. dom.classList.add("ProseMirror-widget");
  1029. }
  1030. _this = _super.call(this, parent, [], dom, null);
  1031. _this.widget = widget;
  1032. _this.widget = widget;
  1033. self = _assertThisInitialized(_this);
  1034. return _this;
  1035. }
  1036. _createClass(WidgetViewDesc, [{
  1037. key: "matchesWidget",
  1038. value: function matchesWidget(widget) {
  1039. return this.dirty == NOT_DIRTY && widget.type.eq(this.widget.type);
  1040. }
  1041. }, {
  1042. key: "parseRule",
  1043. value: function parseRule() {
  1044. return {
  1045. ignore: true
  1046. };
  1047. }
  1048. }, {
  1049. key: "stopEvent",
  1050. value: function stopEvent(event) {
  1051. var stop = this.widget.spec.stopEvent;
  1052. return stop ? stop(event) : false;
  1053. }
  1054. }, {
  1055. key: "ignoreMutation",
  1056. value: function ignoreMutation(mutation) {
  1057. return mutation.type != "selection" || this.widget.spec.ignoreSelection;
  1058. }
  1059. }, {
  1060. key: "destroy",
  1061. value: function destroy() {
  1062. this.widget.type.destroy(this.dom);
  1063. _get(_getPrototypeOf(WidgetViewDesc.prototype), "destroy", this).call(this);
  1064. }
  1065. }, {
  1066. key: "domAtom",
  1067. get: function get() {
  1068. return true;
  1069. }
  1070. }, {
  1071. key: "side",
  1072. get: function get() {
  1073. return this.widget.type.side;
  1074. }
  1075. }]);
  1076. return WidgetViewDesc;
  1077. }(ViewDesc);
  1078. var CompositionViewDesc = function (_ViewDesc2) {
  1079. _inherits(CompositionViewDesc, _ViewDesc2);
  1080. var _super2 = _createSuper(CompositionViewDesc);
  1081. function CompositionViewDesc(parent, dom, textDOM, text) {
  1082. var _this2;
  1083. _classCallCheck(this, CompositionViewDesc);
  1084. _this2 = _super2.call(this, parent, [], dom, null);
  1085. _this2.textDOM = textDOM;
  1086. _this2.text = text;
  1087. return _this2;
  1088. }
  1089. _createClass(CompositionViewDesc, [{
  1090. key: "size",
  1091. get: function get() {
  1092. return this.text.length;
  1093. }
  1094. }, {
  1095. key: "localPosFromDOM",
  1096. value: function localPosFromDOM(dom, offset) {
  1097. if (dom != this.textDOM) return this.posAtStart + (offset ? this.size : 0);
  1098. return this.posAtStart + offset;
  1099. }
  1100. }, {
  1101. key: "domFromPos",
  1102. value: function domFromPos(pos) {
  1103. return {
  1104. node: this.textDOM,
  1105. offset: pos
  1106. };
  1107. }
  1108. }, {
  1109. key: "ignoreMutation",
  1110. value: function ignoreMutation(mut) {
  1111. return mut.type === 'characterData' && mut.target.nodeValue == mut.oldValue;
  1112. }
  1113. }]);
  1114. return CompositionViewDesc;
  1115. }(ViewDesc);
  1116. var MarkViewDesc = function (_ViewDesc3) {
  1117. _inherits(MarkViewDesc, _ViewDesc3);
  1118. var _super3 = _createSuper(MarkViewDesc);
  1119. function MarkViewDesc(parent, mark, dom, contentDOM) {
  1120. var _this3;
  1121. _classCallCheck(this, MarkViewDesc);
  1122. _this3 = _super3.call(this, parent, [], dom, contentDOM);
  1123. _this3.mark = mark;
  1124. return _this3;
  1125. }
  1126. _createClass(MarkViewDesc, [{
  1127. key: "parseRule",
  1128. value: function parseRule() {
  1129. if (this.dirty & NODE_DIRTY || this.mark.type.spec.reparseInView) return null;
  1130. return {
  1131. mark: this.mark.type.name,
  1132. attrs: this.mark.attrs,
  1133. contentElement: this.contentDOM
  1134. };
  1135. }
  1136. }, {
  1137. key: "matchesMark",
  1138. value: function matchesMark(mark) {
  1139. return this.dirty != NODE_DIRTY && this.mark.eq(mark);
  1140. }
  1141. }, {
  1142. key: "markDirty",
  1143. value: function markDirty(from, to) {
  1144. _get(_getPrototypeOf(MarkViewDesc.prototype), "markDirty", this).call(this, from, to);
  1145. if (this.dirty != NOT_DIRTY) {
  1146. var parent = this.parent;
  1147. while (!parent.node) parent = parent.parent;
  1148. if (parent.dirty < this.dirty) parent.dirty = this.dirty;
  1149. this.dirty = NOT_DIRTY;
  1150. }
  1151. }
  1152. }, {
  1153. key: "slice",
  1154. value: function slice(from, to, view) {
  1155. var copy = MarkViewDesc.create(this.parent, this.mark, true, view);
  1156. var nodes = this.children,
  1157. size = this.size;
  1158. if (to < size) nodes = replaceNodes(nodes, to, size, view);
  1159. if (from > 0) nodes = replaceNodes(nodes, 0, from, view);
  1160. for (var i = 0; i < nodes.length; i++) nodes[i].parent = copy;
  1161. copy.children = nodes;
  1162. return copy;
  1163. }
  1164. }], [{
  1165. key: "create",
  1166. value: function create(parent, mark, inline, view) {
  1167. var custom = view.nodeViews[mark.type.name];
  1168. var spec = custom && custom(mark, view, inline);
  1169. if (!spec || !spec.dom) spec = prosemirrorModel.DOMSerializer.renderSpec(document, mark.type.spec.toDOM(mark, inline));
  1170. return new MarkViewDesc(parent, mark, spec.dom, spec.contentDOM || spec.dom);
  1171. }
  1172. }]);
  1173. return MarkViewDesc;
  1174. }(ViewDesc);
  1175. var NodeViewDesc = function (_ViewDesc4) {
  1176. _inherits(NodeViewDesc, _ViewDesc4);
  1177. var _super4 = _createSuper(NodeViewDesc);
  1178. function NodeViewDesc(parent, node, outerDeco, innerDeco, dom, contentDOM, nodeDOM, view, pos) {
  1179. var _this4;
  1180. _classCallCheck(this, NodeViewDesc);
  1181. _this4 = _super4.call(this, parent, [], dom, contentDOM);
  1182. _this4.node = node;
  1183. _this4.outerDeco = outerDeco;
  1184. _this4.innerDeco = innerDeco;
  1185. _this4.nodeDOM = nodeDOM;
  1186. return _this4;
  1187. }
  1188. _createClass(NodeViewDesc, [{
  1189. key: "parseRule",
  1190. value: function parseRule() {
  1191. var _this5 = this;
  1192. if (this.node.type.spec.reparseInView) return null;
  1193. var rule = {
  1194. node: this.node.type.name,
  1195. attrs: this.node.attrs
  1196. };
  1197. if (this.node.type.whitespace == "pre") rule.preserveWhitespace = "full";
  1198. if (!this.contentDOM) {
  1199. rule.getContent = function () {
  1200. return _this5.node.content;
  1201. };
  1202. } else if (!this.contentLost) {
  1203. rule.contentElement = this.contentDOM;
  1204. } else {
  1205. for (var i = this.children.length - 1; i >= 0; i--) {
  1206. var child = this.children[i];
  1207. if (this.dom.contains(child.dom.parentNode)) {
  1208. rule.contentElement = child.dom.parentNode;
  1209. break;
  1210. }
  1211. }
  1212. if (!rule.contentElement) rule.getContent = function () {
  1213. return prosemirrorModel.Fragment.empty;
  1214. };
  1215. }
  1216. return rule;
  1217. }
  1218. }, {
  1219. key: "matchesNode",
  1220. value: function matchesNode(node, outerDeco, innerDeco) {
  1221. return this.dirty == NOT_DIRTY && node.eq(this.node) && sameOuterDeco(outerDeco, this.outerDeco) && innerDeco.eq(this.innerDeco);
  1222. }
  1223. }, {
  1224. key: "size",
  1225. get: function get() {
  1226. return this.node.nodeSize;
  1227. }
  1228. }, {
  1229. key: "border",
  1230. get: function get() {
  1231. return this.node.isLeaf ? 0 : 1;
  1232. }
  1233. }, {
  1234. key: "updateChildren",
  1235. value: function updateChildren(view, pos) {
  1236. var _this6 = this;
  1237. var inline = this.node.inlineContent,
  1238. off = pos;
  1239. var composition = view.composing ? this.localCompositionInfo(view, pos) : null;
  1240. var localComposition = composition && composition.pos > -1 ? composition : null;
  1241. var compositionInChild = composition && composition.pos < 0;
  1242. var updater = new ViewTreeUpdater(this, localComposition && localComposition.node, view);
  1243. iterDeco(this.node, this.innerDeco, function (widget, i, insideNode) {
  1244. if (widget.spec.marks) updater.syncToMarks(widget.spec.marks, inline, view);else if (widget.type.side >= 0 && !insideNode) updater.syncToMarks(i == _this6.node.childCount ? prosemirrorModel.Mark.none : _this6.node.child(i).marks, inline, view);
  1245. updater.placeWidget(widget, view, off);
  1246. }, function (child, outerDeco, innerDeco, i) {
  1247. updater.syncToMarks(child.marks, inline, view);
  1248. var compIndex;
  1249. if (updater.findNodeMatch(child, outerDeco, innerDeco, i)) ;else if (compositionInChild && view.state.selection.from > off && view.state.selection.to < off + child.nodeSize && (compIndex = updater.findIndexWithChild(composition.node)) > -1 && updater.updateNodeAt(child, outerDeco, innerDeco, compIndex, view)) ;else if (updater.updateNextNode(child, outerDeco, innerDeco, view, i, off)) ;else {
  1250. updater.addNode(child, outerDeco, innerDeco, view, off);
  1251. }
  1252. off += child.nodeSize;
  1253. });
  1254. updater.syncToMarks([], inline, view);
  1255. if (this.node.isTextblock) updater.addTextblockHacks();
  1256. updater.destroyRest();
  1257. if (updater.changed || this.dirty == CONTENT_DIRTY) {
  1258. if (localComposition) this.protectLocalComposition(view, localComposition);
  1259. renderDescs(this.contentDOM, this.children, view);
  1260. if (ios) iosHacks(this.dom);
  1261. }
  1262. }
  1263. }, {
  1264. key: "localCompositionInfo",
  1265. value: function localCompositionInfo(view, pos) {
  1266. var _view$state$selection = view.state.selection,
  1267. from = _view$state$selection.from,
  1268. to = _view$state$selection.to;
  1269. if (!(view.state.selection instanceof prosemirrorState.TextSelection) || from < pos || to > pos + this.node.content.size) return null;
  1270. var sel = view.domSelectionRange();
  1271. var textNode = nearbyTextNode(sel.focusNode, sel.focusOffset);
  1272. if (!textNode || !this.dom.contains(textNode.parentNode)) return null;
  1273. if (this.node.inlineContent) {
  1274. var text = textNode.nodeValue;
  1275. var textPos = findTextInFragment(this.node.content, text, from - pos, to - pos);
  1276. return textPos < 0 ? null : {
  1277. node: textNode,
  1278. pos: textPos,
  1279. text: text
  1280. };
  1281. } else {
  1282. return {
  1283. node: textNode,
  1284. pos: -1,
  1285. text: ""
  1286. };
  1287. }
  1288. }
  1289. }, {
  1290. key: "protectLocalComposition",
  1291. value: function protectLocalComposition(view, _ref2) {
  1292. var node = _ref2.node,
  1293. pos = _ref2.pos,
  1294. text = _ref2.text;
  1295. if (this.getDesc(node)) return;
  1296. var topNode = node;
  1297. for (;; topNode = topNode.parentNode) {
  1298. if (topNode.parentNode == this.contentDOM) break;
  1299. while (topNode.previousSibling) topNode.parentNode.removeChild(topNode.previousSibling);
  1300. while (topNode.nextSibling) topNode.parentNode.removeChild(topNode.nextSibling);
  1301. if (topNode.pmViewDesc) topNode.pmViewDesc = undefined;
  1302. }
  1303. var desc = new CompositionViewDesc(this, topNode, node, text);
  1304. view.input.compositionNodes.push(desc);
  1305. this.children = replaceNodes(this.children, pos, pos + text.length, view, desc);
  1306. }
  1307. }, {
  1308. key: "update",
  1309. value: function update(node, outerDeco, innerDeco, view) {
  1310. if (this.dirty == NODE_DIRTY || !node.sameMarkup(this.node)) return false;
  1311. this.updateInner(node, outerDeco, innerDeco, view);
  1312. return true;
  1313. }
  1314. }, {
  1315. key: "updateInner",
  1316. value: function updateInner(node, outerDeco, innerDeco, view) {
  1317. this.updateOuterDeco(outerDeco);
  1318. this.node = node;
  1319. this.innerDeco = innerDeco;
  1320. if (this.contentDOM) this.updateChildren(view, this.posAtStart);
  1321. this.dirty = NOT_DIRTY;
  1322. }
  1323. }, {
  1324. key: "updateOuterDeco",
  1325. value: function updateOuterDeco(outerDeco) {
  1326. if (sameOuterDeco(outerDeco, this.outerDeco)) return;
  1327. var needsWrap = this.nodeDOM.nodeType != 1;
  1328. var oldDOM = this.dom;
  1329. this.dom = patchOuterDeco(this.dom, this.nodeDOM, computeOuterDeco(this.outerDeco, this.node, needsWrap), computeOuterDeco(outerDeco, this.node, needsWrap));
  1330. if (this.dom != oldDOM) {
  1331. oldDOM.pmViewDesc = undefined;
  1332. this.dom.pmViewDesc = this;
  1333. }
  1334. this.outerDeco = outerDeco;
  1335. }
  1336. }, {
  1337. key: "selectNode",
  1338. value: function selectNode() {
  1339. if (this.nodeDOM.nodeType == 1) this.nodeDOM.classList.add("ProseMirror-selectednode");
  1340. if (this.contentDOM || !this.node.type.spec.draggable) this.dom.draggable = true;
  1341. }
  1342. }, {
  1343. key: "deselectNode",
  1344. value: function deselectNode() {
  1345. if (this.nodeDOM.nodeType == 1) this.nodeDOM.classList.remove("ProseMirror-selectednode");
  1346. if (this.contentDOM || !this.node.type.spec.draggable) this.dom.removeAttribute("draggable");
  1347. }
  1348. }, {
  1349. key: "domAtom",
  1350. get: function get() {
  1351. return this.node.isAtom;
  1352. }
  1353. }], [{
  1354. key: "create",
  1355. value: function create(parent, node, outerDeco, innerDeco, view, pos) {
  1356. var custom = view.nodeViews[node.type.name],
  1357. descObj;
  1358. var spec = custom && custom(node, view, function () {
  1359. if (!descObj) return pos;
  1360. if (descObj.parent) return descObj.parent.posBeforeChild(descObj);
  1361. }, outerDeco, innerDeco);
  1362. var dom = spec && spec.dom,
  1363. contentDOM = spec && spec.contentDOM;
  1364. if (node.isText) {
  1365. if (!dom) dom = document.createTextNode(node.text);else if (dom.nodeType != 3) throw new RangeError("Text must be rendered as a DOM text node");
  1366. } else if (!dom) {
  1367. var _prosemirrorModel$DOM = prosemirrorModel.DOMSerializer.renderSpec(document, node.type.spec.toDOM(node));
  1368. dom = _prosemirrorModel$DOM.dom;
  1369. contentDOM = _prosemirrorModel$DOM.contentDOM;
  1370. }
  1371. if (!contentDOM && !node.isText && dom.nodeName != "BR") {
  1372. if (!dom.hasAttribute("contenteditable")) dom.contentEditable = "false";
  1373. if (node.type.spec.draggable) dom.draggable = true;
  1374. }
  1375. var nodeDOM = dom;
  1376. dom = applyOuterDeco(dom, outerDeco, node);
  1377. if (spec) return descObj = new CustomNodeViewDesc(parent, node, outerDeco, innerDeco, dom, contentDOM || null, nodeDOM, spec, view, pos + 1);else if (node.isText) return new TextViewDesc(parent, node, outerDeco, innerDeco, dom, nodeDOM, view);else return new NodeViewDesc(parent, node, outerDeco, innerDeco, dom, contentDOM || null, nodeDOM, view, pos + 1);
  1378. }
  1379. }]);
  1380. return NodeViewDesc;
  1381. }(ViewDesc);
  1382. function docViewDesc(doc, outerDeco, innerDeco, dom, view) {
  1383. applyOuterDeco(dom, outerDeco, doc);
  1384. var docView = new NodeViewDesc(undefined, doc, outerDeco, innerDeco, dom, dom, dom, view, 0);
  1385. if (docView.contentDOM) docView.updateChildren(view, 0);
  1386. return docView;
  1387. }
  1388. var TextViewDesc = function (_NodeViewDesc) {
  1389. _inherits(TextViewDesc, _NodeViewDesc);
  1390. var _super5 = _createSuper(TextViewDesc);
  1391. function TextViewDesc(parent, node, outerDeco, innerDeco, dom, nodeDOM, view) {
  1392. _classCallCheck(this, TextViewDesc);
  1393. return _super5.call(this, parent, node, outerDeco, innerDeco, dom, null, nodeDOM, view, 0);
  1394. }
  1395. _createClass(TextViewDesc, [{
  1396. key: "parseRule",
  1397. value: function parseRule() {
  1398. var skip = this.nodeDOM.parentNode;
  1399. while (skip && skip != this.dom && !skip.pmIsDeco) skip = skip.parentNode;
  1400. return {
  1401. skip: skip || true
  1402. };
  1403. }
  1404. }, {
  1405. key: "update",
  1406. value: function update(node, outerDeco, innerDeco, view) {
  1407. if (this.dirty == NODE_DIRTY || this.dirty != NOT_DIRTY && !this.inParent() || !node.sameMarkup(this.node)) return false;
  1408. this.updateOuterDeco(outerDeco);
  1409. if ((this.dirty != NOT_DIRTY || node.text != this.node.text) && node.text != this.nodeDOM.nodeValue) {
  1410. this.nodeDOM.nodeValue = node.text;
  1411. if (view.trackWrites == this.nodeDOM) view.trackWrites = null;
  1412. }
  1413. this.node = node;
  1414. this.dirty = NOT_DIRTY;
  1415. return true;
  1416. }
  1417. }, {
  1418. key: "inParent",
  1419. value: function inParent() {
  1420. var parentDOM = this.parent.contentDOM;
  1421. for (var n = this.nodeDOM; n; n = n.parentNode) if (n == parentDOM) return true;
  1422. return false;
  1423. }
  1424. }, {
  1425. key: "domFromPos",
  1426. value: function domFromPos(pos) {
  1427. return {
  1428. node: this.nodeDOM,
  1429. offset: pos
  1430. };
  1431. }
  1432. }, {
  1433. key: "localPosFromDOM",
  1434. value: function localPosFromDOM(dom, offset, bias) {
  1435. if (dom == this.nodeDOM) return this.posAtStart + Math.min(offset, this.node.text.length);
  1436. return _get(_getPrototypeOf(TextViewDesc.prototype), "localPosFromDOM", this).call(this, dom, offset, bias);
  1437. }
  1438. }, {
  1439. key: "ignoreMutation",
  1440. value: function ignoreMutation(mutation) {
  1441. return mutation.type != "characterData" && mutation.type != "selection";
  1442. }
  1443. }, {
  1444. key: "slice",
  1445. value: function slice(from, to, view) {
  1446. var node = this.node.cut(from, to),
  1447. dom = document.createTextNode(node.text);
  1448. return new TextViewDesc(this.parent, node, this.outerDeco, this.innerDeco, dom, dom, view);
  1449. }
  1450. }, {
  1451. key: "markDirty",
  1452. value: function markDirty(from, to) {
  1453. _get(_getPrototypeOf(TextViewDesc.prototype), "markDirty", this).call(this, from, to);
  1454. if (this.dom != this.nodeDOM && (from == 0 || to == this.nodeDOM.nodeValue.length)) this.dirty = NODE_DIRTY;
  1455. }
  1456. }, {
  1457. key: "domAtom",
  1458. get: function get() {
  1459. return false;
  1460. }
  1461. }]);
  1462. return TextViewDesc;
  1463. }(NodeViewDesc);
  1464. var TrailingHackViewDesc = function (_ViewDesc5) {
  1465. _inherits(TrailingHackViewDesc, _ViewDesc5);
  1466. var _super6 = _createSuper(TrailingHackViewDesc);
  1467. function TrailingHackViewDesc() {
  1468. _classCallCheck(this, TrailingHackViewDesc);
  1469. return _super6.apply(this, arguments);
  1470. }
  1471. _createClass(TrailingHackViewDesc, [{
  1472. key: "parseRule",
  1473. value: function parseRule() {
  1474. return {
  1475. ignore: true
  1476. };
  1477. }
  1478. }, {
  1479. key: "matchesHack",
  1480. value: function matchesHack(nodeName) {
  1481. return this.dirty == NOT_DIRTY && this.dom.nodeName == nodeName;
  1482. }
  1483. }, {
  1484. key: "domAtom",
  1485. get: function get() {
  1486. return true;
  1487. }
  1488. }, {
  1489. key: "ignoreForCoords",
  1490. get: function get() {
  1491. return this.dom.nodeName == "IMG";
  1492. }
  1493. }]);
  1494. return TrailingHackViewDesc;
  1495. }(ViewDesc);
  1496. var CustomNodeViewDesc = function (_NodeViewDesc2) {
  1497. _inherits(CustomNodeViewDesc, _NodeViewDesc2);
  1498. var _super7 = _createSuper(CustomNodeViewDesc);
  1499. function CustomNodeViewDesc(parent, node, outerDeco, innerDeco, dom, contentDOM, nodeDOM, spec, view, pos) {
  1500. var _this7;
  1501. _classCallCheck(this, CustomNodeViewDesc);
  1502. _this7 = _super7.call(this, parent, node, outerDeco, innerDeco, dom, contentDOM, nodeDOM, view, pos);
  1503. _this7.spec = spec;
  1504. return _this7;
  1505. }
  1506. _createClass(CustomNodeViewDesc, [{
  1507. key: "update",
  1508. value: function update(node, outerDeco, innerDeco, view) {
  1509. if (this.dirty == NODE_DIRTY) return false;
  1510. if (this.spec.update) {
  1511. var result = this.spec.update(node, outerDeco, innerDeco);
  1512. if (result) this.updateInner(node, outerDeco, innerDeco, view);
  1513. return result;
  1514. } else if (!this.contentDOM && !node.isLeaf) {
  1515. return false;
  1516. } else {
  1517. return _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "update", this).call(this, node, outerDeco, innerDeco, view);
  1518. }
  1519. }
  1520. }, {
  1521. key: "selectNode",
  1522. value: function selectNode() {
  1523. this.spec.selectNode ? this.spec.selectNode() : _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "selectNode", this).call(this);
  1524. }
  1525. }, {
  1526. key: "deselectNode",
  1527. value: function deselectNode() {
  1528. this.spec.deselectNode ? this.spec.deselectNode() : _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "deselectNode", this).call(this);
  1529. }
  1530. }, {
  1531. key: "setSelection",
  1532. value: function setSelection(anchor, head, root, force) {
  1533. this.spec.setSelection ? this.spec.setSelection(anchor, head, root) : _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "setSelection", this).call(this, anchor, head, root, force);
  1534. }
  1535. }, {
  1536. key: "destroy",
  1537. value: function destroy() {
  1538. if (this.spec.destroy) this.spec.destroy();
  1539. _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "destroy", this).call(this);
  1540. }
  1541. }, {
  1542. key: "stopEvent",
  1543. value: function stopEvent(event) {
  1544. return this.spec.stopEvent ? this.spec.stopEvent(event) : false;
  1545. }
  1546. }, {
  1547. key: "ignoreMutation",
  1548. value: function ignoreMutation(mutation) {
  1549. return this.spec.ignoreMutation ? this.spec.ignoreMutation(mutation) : _get(_getPrototypeOf(CustomNodeViewDesc.prototype), "ignoreMutation", this).call(this, mutation);
  1550. }
  1551. }]);
  1552. return CustomNodeViewDesc;
  1553. }(NodeViewDesc);
  1554. function renderDescs(parentDOM, descs, view) {
  1555. var dom = parentDOM.firstChild,
  1556. written = false;
  1557. for (var i = 0; i < descs.length; i++) {
  1558. var desc = descs[i],
  1559. childDOM = desc.dom;
  1560. if (childDOM.parentNode == parentDOM) {
  1561. while (childDOM != dom) {
  1562. dom = rm(dom);
  1563. written = true;
  1564. }
  1565. dom = dom.nextSibling;
  1566. } else {
  1567. written = true;
  1568. parentDOM.insertBefore(childDOM, dom);
  1569. }
  1570. if (desc instanceof MarkViewDesc) {
  1571. var pos = dom ? dom.previousSibling : parentDOM.lastChild;
  1572. renderDescs(desc.contentDOM, desc.children, view);
  1573. dom = pos ? pos.nextSibling : parentDOM.firstChild;
  1574. }
  1575. }
  1576. while (dom) {
  1577. dom = rm(dom);
  1578. written = true;
  1579. }
  1580. if (written && view.trackWrites == parentDOM) view.trackWrites = null;
  1581. }
  1582. var OuterDecoLevel = function OuterDecoLevel(nodeName) {
  1583. if (nodeName) this.nodeName = nodeName;
  1584. };
  1585. OuterDecoLevel.prototype = Object.create(null);
  1586. var noDeco = [new OuterDecoLevel()];
  1587. function computeOuterDeco(outerDeco, node, needsWrap) {
  1588. if (outerDeco.length == 0) return noDeco;
  1589. var top = needsWrap ? noDeco[0] : new OuterDecoLevel(),
  1590. result = [top];
  1591. for (var i = 0; i < outerDeco.length; i++) {
  1592. var attrs = outerDeco[i].type.attrs;
  1593. if (!attrs) continue;
  1594. if (attrs.nodeName) result.push(top = new OuterDecoLevel(attrs.nodeName));
  1595. for (var name in attrs) {
  1596. var val = attrs[name];
  1597. if (val == null) continue;
  1598. if (needsWrap && result.length == 1) result.push(top = new OuterDecoLevel(node.isInline ? "span" : "div"));
  1599. if (name == "class") top["class"] = (top["class"] ? top["class"] + " " : "") + val;else if (name == "style") top.style = (top.style ? top.style + ";" : "") + val;else if (name != "nodeName") top[name] = val;
  1600. }
  1601. }
  1602. return result;
  1603. }
  1604. function patchOuterDeco(outerDOM, nodeDOM, prevComputed, curComputed) {
  1605. if (prevComputed == noDeco && curComputed == noDeco) return nodeDOM;
  1606. var curDOM = nodeDOM;
  1607. for (var i = 0; i < curComputed.length; i++) {
  1608. var deco = curComputed[i],
  1609. prev = prevComputed[i];
  1610. if (i) {
  1611. var parent = void 0;
  1612. if (prev && prev.nodeName == deco.nodeName && curDOM != outerDOM && (parent = curDOM.parentNode) && parent.nodeName.toLowerCase() == deco.nodeName) {
  1613. curDOM = parent;
  1614. } else {
  1615. parent = document.createElement(deco.nodeName);
  1616. parent.pmIsDeco = true;
  1617. parent.appendChild(curDOM);
  1618. prev = noDeco[0];
  1619. curDOM = parent;
  1620. }
  1621. }
  1622. patchAttributes(curDOM, prev || noDeco[0], deco);
  1623. }
  1624. return curDOM;
  1625. }
  1626. function patchAttributes(dom, prev, cur) {
  1627. for (var name in prev) if (name != "class" && name != "style" && name != "nodeName" && !(name in cur)) dom.removeAttribute(name);
  1628. for (var _name in cur) if (_name != "class" && _name != "style" && _name != "nodeName" && cur[_name] != prev[_name]) dom.setAttribute(_name, cur[_name]);
  1629. if (prev["class"] != cur["class"]) {
  1630. var prevList = prev["class"] ? prev["class"].split(" ").filter(Boolean) : [];
  1631. var curList = cur["class"] ? cur["class"].split(" ").filter(Boolean) : [];
  1632. for (var i = 0; i < prevList.length; i++) if (curList.indexOf(prevList[i]) == -1) dom.classList.remove(prevList[i]);
  1633. for (var _i = 0; _i < curList.length; _i++) if (prevList.indexOf(curList[_i]) == -1) dom.classList.add(curList[_i]);
  1634. if (dom.classList.length == 0) dom.removeAttribute("class");
  1635. }
  1636. if (prev.style != cur.style) {
  1637. if (prev.style) {
  1638. var prop = /\s*([\w\-\xa1-\uffff]+)\s*:(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*'|\(.*?\)|[^;])*/g,
  1639. m;
  1640. while (m = prop.exec(prev.style)) dom.style.removeProperty(m[1]);
  1641. }
  1642. if (cur.style) dom.style.cssText += cur.style;
  1643. }
  1644. }
  1645. function applyOuterDeco(dom, deco, node) {
  1646. return patchOuterDeco(dom, dom, noDeco, computeOuterDeco(deco, node, dom.nodeType != 1));
  1647. }
  1648. function sameOuterDeco(a, b) {
  1649. if (a.length != b.length) return false;
  1650. for (var i = 0; i < a.length; i++) if (!a[i].type.eq(b[i].type)) return false;
  1651. return true;
  1652. }
  1653. function rm(dom) {
  1654. var next = dom.nextSibling;
  1655. dom.parentNode.removeChild(dom);
  1656. return next;
  1657. }
  1658. var ViewTreeUpdater = function () {
  1659. function ViewTreeUpdater(top, lock, view) {
  1660. _classCallCheck(this, ViewTreeUpdater);
  1661. this.lock = lock;
  1662. this.view = view;
  1663. this.index = 0;
  1664. this.stack = [];
  1665. this.changed = false;
  1666. this.top = top;
  1667. this.preMatch = preMatch(top.node.content, top);
  1668. }
  1669. _createClass(ViewTreeUpdater, [{
  1670. key: "destroyBetween",
  1671. value: function destroyBetween(start, end) {
  1672. if (start == end) return;
  1673. for (var i = start; i < end; i++) this.top.children[i].destroy();
  1674. this.top.children.splice(start, end - start);
  1675. this.changed = true;
  1676. }
  1677. }, {
  1678. key: "destroyRest",
  1679. value: function destroyRest() {
  1680. this.destroyBetween(this.index, this.top.children.length);
  1681. }
  1682. }, {
  1683. key: "syncToMarks",
  1684. value: function syncToMarks(marks, inline, view) {
  1685. var keep = 0,
  1686. depth = this.stack.length >> 1;
  1687. var maxKeep = Math.min(depth, marks.length);
  1688. while (keep < maxKeep && (keep == depth - 1 ? this.top : this.stack[keep + 1 << 1]).matchesMark(marks[keep]) && marks[keep].type.spec.spanning !== false) keep++;
  1689. while (keep < depth) {
  1690. this.destroyRest();
  1691. this.top.dirty = NOT_DIRTY;
  1692. this.index = this.stack.pop();
  1693. this.top = this.stack.pop();
  1694. depth--;
  1695. }
  1696. while (depth < marks.length) {
  1697. this.stack.push(this.top, this.index + 1);
  1698. var found = -1;
  1699. for (var i = this.index; i < Math.min(this.index + 3, this.top.children.length); i++) {
  1700. var next = this.top.children[i];
  1701. if (next.matchesMark(marks[depth]) && !this.isLocked(next.dom)) {
  1702. found = i;
  1703. break;
  1704. }
  1705. }
  1706. if (found > -1) {
  1707. if (found > this.index) {
  1708. this.changed = true;
  1709. this.destroyBetween(this.index, found);
  1710. }
  1711. this.top = this.top.children[this.index];
  1712. } else {
  1713. var markDesc = MarkViewDesc.create(this.top, marks[depth], inline, view);
  1714. this.top.children.splice(this.index, 0, markDesc);
  1715. this.top = markDesc;
  1716. this.changed = true;
  1717. }
  1718. this.index = 0;
  1719. depth++;
  1720. }
  1721. }
  1722. }, {
  1723. key: "findNodeMatch",
  1724. value: function findNodeMatch(node, outerDeco, innerDeco, index) {
  1725. var found = -1,
  1726. targetDesc;
  1727. if (index >= this.preMatch.index && (targetDesc = this.preMatch.matches[index - this.preMatch.index]).parent == this.top && targetDesc.matchesNode(node, outerDeco, innerDeco)) {
  1728. found = this.top.children.indexOf(targetDesc, this.index);
  1729. } else {
  1730. for (var i = this.index, e = Math.min(this.top.children.length, i + 5); i < e; i++) {
  1731. var child = this.top.children[i];
  1732. if (child.matchesNode(node, outerDeco, innerDeco) && !this.preMatch.matched.has(child)) {
  1733. found = i;
  1734. break;
  1735. }
  1736. }
  1737. }
  1738. if (found < 0) return false;
  1739. this.destroyBetween(this.index, found);
  1740. this.index++;
  1741. return true;
  1742. }
  1743. }, {
  1744. key: "updateNodeAt",
  1745. value: function updateNodeAt(node, outerDeco, innerDeco, index, view) {
  1746. var child = this.top.children[index];
  1747. if (child.dirty == NODE_DIRTY && child.dom == child.contentDOM) child.dirty = CONTENT_DIRTY;
  1748. if (!child.update(node, outerDeco, innerDeco, view)) return false;
  1749. this.destroyBetween(this.index, index);
  1750. this.index++;
  1751. return true;
  1752. }
  1753. }, {
  1754. key: "findIndexWithChild",
  1755. value: function findIndexWithChild(domNode) {
  1756. for (;;) {
  1757. var parent = domNode.parentNode;
  1758. if (!parent) return -1;
  1759. if (parent == this.top.contentDOM) {
  1760. var desc = domNode.pmViewDesc;
  1761. if (desc) for (var i = this.index; i < this.top.children.length; i++) {
  1762. if (this.top.children[i] == desc) return i;
  1763. }
  1764. return -1;
  1765. }
  1766. domNode = parent;
  1767. }
  1768. }
  1769. }, {
  1770. key: "updateNextNode",
  1771. value: function updateNextNode(node, outerDeco, innerDeco, view, index, pos) {
  1772. for (var i = this.index; i < this.top.children.length; i++) {
  1773. var next = this.top.children[i];
  1774. if (next instanceof NodeViewDesc) {
  1775. var _preMatch = this.preMatch.matched.get(next);
  1776. if (_preMatch != null && _preMatch != index) return false;
  1777. var nextDOM = next.dom,
  1778. updated = void 0;
  1779. var locked = this.isLocked(nextDOM) && !(node.isText && next.node && next.node.isText && next.nodeDOM.nodeValue == node.text && next.dirty != NODE_DIRTY && sameOuterDeco(outerDeco, next.outerDeco));
  1780. if (!locked && next.update(node, outerDeco, innerDeco, view)) {
  1781. this.destroyBetween(this.index, i);
  1782. if (next.dom != nextDOM) this.changed = true;
  1783. this.index++;
  1784. return true;
  1785. } else if (!locked && (updated = this.recreateWrapper(next, node, outerDeco, innerDeco, view, pos))) {
  1786. this.top.children[this.index] = updated;
  1787. if (updated.contentDOM) {
  1788. updated.dirty = CONTENT_DIRTY;
  1789. updated.updateChildren(view, pos + 1);
  1790. updated.dirty = NOT_DIRTY;
  1791. }
  1792. this.changed = true;
  1793. this.index++;
  1794. return true;
  1795. }
  1796. break;
  1797. }
  1798. }
  1799. return false;
  1800. }
  1801. }, {
  1802. key: "recreateWrapper",
  1803. value: function recreateWrapper(next, node, outerDeco, innerDeco, view, pos) {
  1804. if (next.dirty || node.isAtom || !next.children.length || !next.node.content.eq(node.content)) return null;
  1805. var wrapper = NodeViewDesc.create(this.top, node, outerDeco, innerDeco, view, pos);
  1806. if (wrapper.contentDOM) {
  1807. wrapper.children = next.children;
  1808. next.children = [];
  1809. var _iterator = _createForOfIteratorHelper(wrapper.children),
  1810. _step;
  1811. try {
  1812. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  1813. var ch = _step.value;
  1814. ch.parent = wrapper;
  1815. }
  1816. } catch (err) {
  1817. _iterator.e(err);
  1818. } finally {
  1819. _iterator.f();
  1820. }
  1821. }
  1822. next.destroy();
  1823. return wrapper;
  1824. }
  1825. }, {
  1826. key: "addNode",
  1827. value: function addNode(node, outerDeco, innerDeco, view, pos) {
  1828. var desc = NodeViewDesc.create(this.top, node, outerDeco, innerDeco, view, pos);
  1829. if (desc.contentDOM) desc.updateChildren(view, pos + 1);
  1830. this.top.children.splice(this.index++, 0, desc);
  1831. this.changed = true;
  1832. }
  1833. }, {
  1834. key: "placeWidget",
  1835. value: function placeWidget(widget, view, pos) {
  1836. var next = this.index < this.top.children.length ? this.top.children[this.index] : null;
  1837. if (next && next.matchesWidget(widget) && (widget == next.widget || !next.widget.type.toDOM.parentNode)) {
  1838. this.index++;
  1839. } else {
  1840. var desc = new WidgetViewDesc(this.top, widget, view, pos);
  1841. this.top.children.splice(this.index++, 0, desc);
  1842. this.changed = true;
  1843. }
  1844. }
  1845. }, {
  1846. key: "addTextblockHacks",
  1847. value: function addTextblockHacks() {
  1848. var lastChild = this.top.children[this.index - 1],
  1849. parent = this.top;
  1850. while (lastChild instanceof MarkViewDesc) {
  1851. parent = lastChild;
  1852. lastChild = parent.children[parent.children.length - 1];
  1853. }
  1854. if (!lastChild || !(lastChild instanceof TextViewDesc) || /\n$/.test(lastChild.node.text) || this.view.requiresGeckoHackNode && /\s$/.test(lastChild.node.text)) {
  1855. if ((safari || chrome) && lastChild && lastChild.dom.contentEditable == "false") this.addHackNode("IMG", parent);
  1856. this.addHackNode("BR", this.top);
  1857. }
  1858. }
  1859. }, {
  1860. key: "addHackNode",
  1861. value: function addHackNode(nodeName, parent) {
  1862. if (parent == this.top && this.index < parent.children.length && parent.children[this.index].matchesHack(nodeName)) {
  1863. this.index++;
  1864. } else {
  1865. var dom = document.createElement(nodeName);
  1866. if (nodeName == "IMG") {
  1867. dom.className = "ProseMirror-separator";
  1868. dom.alt = "";
  1869. }
  1870. if (nodeName == "BR") dom.className = "ProseMirror-trailingBreak";
  1871. var hack = new TrailingHackViewDesc(this.top, [], dom, null);
  1872. if (parent != this.top) parent.children.push(hack);else parent.children.splice(this.index++, 0, hack);
  1873. this.changed = true;
  1874. }
  1875. }
  1876. }, {
  1877. key: "isLocked",
  1878. value: function isLocked(node) {
  1879. return this.lock && (node == this.lock || node.nodeType == 1 && node.contains(this.lock.parentNode));
  1880. }
  1881. }]);
  1882. return ViewTreeUpdater;
  1883. }();
  1884. function preMatch(frag, parentDesc) {
  1885. var curDesc = parentDesc,
  1886. descI = curDesc.children.length;
  1887. var fI = frag.childCount,
  1888. matched = new Map(),
  1889. matches = [];
  1890. outer: while (fI > 0) {
  1891. var desc = void 0;
  1892. for (;;) {
  1893. if (descI) {
  1894. var next = curDesc.children[descI - 1];
  1895. if (next instanceof MarkViewDesc) {
  1896. curDesc = next;
  1897. descI = next.children.length;
  1898. } else {
  1899. desc = next;
  1900. descI--;
  1901. break;
  1902. }
  1903. } else if (curDesc == parentDesc) {
  1904. break outer;
  1905. } else {
  1906. descI = curDesc.parent.children.indexOf(curDesc);
  1907. curDesc = curDesc.parent;
  1908. }
  1909. }
  1910. var node = desc.node;
  1911. if (!node) continue;
  1912. if (node != frag.child(fI - 1)) break;
  1913. --fI;
  1914. matched.set(desc, fI);
  1915. matches.push(desc);
  1916. }
  1917. return {
  1918. index: fI,
  1919. matched: matched,
  1920. matches: matches.reverse()
  1921. };
  1922. }
  1923. function compareSide(a, b) {
  1924. return a.type.side - b.type.side;
  1925. }
  1926. function iterDeco(parent, deco, onWidget, onNode) {
  1927. var locals = deco.locals(parent),
  1928. offset = 0;
  1929. if (locals.length == 0) {
  1930. for (var i = 0; i < parent.childCount; i++) {
  1931. var child = parent.child(i);
  1932. onNode(child, locals, deco.forChild(offset, child), i);
  1933. offset += child.nodeSize;
  1934. }
  1935. return;
  1936. }
  1937. var decoIndex = 0,
  1938. active = [],
  1939. restNode = null;
  1940. for (var parentIndex = 0;;) {
  1941. var widget = void 0,
  1942. widgets = void 0;
  1943. while (decoIndex < locals.length && locals[decoIndex].to == offset) {
  1944. var next = locals[decoIndex++];
  1945. if (next.widget) {
  1946. if (!widget) widget = next;else (widgets || (widgets = [widget])).push(next);
  1947. }
  1948. }
  1949. if (widget) {
  1950. if (widgets) {
  1951. widgets.sort(compareSide);
  1952. for (var _i2 = 0; _i2 < widgets.length; _i2++) onWidget(widgets[_i2], parentIndex, !!restNode);
  1953. } else {
  1954. onWidget(widget, parentIndex, !!restNode);
  1955. }
  1956. }
  1957. var _child = void 0,
  1958. index = void 0;
  1959. if (restNode) {
  1960. index = -1;
  1961. _child = restNode;
  1962. restNode = null;
  1963. } else if (parentIndex < parent.childCount) {
  1964. index = parentIndex;
  1965. _child = parent.child(parentIndex++);
  1966. } else {
  1967. break;
  1968. }
  1969. for (var _i3 = 0; _i3 < active.length; _i3++) if (active[_i3].to <= offset) active.splice(_i3--, 1);
  1970. while (decoIndex < locals.length && locals[decoIndex].from <= offset && locals[decoIndex].to > offset) active.push(locals[decoIndex++]);
  1971. var end = offset + _child.nodeSize;
  1972. if (_child.isText) {
  1973. var cutAt = end;
  1974. if (decoIndex < locals.length && locals[decoIndex].from < cutAt) cutAt = locals[decoIndex].from;
  1975. for (var _i4 = 0; _i4 < active.length; _i4++) if (active[_i4].to < cutAt) cutAt = active[_i4].to;
  1976. if (cutAt < end) {
  1977. restNode = _child.cut(cutAt - offset);
  1978. _child = _child.cut(0, cutAt - offset);
  1979. end = cutAt;
  1980. index = -1;
  1981. }
  1982. } else {
  1983. while (decoIndex < locals.length && locals[decoIndex].to < end) decoIndex++;
  1984. }
  1985. var outerDeco = _child.isInline && !_child.isLeaf ? active.filter(function (d) {
  1986. return !d.inline;
  1987. }) : active.slice();
  1988. onNode(_child, outerDeco, deco.forChild(offset, _child), index);
  1989. offset = end;
  1990. }
  1991. }
  1992. function iosHacks(dom) {
  1993. if (dom.nodeName == "UL" || dom.nodeName == "OL") {
  1994. var oldCSS = dom.style.cssText;
  1995. dom.style.cssText = oldCSS + "; list-style: square !important";
  1996. window.getComputedStyle(dom).listStyle;
  1997. dom.style.cssText = oldCSS;
  1998. }
  1999. }
  2000. function nearbyTextNode(node, offset) {
  2001. for (;;) {
  2002. if (node.nodeType == 3) return node;
  2003. if (node.nodeType == 1 && offset > 0) {
  2004. if (node.childNodes.length > offset && node.childNodes[offset].nodeType == 3) return node.childNodes[offset];
  2005. node = node.childNodes[offset - 1];
  2006. offset = nodeSize(node);
  2007. } else if (node.nodeType == 1 && offset < node.childNodes.length) {
  2008. node = node.childNodes[offset];
  2009. offset = 0;
  2010. } else {
  2011. return null;
  2012. }
  2013. }
  2014. }
  2015. function findTextInFragment(frag, text, from, to) {
  2016. for (var i = 0, pos = 0; i < frag.childCount && pos <= to;) {
  2017. var child = frag.child(i++),
  2018. childStart = pos;
  2019. pos += child.nodeSize;
  2020. if (!child.isText) continue;
  2021. var str = child.text;
  2022. while (i < frag.childCount) {
  2023. var next = frag.child(i++);
  2024. pos += next.nodeSize;
  2025. if (!next.isText) break;
  2026. str += next.text;
  2027. }
  2028. if (pos >= from) {
  2029. if (pos >= to && str.slice(to - text.length - childStart, to - childStart) == text) return to - text.length;
  2030. var found = childStart < to ? str.lastIndexOf(text, to - childStart - 1) : -1;
  2031. if (found >= 0 && found + text.length + childStart >= from) return childStart + found;
  2032. if (from == to && str.length >= to + text.length - childStart && str.slice(to - childStart, to - childStart + text.length) == text) return to;
  2033. }
  2034. }
  2035. return -1;
  2036. }
  2037. function replaceNodes(nodes, from, to, view, replacement) {
  2038. var result = [];
  2039. for (var i = 0, off = 0; i < nodes.length; i++) {
  2040. var child = nodes[i],
  2041. start = off,
  2042. end = off += child.size;
  2043. if (start >= to || end <= from) {
  2044. result.push(child);
  2045. } else {
  2046. if (start < from) result.push(child.slice(0, from - start, view));
  2047. if (replacement) {
  2048. result.push(replacement);
  2049. replacement = undefined;
  2050. }
  2051. if (end > to) result.push(child.slice(to - start, child.size, view));
  2052. }
  2053. }
  2054. return result;
  2055. }
  2056. function selectionFromDOM(view) {
  2057. var origin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  2058. var domSel = view.domSelectionRange(),
  2059. doc = view.state.doc;
  2060. if (!domSel.focusNode) return null;
  2061. var nearestDesc = view.docView.nearestDesc(domSel.focusNode),
  2062. inWidget = nearestDesc && nearestDesc.size == 0;
  2063. var head = view.docView.posFromDOM(domSel.focusNode, domSel.focusOffset, 1);
  2064. if (head < 0) return null;
  2065. var $head = doc.resolve(head),
  2066. $anchor,
  2067. selection;
  2068. if (selectionCollapsed(domSel)) {
  2069. $anchor = $head;
  2070. while (nearestDesc && !nearestDesc.node) nearestDesc = nearestDesc.parent;
  2071. var nearestDescNode = nearestDesc.node;
  2072. if (nearestDesc && nearestDescNode.isAtom && prosemirrorState.NodeSelection.isSelectable(nearestDescNode) && nearestDesc.parent && !(nearestDescNode.isInline && isOnEdge(domSel.focusNode, domSel.focusOffset, nearestDesc.dom))) {
  2073. var pos = nearestDesc.posBefore;
  2074. selection = new prosemirrorState.NodeSelection(head == pos ? $head : doc.resolve(pos));
  2075. }
  2076. } else {
  2077. var anchor = view.docView.posFromDOM(domSel.anchorNode, domSel.anchorOffset, 1);
  2078. if (anchor < 0) return null;
  2079. $anchor = doc.resolve(anchor);
  2080. }
  2081. if (!selection) {
  2082. var bias = origin == "pointer" || view.state.selection.head < $head.pos && !inWidget ? 1 : -1;
  2083. selection = selectionBetween(view, $anchor, $head, bias);
  2084. }
  2085. return selection;
  2086. }
  2087. function editorOwnsSelection(view) {
  2088. return view.editable ? view.hasFocus() : hasSelection(view) && document.activeElement && document.activeElement.contains(view.dom);
  2089. }
  2090. function selectionToDOM(view) {
  2091. var force = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  2092. var sel = view.state.selection;
  2093. syncNodeSelection(view, sel);
  2094. if (!editorOwnsSelection(view)) return;
  2095. if (!force && view.input.mouseDown && view.input.mouseDown.allowDefault && chrome) {
  2096. var domSel = view.domSelectionRange(),
  2097. curSel = view.domObserver.currentSelection;
  2098. if (domSel.anchorNode && curSel.anchorNode && isEquivalentPosition(domSel.anchorNode, domSel.anchorOffset, curSel.anchorNode, curSel.anchorOffset)) {
  2099. view.input.mouseDown.delayedSelectionSync = true;
  2100. view.domObserver.setCurSelection();
  2101. return;
  2102. }
  2103. }
  2104. view.domObserver.disconnectSelection();
  2105. if (view.cursorWrapper) {
  2106. selectCursorWrapper(view);
  2107. } else {
  2108. var anchor = sel.anchor,
  2109. head = sel.head,
  2110. resetEditableFrom,
  2111. resetEditableTo;
  2112. if (brokenSelectBetweenUneditable && !(sel instanceof prosemirrorState.TextSelection)) {
  2113. if (!sel.$from.parent.inlineContent) resetEditableFrom = temporarilyEditableNear(view, sel.from);
  2114. if (!sel.empty && !sel.$from.parent.inlineContent) resetEditableTo = temporarilyEditableNear(view, sel.to);
  2115. }
  2116. view.docView.setSelection(anchor, head, view.root, force);
  2117. if (brokenSelectBetweenUneditable) {
  2118. if (resetEditableFrom) resetEditable(resetEditableFrom);
  2119. if (resetEditableTo) resetEditable(resetEditableTo);
  2120. }
  2121. if (sel.visible) {
  2122. view.dom.classList.remove("ProseMirror-hideselection");
  2123. } else {
  2124. view.dom.classList.add("ProseMirror-hideselection");
  2125. if ("onselectionchange" in document) removeClassOnSelectionChange(view);
  2126. }
  2127. }
  2128. view.domObserver.setCurSelection();
  2129. view.domObserver.connectSelection();
  2130. }
  2131. var brokenSelectBetweenUneditable = safari || chrome && chrome_version < 63;
  2132. function temporarilyEditableNear(view, pos) {
  2133. var _view$docView$domFrom3 = view.docView.domFromPos(pos, 0),
  2134. node = _view$docView$domFrom3.node,
  2135. offset = _view$docView$domFrom3.offset;
  2136. var after = offset < node.childNodes.length ? node.childNodes[offset] : null;
  2137. var before = offset ? node.childNodes[offset - 1] : null;
  2138. if (safari && after && after.contentEditable == "false") return setEditable(after);
  2139. if ((!after || after.contentEditable == "false") && (!before || before.contentEditable == "false")) {
  2140. if (after) return setEditable(after);else if (before) return setEditable(before);
  2141. }
  2142. }
  2143. function setEditable(element) {
  2144. element.contentEditable = "true";
  2145. if (safari && element.draggable) {
  2146. element.draggable = false;
  2147. element.wasDraggable = true;
  2148. }
  2149. return element;
  2150. }
  2151. function resetEditable(element) {
  2152. element.contentEditable = "false";
  2153. if (element.wasDraggable) {
  2154. element.draggable = true;
  2155. element.wasDraggable = null;
  2156. }
  2157. }
  2158. function removeClassOnSelectionChange(view) {
  2159. var doc = view.dom.ownerDocument;
  2160. doc.removeEventListener("selectionchange", view.input.hideSelectionGuard);
  2161. var domSel = view.domSelectionRange();
  2162. var node = domSel.anchorNode,
  2163. offset = domSel.anchorOffset;
  2164. doc.addEventListener("selectionchange", view.input.hideSelectionGuard = function () {
  2165. if (domSel.anchorNode != node || domSel.anchorOffset != offset) {
  2166. doc.removeEventListener("selectionchange", view.input.hideSelectionGuard);
  2167. setTimeout(function () {
  2168. if (!editorOwnsSelection(view) || view.state.selection.visible) view.dom.classList.remove("ProseMirror-hideselection");
  2169. }, 20);
  2170. }
  2171. });
  2172. }
  2173. function selectCursorWrapper(view) {
  2174. var domSel = view.domSelection(),
  2175. range = document.createRange();
  2176. var node = view.cursorWrapper.dom,
  2177. img = node.nodeName == "IMG";
  2178. if (img) range.setEnd(node.parentNode, domIndex(node) + 1);else range.setEnd(node, 0);
  2179. range.collapse(false);
  2180. domSel.removeAllRanges();
  2181. domSel.addRange(range);
  2182. if (!img && !view.state.selection.visible && ie && ie_version <= 11) {
  2183. node.disabled = true;
  2184. node.disabled = false;
  2185. }
  2186. }
  2187. function syncNodeSelection(view, sel) {
  2188. if (sel instanceof prosemirrorState.NodeSelection) {
  2189. var desc = view.docView.descAt(sel.from);
  2190. if (desc != view.lastSelectedViewDesc) {
  2191. clearNodeSelection(view);
  2192. if (desc) desc.selectNode();
  2193. view.lastSelectedViewDesc = desc;
  2194. }
  2195. } else {
  2196. clearNodeSelection(view);
  2197. }
  2198. }
  2199. function clearNodeSelection(view) {
  2200. if (view.lastSelectedViewDesc) {
  2201. if (view.lastSelectedViewDesc.parent) view.lastSelectedViewDesc.deselectNode();
  2202. view.lastSelectedViewDesc = undefined;
  2203. }
  2204. }
  2205. function selectionBetween(view, $anchor, $head, bias) {
  2206. return view.someProp("createSelectionBetween", function (f) {
  2207. return f(view, $anchor, $head);
  2208. }) || prosemirrorState.TextSelection.between($anchor, $head, bias);
  2209. }
  2210. function hasFocusAndSelection(view) {
  2211. if (view.editable && !view.hasFocus()) return false;
  2212. return hasSelection(view);
  2213. }
  2214. function hasSelection(view) {
  2215. var sel = view.domSelectionRange();
  2216. if (!sel.anchorNode) return false;
  2217. try {
  2218. return view.dom.contains(sel.anchorNode.nodeType == 3 ? sel.anchorNode.parentNode : sel.anchorNode) && (view.editable || view.dom.contains(sel.focusNode.nodeType == 3 ? sel.focusNode.parentNode : sel.focusNode));
  2219. } catch (_) {
  2220. return false;
  2221. }
  2222. }
  2223. function anchorInRightPlace(view) {
  2224. var anchorDOM = view.docView.domFromPos(view.state.selection.anchor, 0);
  2225. var domSel = view.domSelectionRange();
  2226. return isEquivalentPosition(anchorDOM.node, anchorDOM.offset, domSel.anchorNode, domSel.anchorOffset);
  2227. }
  2228. function moveSelectionBlock(state, dir) {
  2229. var _state$selection = state.selection,
  2230. $anchor = _state$selection.$anchor,
  2231. $head = _state$selection.$head;
  2232. var $side = dir > 0 ? $anchor.max($head) : $anchor.min($head);
  2233. var $start = !$side.parent.inlineContent ? $side : $side.depth ? state.doc.resolve(dir > 0 ? $side.after() : $side.before()) : null;
  2234. return $start && prosemirrorState.Selection.findFrom($start, dir);
  2235. }
  2236. function apply(view, sel) {
  2237. view.dispatch(view.state.tr.setSelection(sel).scrollIntoView());
  2238. return true;
  2239. }
  2240. function selectHorizontally(view, dir, mods) {
  2241. var sel = view.state.selection;
  2242. if (sel instanceof prosemirrorState.TextSelection) {
  2243. if (mods.indexOf("s") > -1) {
  2244. var $head = sel.$head,
  2245. node = $head.textOffset ? null : dir < 0 ? $head.nodeBefore : $head.nodeAfter;
  2246. if (!node || node.isText || !node.isLeaf) return false;
  2247. var $newHead = view.state.doc.resolve($head.pos + node.nodeSize * (dir < 0 ? -1 : 1));
  2248. return apply(view, new prosemirrorState.TextSelection(sel.$anchor, $newHead));
  2249. } else if (!sel.empty) {
  2250. return false;
  2251. } else if (view.endOfTextblock(dir > 0 ? "forward" : "backward")) {
  2252. var next = moveSelectionBlock(view.state, dir);
  2253. if (next && next instanceof prosemirrorState.NodeSelection) return apply(view, next);
  2254. return false;
  2255. } else if (!(mac && mods.indexOf("m") > -1)) {
  2256. var _$head = sel.$head,
  2257. _node = _$head.textOffset ? null : dir < 0 ? _$head.nodeBefore : _$head.nodeAfter,
  2258. desc;
  2259. if (!_node || _node.isText) return false;
  2260. var nodePos = dir < 0 ? _$head.pos - _node.nodeSize : _$head.pos;
  2261. if (!(_node.isAtom || (desc = view.docView.descAt(nodePos)) && !desc.contentDOM)) return false;
  2262. if (prosemirrorState.NodeSelection.isSelectable(_node)) {
  2263. return apply(view, new prosemirrorState.NodeSelection(dir < 0 ? view.state.doc.resolve(_$head.pos - _node.nodeSize) : _$head));
  2264. } else if (webkit) {
  2265. return apply(view, new prosemirrorState.TextSelection(view.state.doc.resolve(dir < 0 ? nodePos : nodePos + _node.nodeSize)));
  2266. } else {
  2267. return false;
  2268. }
  2269. }
  2270. } else if (sel instanceof prosemirrorState.NodeSelection && sel.node.isInline) {
  2271. return apply(view, new prosemirrorState.TextSelection(dir > 0 ? sel.$to : sel.$from));
  2272. } else {
  2273. var _next = moveSelectionBlock(view.state, dir);
  2274. if (_next) return apply(view, _next);
  2275. return false;
  2276. }
  2277. }
  2278. function nodeLen(node) {
  2279. return node.nodeType == 3 ? node.nodeValue.length : node.childNodes.length;
  2280. }
  2281. function isIgnorable(dom, dir) {
  2282. var desc = dom.pmViewDesc;
  2283. return desc && desc.size == 0 && (dir < 0 || dom.nextSibling || dom.nodeName != "BR");
  2284. }
  2285. function skipIgnoredNodes(view, dir) {
  2286. return dir < 0 ? skipIgnoredNodesBefore(view) : skipIgnoredNodesAfter(view);
  2287. }
  2288. function skipIgnoredNodesBefore(view) {
  2289. var sel = view.domSelectionRange();
  2290. var node = sel.focusNode,
  2291. offset = sel.focusOffset;
  2292. if (!node) return;
  2293. var moveNode,
  2294. moveOffset,
  2295. force = false;
  2296. if (gecko && node.nodeType == 1 && offset < nodeLen(node) && isIgnorable(node.childNodes[offset], -1)) force = true;
  2297. for (;;) {
  2298. if (offset > 0) {
  2299. if (node.nodeType != 1) {
  2300. break;
  2301. } else {
  2302. var before = node.childNodes[offset - 1];
  2303. if (isIgnorable(before, -1)) {
  2304. moveNode = node;
  2305. moveOffset = --offset;
  2306. } else if (before.nodeType == 3) {
  2307. node = before;
  2308. offset = node.nodeValue.length;
  2309. } else break;
  2310. }
  2311. } else if (isBlockNode(node)) {
  2312. break;
  2313. } else {
  2314. var prev = node.previousSibling;
  2315. while (prev && isIgnorable(prev, -1)) {
  2316. moveNode = node.parentNode;
  2317. moveOffset = domIndex(prev);
  2318. prev = prev.previousSibling;
  2319. }
  2320. if (!prev) {
  2321. node = node.parentNode;
  2322. if (node == view.dom) break;
  2323. offset = 0;
  2324. } else {
  2325. node = prev;
  2326. offset = nodeLen(node);
  2327. }
  2328. }
  2329. }
  2330. if (force) setSelFocus(view, node, offset);else if (moveNode) setSelFocus(view, moveNode, moveOffset);
  2331. }
  2332. function skipIgnoredNodesAfter(view) {
  2333. var sel = view.domSelectionRange();
  2334. var node = sel.focusNode,
  2335. offset = sel.focusOffset;
  2336. if (!node) return;
  2337. var len = nodeLen(node);
  2338. var moveNode, moveOffset;
  2339. for (;;) {
  2340. if (offset < len) {
  2341. if (node.nodeType != 1) break;
  2342. var after = node.childNodes[offset];
  2343. if (isIgnorable(after, 1)) {
  2344. moveNode = node;
  2345. moveOffset = ++offset;
  2346. } else break;
  2347. } else if (isBlockNode(node)) {
  2348. break;
  2349. } else {
  2350. var next = node.nextSibling;
  2351. while (next && isIgnorable(next, 1)) {
  2352. moveNode = next.parentNode;
  2353. moveOffset = domIndex(next) + 1;
  2354. next = next.nextSibling;
  2355. }
  2356. if (!next) {
  2357. node = node.parentNode;
  2358. if (node == view.dom) break;
  2359. offset = len = 0;
  2360. } else {
  2361. node = next;
  2362. offset = 0;
  2363. len = nodeLen(node);
  2364. }
  2365. }
  2366. }
  2367. if (moveNode) setSelFocus(view, moveNode, moveOffset);
  2368. }
  2369. function isBlockNode(dom) {
  2370. var desc = dom.pmViewDesc;
  2371. return desc && desc.node && desc.node.isBlock;
  2372. }
  2373. function textNodeAfter(node, offset) {
  2374. while (node && offset == node.childNodes.length && !hasBlockDesc(node)) {
  2375. offset = domIndex(node) + 1;
  2376. node = node.parentNode;
  2377. }
  2378. while (node && offset < node.childNodes.length) {
  2379. var next = node.childNodes[offset];
  2380. if (next.nodeType == 3) return next;
  2381. if (next.nodeType == 1 && next.contentEditable == "false") break;
  2382. node = next;
  2383. offset = 0;
  2384. }
  2385. }
  2386. function textNodeBefore(node, offset) {
  2387. while (node && !offset && !hasBlockDesc(node)) {
  2388. offset = domIndex(node);
  2389. node = node.parentNode;
  2390. }
  2391. while (node && offset) {
  2392. var next = node.childNodes[offset - 1];
  2393. if (next.nodeType == 3) return next;
  2394. if (next.nodeType == 1 && next.contentEditable == "false") break;
  2395. node = next;
  2396. offset = node.childNodes.length;
  2397. }
  2398. }
  2399. function setSelFocus(view, node, offset) {
  2400. if (node.nodeType != 3) {
  2401. var before, after;
  2402. if (after = textNodeAfter(node, offset)) {
  2403. node = after;
  2404. offset = 0;
  2405. } else if (before = textNodeBefore(node, offset)) {
  2406. node = before;
  2407. offset = before.nodeValue.length;
  2408. }
  2409. }
  2410. var sel = view.domSelection();
  2411. if (selectionCollapsed(sel)) {
  2412. var range = document.createRange();
  2413. range.setEnd(node, offset);
  2414. range.setStart(node, offset);
  2415. sel.removeAllRanges();
  2416. sel.addRange(range);
  2417. } else if (sel.extend) {
  2418. sel.extend(node, offset);
  2419. }
  2420. view.domObserver.setCurSelection();
  2421. var state = view.state;
  2422. setTimeout(function () {
  2423. if (view.state == state) selectionToDOM(view);
  2424. }, 50);
  2425. }
  2426. function findDirection(view, pos) {
  2427. var $pos = view.state.doc.resolve(pos);
  2428. if (!(chrome || windows) && $pos.parent.inlineContent) {
  2429. var coords = view.coordsAtPos(pos);
  2430. if (pos > $pos.start()) {
  2431. var before = view.coordsAtPos(pos - 1);
  2432. var mid = (before.top + before.bottom) / 2;
  2433. if (mid > coords.top && mid < coords.bottom && Math.abs(before.left - coords.left) > 1) return before.left < coords.left ? "ltr" : "rtl";
  2434. }
  2435. if (pos < $pos.end()) {
  2436. var after = view.coordsAtPos(pos + 1);
  2437. var _mid = (after.top + after.bottom) / 2;
  2438. if (_mid > coords.top && _mid < coords.bottom && Math.abs(after.left - coords.left) > 1) return after.left > coords.left ? "ltr" : "rtl";
  2439. }
  2440. }
  2441. var computed = getComputedStyle(view.dom).direction;
  2442. return computed == "rtl" ? "rtl" : "ltr";
  2443. }
  2444. function selectVertically(view, dir, mods) {
  2445. var sel = view.state.selection;
  2446. if (sel instanceof prosemirrorState.TextSelection && !sel.empty || mods.indexOf("s") > -1) return false;
  2447. if (mac && mods.indexOf("m") > -1) return false;
  2448. var $from = sel.$from,
  2449. $to = sel.$to;
  2450. if (!$from.parent.inlineContent || view.endOfTextblock(dir < 0 ? "up" : "down")) {
  2451. var next = moveSelectionBlock(view.state, dir);
  2452. if (next && next instanceof prosemirrorState.NodeSelection) return apply(view, next);
  2453. }
  2454. if (!$from.parent.inlineContent) {
  2455. var side = dir < 0 ? $from : $to;
  2456. var beyond = sel instanceof prosemirrorState.AllSelection ? prosemirrorState.Selection.near(side, dir) : prosemirrorState.Selection.findFrom(side, dir);
  2457. return beyond ? apply(view, beyond) : false;
  2458. }
  2459. return false;
  2460. }
  2461. function stopNativeHorizontalDelete(view, dir) {
  2462. if (!(view.state.selection instanceof prosemirrorState.TextSelection)) return true;
  2463. var _view$state$selection2 = view.state.selection,
  2464. $head = _view$state$selection2.$head,
  2465. $anchor = _view$state$selection2.$anchor,
  2466. empty = _view$state$selection2.empty;
  2467. if (!$head.sameParent($anchor)) return true;
  2468. if (!empty) return false;
  2469. if (view.endOfTextblock(dir > 0 ? "forward" : "backward")) return true;
  2470. var nextNode = !$head.textOffset && (dir < 0 ? $head.nodeBefore : $head.nodeAfter);
  2471. if (nextNode && !nextNode.isText) {
  2472. var tr = view.state.tr;
  2473. if (dir < 0) tr["delete"]($head.pos - nextNode.nodeSize, $head.pos);else tr["delete"]($head.pos, $head.pos + nextNode.nodeSize);
  2474. view.dispatch(tr);
  2475. return true;
  2476. }
  2477. return false;
  2478. }
  2479. function switchEditable(view, node, state) {
  2480. view.domObserver.stop();
  2481. node.contentEditable = state;
  2482. view.domObserver.start();
  2483. }
  2484. function safariDownArrowBug(view) {
  2485. if (!safari || view.state.selection.$head.parentOffset > 0) return false;
  2486. var _view$domSelectionRan3 = view.domSelectionRange(),
  2487. focusNode = _view$domSelectionRan3.focusNode,
  2488. focusOffset = _view$domSelectionRan3.focusOffset;
  2489. if (focusNode && focusNode.nodeType == 1 && focusOffset == 0 && focusNode.firstChild && focusNode.firstChild.contentEditable == "false") {
  2490. var child = focusNode.firstChild;
  2491. switchEditable(view, child, "true");
  2492. setTimeout(function () {
  2493. return switchEditable(view, child, "false");
  2494. }, 20);
  2495. }
  2496. return false;
  2497. }
  2498. function getMods(event) {
  2499. var result = "";
  2500. if (event.ctrlKey) result += "c";
  2501. if (event.metaKey) result += "m";
  2502. if (event.altKey) result += "a";
  2503. if (event.shiftKey) result += "s";
  2504. return result;
  2505. }
  2506. function captureKeyDown(view, event) {
  2507. var code = event.keyCode,
  2508. mods = getMods(event);
  2509. if (code == 8 || mac && code == 72 && mods == "c") {
  2510. return stopNativeHorizontalDelete(view, -1) || skipIgnoredNodes(view, -1);
  2511. } else if (code == 46 && !event.shiftKey || mac && code == 68 && mods == "c") {
  2512. return stopNativeHorizontalDelete(view, 1) || skipIgnoredNodes(view, 1);
  2513. } else if (code == 13 || code == 27) {
  2514. return true;
  2515. } else if (code == 37 || mac && code == 66 && mods == "c") {
  2516. var dir = code == 37 ? findDirection(view, view.state.selection.from) == "ltr" ? -1 : 1 : -1;
  2517. return selectHorizontally(view, dir, mods) || skipIgnoredNodes(view, dir);
  2518. } else if (code == 39 || mac && code == 70 && mods == "c") {
  2519. var _dir = code == 39 ? findDirection(view, view.state.selection.from) == "ltr" ? 1 : -1 : 1;
  2520. return selectHorizontally(view, _dir, mods) || skipIgnoredNodes(view, _dir);
  2521. } else if (code == 38 || mac && code == 80 && mods == "c") {
  2522. return selectVertically(view, -1, mods) || skipIgnoredNodes(view, -1);
  2523. } else if (code == 40 || mac && code == 78 && mods == "c") {
  2524. return safariDownArrowBug(view) || selectVertically(view, 1, mods) || skipIgnoredNodes(view, 1);
  2525. } else if (mods == (mac ? "m" : "c") && (code == 66 || code == 73 || code == 89 || code == 90)) {
  2526. return true;
  2527. }
  2528. return false;
  2529. }
  2530. function serializeForClipboard(view, slice) {
  2531. view.someProp("transformCopied", function (f) {
  2532. slice = f(slice, view);
  2533. });
  2534. var context = [],
  2535. _slice = slice,
  2536. content = _slice.content,
  2537. openStart = _slice.openStart,
  2538. openEnd = _slice.openEnd;
  2539. while (openStart > 1 && openEnd > 1 && content.childCount == 1 && content.firstChild.childCount == 1) {
  2540. openStart--;
  2541. openEnd--;
  2542. var node = content.firstChild;
  2543. context.push(node.type.name, node.attrs != node.type.defaultAttrs ? node.attrs : null);
  2544. content = node.content;
  2545. }
  2546. var serializer = view.someProp("clipboardSerializer") || prosemirrorModel.DOMSerializer.fromSchema(view.state.schema);
  2547. var doc = detachedDoc(),
  2548. wrap = doc.createElement("div");
  2549. wrap.appendChild(serializer.serializeFragment(content, {
  2550. document: doc
  2551. }));
  2552. var firstChild = wrap.firstChild,
  2553. needsWrap,
  2554. wrappers = 0;
  2555. while (firstChild && firstChild.nodeType == 1 && (needsWrap = wrapMap[firstChild.nodeName.toLowerCase()])) {
  2556. for (var i = needsWrap.length - 1; i >= 0; i--) {
  2557. var wrapper = doc.createElement(needsWrap[i]);
  2558. while (wrap.firstChild) wrapper.appendChild(wrap.firstChild);
  2559. wrap.appendChild(wrapper);
  2560. wrappers++;
  2561. }
  2562. firstChild = wrap.firstChild;
  2563. }
  2564. if (firstChild && firstChild.nodeType == 1) firstChild.setAttribute("data-pm-slice", "".concat(openStart, " ").concat(openEnd).concat(wrappers ? " -".concat(wrappers) : "", " ").concat(JSON.stringify(context)));
  2565. var text = view.someProp("clipboardTextSerializer", function (f) {
  2566. return f(slice, view);
  2567. }) || slice.content.textBetween(0, slice.content.size, "\n\n");
  2568. return {
  2569. dom: wrap,
  2570. text: text
  2571. };
  2572. }
  2573. function parseFromClipboard(view, text, html, plainText, $context) {
  2574. var inCode = $context.parent.type.spec.code;
  2575. var dom, slice;
  2576. if (!html && !text) return null;
  2577. var asText = text && (plainText || inCode || !html);
  2578. if (asText) {
  2579. view.someProp("transformPastedText", function (f) {
  2580. text = f(text, inCode || plainText, view);
  2581. });
  2582. if (inCode) return text ? new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(view.state.schema.text(text.replace(/\r\n?/g, "\n"))), 0, 0) : prosemirrorModel.Slice.empty;
  2583. var parsed = view.someProp("clipboardTextParser", function (f) {
  2584. return f(text, $context, plainText, view);
  2585. });
  2586. if (parsed) {
  2587. slice = parsed;
  2588. } else {
  2589. var marks = $context.marks();
  2590. var schema = view.state.schema,
  2591. serializer = prosemirrorModel.DOMSerializer.fromSchema(schema);
  2592. dom = document.createElement("div");
  2593. text.split(/(?:\r\n?|\n)+/).forEach(function (block) {
  2594. var p = dom.appendChild(document.createElement("p"));
  2595. if (block) p.appendChild(serializer.serializeNode(schema.text(block, marks)));
  2596. });
  2597. }
  2598. } else {
  2599. view.someProp("transformPastedHTML", function (f) {
  2600. html = f(html, view);
  2601. });
  2602. dom = readHTML(html);
  2603. if (webkit) restoreReplacedSpaces(dom);
  2604. }
  2605. var contextNode = dom && dom.querySelector("[data-pm-slice]");
  2606. var sliceData = contextNode && /^(\d+) (\d+)(?: -(\d+))? (.*)/.exec(contextNode.getAttribute("data-pm-slice") || "");
  2607. if (sliceData && sliceData[3]) for (var i = +sliceData[3]; i > 0; i--) {
  2608. var child = dom.firstChild;
  2609. while (child && child.nodeType != 1) child = child.nextSibling;
  2610. if (!child) break;
  2611. dom = child;
  2612. }
  2613. if (!slice) {
  2614. var parser = view.someProp("clipboardParser") || view.someProp("domParser") || prosemirrorModel.DOMParser.fromSchema(view.state.schema);
  2615. slice = parser.parseSlice(dom, {
  2616. preserveWhitespace: !!(asText || sliceData),
  2617. context: $context,
  2618. ruleFromNode: function ruleFromNode(dom) {
  2619. if (dom.nodeName == "BR" && !dom.nextSibling && dom.parentNode && !inlineParents.test(dom.parentNode.nodeName)) return {
  2620. ignore: true
  2621. };
  2622. return null;
  2623. }
  2624. });
  2625. }
  2626. if (sliceData) {
  2627. slice = addContext(closeSlice(slice, +sliceData[1], +sliceData[2]), sliceData[4]);
  2628. } else {
  2629. slice = prosemirrorModel.Slice.maxOpen(normalizeSiblings(slice.content, $context), true);
  2630. if (slice.openStart || slice.openEnd) {
  2631. var openStart = 0,
  2632. openEnd = 0;
  2633. for (var node = slice.content.firstChild; openStart < slice.openStart && !node.type.spec.isolating; openStart++, node = node.firstChild) {}
  2634. for (var _node2 = slice.content.lastChild; openEnd < slice.openEnd && !_node2.type.spec.isolating; openEnd++, _node2 = _node2.lastChild) {}
  2635. slice = closeSlice(slice, openStart, openEnd);
  2636. }
  2637. }
  2638. view.someProp("transformPasted", function (f) {
  2639. slice = f(slice, view);
  2640. });
  2641. return slice;
  2642. }
  2643. var inlineParents = /^(a|abbr|acronym|b|cite|code|del|em|i|ins|kbd|label|output|q|ruby|s|samp|span|strong|sub|sup|time|u|tt|var)$/i;
  2644. function normalizeSiblings(fragment, $context) {
  2645. if (fragment.childCount < 2) return fragment;
  2646. var _loop = function _loop() {
  2647. var parent = $context.node(d);
  2648. var match = parent.contentMatchAt($context.index(d));
  2649. var lastWrap,
  2650. result = [];
  2651. fragment.forEach(function (node) {
  2652. if (!result) return;
  2653. var wrap = match.findWrapping(node.type),
  2654. inLast;
  2655. if (!wrap) return result = null;
  2656. if (inLast = result.length && lastWrap.length && addToSibling(wrap, lastWrap, node, result[result.length - 1], 0)) {
  2657. result[result.length - 1] = inLast;
  2658. } else {
  2659. if (result.length) result[result.length - 1] = closeRight(result[result.length - 1], lastWrap.length);
  2660. var wrapped = withWrappers(node, wrap);
  2661. result.push(wrapped);
  2662. match = match.matchType(wrapped.type);
  2663. lastWrap = wrap;
  2664. }
  2665. });
  2666. if (result) return {
  2667. v: prosemirrorModel.Fragment.from(result)
  2668. };
  2669. },
  2670. _ret;
  2671. for (var d = $context.depth; d >= 0; d--) {
  2672. _ret = _loop();
  2673. if (_ret) return _ret.v;
  2674. }
  2675. return fragment;
  2676. }
  2677. function withWrappers(node, wrap) {
  2678. var from = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  2679. for (var i = wrap.length - 1; i >= from; i--) node = wrap[i].create(null, prosemirrorModel.Fragment.from(node));
  2680. return node;
  2681. }
  2682. function addToSibling(wrap, lastWrap, node, sibling, depth) {
  2683. if (depth < wrap.length && depth < lastWrap.length && wrap[depth] == lastWrap[depth]) {
  2684. var inner = addToSibling(wrap, lastWrap, node, sibling.lastChild, depth + 1);
  2685. if (inner) return sibling.copy(sibling.content.replaceChild(sibling.childCount - 1, inner));
  2686. var match = sibling.contentMatchAt(sibling.childCount);
  2687. if (match.matchType(depth == wrap.length - 1 ? node.type : wrap[depth + 1])) return sibling.copy(sibling.content.append(prosemirrorModel.Fragment.from(withWrappers(node, wrap, depth + 1))));
  2688. }
  2689. }
  2690. function closeRight(node, depth) {
  2691. if (depth == 0) return node;
  2692. var fragment = node.content.replaceChild(node.childCount - 1, closeRight(node.lastChild, depth - 1));
  2693. var fill = node.contentMatchAt(node.childCount).fillBefore(prosemirrorModel.Fragment.empty, true);
  2694. return node.copy(fragment.append(fill));
  2695. }
  2696. function closeRange(fragment, side, from, to, depth, openEnd) {
  2697. var node = side < 0 ? fragment.firstChild : fragment.lastChild,
  2698. inner = node.content;
  2699. if (fragment.childCount > 1) openEnd = 0;
  2700. if (depth < to - 1) inner = closeRange(inner, side, from, to, depth + 1, openEnd);
  2701. if (depth >= from) inner = side < 0 ? node.contentMatchAt(0).fillBefore(inner, openEnd <= depth).append(inner) : inner.append(node.contentMatchAt(node.childCount).fillBefore(prosemirrorModel.Fragment.empty, true));
  2702. return fragment.replaceChild(side < 0 ? 0 : fragment.childCount - 1, node.copy(inner));
  2703. }
  2704. function closeSlice(slice, openStart, openEnd) {
  2705. if (openStart < slice.openStart) slice = new prosemirrorModel.Slice(closeRange(slice.content, -1, openStart, slice.openStart, 0, slice.openEnd), openStart, slice.openEnd);
  2706. if (openEnd < slice.openEnd) slice = new prosemirrorModel.Slice(closeRange(slice.content, 1, openEnd, slice.openEnd, 0, 0), slice.openStart, openEnd);
  2707. return slice;
  2708. }
  2709. var wrapMap = {
  2710. thead: ["table"],
  2711. tbody: ["table"],
  2712. tfoot: ["table"],
  2713. caption: ["table"],
  2714. colgroup: ["table"],
  2715. col: ["table", "colgroup"],
  2716. tr: ["table", "tbody"],
  2717. td: ["table", "tbody", "tr"],
  2718. th: ["table", "tbody", "tr"]
  2719. };
  2720. var _detachedDoc = null;
  2721. function detachedDoc() {
  2722. return _detachedDoc || (_detachedDoc = document.implementation.createHTMLDocument("title"));
  2723. }
  2724. function readHTML(html) {
  2725. var metas = /^(\s*<meta [^>]*>)*/.exec(html);
  2726. if (metas) html = html.slice(metas[0].length);
  2727. var elt = detachedDoc().createElement("div");
  2728. var firstTag = /<([a-z][^>\s]+)/i.exec(html),
  2729. wrap;
  2730. if (wrap = firstTag && wrapMap[firstTag[1].toLowerCase()]) html = wrap.map(function (n) {
  2731. return "<" + n + ">";
  2732. }).join("") + html + wrap.map(function (n) {
  2733. return "</" + n + ">";
  2734. }).reverse().join("");
  2735. elt.innerHTML = html;
  2736. if (wrap) for (var i = 0; i < wrap.length; i++) elt = elt.querySelector(wrap[i]) || elt;
  2737. return elt;
  2738. }
  2739. function restoreReplacedSpaces(dom) {
  2740. var nodes = dom.querySelectorAll(chrome ? "span:not([class]):not([style])" : "span.Apple-converted-space");
  2741. for (var i = 0; i < nodes.length; i++) {
  2742. var node = nodes[i];
  2743. if (node.childNodes.length == 1 && node.textContent == "\xA0" && node.parentNode) node.parentNode.replaceChild(dom.ownerDocument.createTextNode(" "), node);
  2744. }
  2745. }
  2746. function addContext(slice, context) {
  2747. if (!slice.size) return slice;
  2748. var schema = slice.content.firstChild.type.schema,
  2749. array;
  2750. try {
  2751. array = JSON.parse(context);
  2752. } catch (e) {
  2753. return slice;
  2754. }
  2755. var content = slice.content,
  2756. openStart = slice.openStart,
  2757. openEnd = slice.openEnd;
  2758. for (var i = array.length - 2; i >= 0; i -= 2) {
  2759. var type = schema.nodes[array[i]];
  2760. if (!type || type.hasRequiredAttrs()) break;
  2761. content = prosemirrorModel.Fragment.from(type.create(array[i + 1], content));
  2762. openStart++;
  2763. openEnd++;
  2764. }
  2765. return new prosemirrorModel.Slice(content, openStart, openEnd);
  2766. }
  2767. var handlers = {};
  2768. var editHandlers = {};
  2769. var passiveHandlers = {
  2770. touchstart: true,
  2771. touchmove: true
  2772. };
  2773. var InputState = _createClass(function InputState() {
  2774. _classCallCheck(this, InputState);
  2775. this.shiftKey = false;
  2776. this.mouseDown = null;
  2777. this.lastKeyCode = null;
  2778. this.lastKeyCodeTime = 0;
  2779. this.lastClick = {
  2780. time: 0,
  2781. x: 0,
  2782. y: 0,
  2783. type: ""
  2784. };
  2785. this.lastSelectionOrigin = null;
  2786. this.lastSelectionTime = 0;
  2787. this.lastIOSEnter = 0;
  2788. this.lastIOSEnterFallbackTimeout = -1;
  2789. this.lastFocus = 0;
  2790. this.lastTouch = 0;
  2791. this.lastAndroidDelete = 0;
  2792. this.composing = false;
  2793. this.composingTimeout = -1;
  2794. this.compositionNodes = [];
  2795. this.compositionEndedAt = -2e8;
  2796. this.compositionID = 1;
  2797. this.compositionPendingChanges = 0;
  2798. this.domChangeCount = 0;
  2799. this.eventHandlers = Object.create(null);
  2800. this.hideSelectionGuard = null;
  2801. });
  2802. function initInput(view) {
  2803. var _loop2 = function _loop2() {
  2804. var handler = handlers[event];
  2805. view.dom.addEventListener(event, view.input.eventHandlers[event] = function (event) {
  2806. if (eventBelongsToView(view, event) && !runCustomHandler(view, event) && (view.editable || !(event.type in editHandlers))) handler(view, event);
  2807. }, passiveHandlers[event] ? {
  2808. passive: true
  2809. } : undefined);
  2810. };
  2811. for (var event in handlers) {
  2812. _loop2();
  2813. }
  2814. if (safari) view.dom.addEventListener("input", function () {
  2815. return null;
  2816. });
  2817. ensureListeners(view);
  2818. }
  2819. function setSelectionOrigin(view, origin) {
  2820. view.input.lastSelectionOrigin = origin;
  2821. view.input.lastSelectionTime = Date.now();
  2822. }
  2823. function destroyInput(view) {
  2824. view.domObserver.stop();
  2825. for (var type in view.input.eventHandlers) view.dom.removeEventListener(type, view.input.eventHandlers[type]);
  2826. clearTimeout(view.input.composingTimeout);
  2827. clearTimeout(view.input.lastIOSEnterFallbackTimeout);
  2828. }
  2829. function ensureListeners(view) {
  2830. view.someProp("handleDOMEvents", function (currentHandlers) {
  2831. for (var type in currentHandlers) if (!view.input.eventHandlers[type]) view.dom.addEventListener(type, view.input.eventHandlers[type] = function (event) {
  2832. return runCustomHandler(view, event);
  2833. });
  2834. });
  2835. }
  2836. function runCustomHandler(view, event) {
  2837. return view.someProp("handleDOMEvents", function (handlers) {
  2838. var handler = handlers[event.type];
  2839. return handler ? handler(view, event) || event.defaultPrevented : false;
  2840. });
  2841. }
  2842. function eventBelongsToView(view, event) {
  2843. if (!event.bubbles) return true;
  2844. if (event.defaultPrevented) return false;
  2845. for (var node = event.target; node != view.dom; node = node.parentNode) if (!node || node.nodeType == 11 || node.pmViewDesc && node.pmViewDesc.stopEvent(event)) return false;
  2846. return true;
  2847. }
  2848. function _dispatchEvent(view, event) {
  2849. if (!runCustomHandler(view, event) && handlers[event.type] && (view.editable || !(event.type in editHandlers))) handlers[event.type](view, event);
  2850. }
  2851. editHandlers.keydown = function (view, _event) {
  2852. var event = _event;
  2853. view.input.shiftKey = event.keyCode == 16 || event.shiftKey;
  2854. if (inOrNearComposition(view, event)) return;
  2855. view.input.lastKeyCode = event.keyCode;
  2856. view.input.lastKeyCodeTime = Date.now();
  2857. if (android && chrome && event.keyCode == 13) return;
  2858. if (event.keyCode != 229) view.domObserver.forceFlush();
  2859. if (ios && event.keyCode == 13 && !event.ctrlKey && !event.altKey && !event.metaKey) {
  2860. var now = Date.now();
  2861. view.input.lastIOSEnter = now;
  2862. view.input.lastIOSEnterFallbackTimeout = setTimeout(function () {
  2863. if (view.input.lastIOSEnter == now) {
  2864. view.someProp("handleKeyDown", function (f) {
  2865. return f(view, keyEvent(13, "Enter"));
  2866. });
  2867. view.input.lastIOSEnter = 0;
  2868. }
  2869. }, 200);
  2870. } else if (view.someProp("handleKeyDown", function (f) {
  2871. return f(view, event);
  2872. }) || captureKeyDown(view, event)) {
  2873. event.preventDefault();
  2874. } else {
  2875. setSelectionOrigin(view, "key");
  2876. }
  2877. };
  2878. editHandlers.keyup = function (view, event) {
  2879. if (event.keyCode == 16) view.input.shiftKey = false;
  2880. };
  2881. editHandlers.keypress = function (view, _event) {
  2882. var event = _event;
  2883. if (inOrNearComposition(view, event) || !event.charCode || event.ctrlKey && !event.altKey || mac && event.metaKey) return;
  2884. if (view.someProp("handleKeyPress", function (f) {
  2885. return f(view, event);
  2886. })) {
  2887. event.preventDefault();
  2888. return;
  2889. }
  2890. var sel = view.state.selection;
  2891. if (!(sel instanceof prosemirrorState.TextSelection) || !sel.$from.sameParent(sel.$to)) {
  2892. var text = String.fromCharCode(event.charCode);
  2893. if (!/[\r\n]/.test(text) && !view.someProp("handleTextInput", function (f) {
  2894. return f(view, sel.$from.pos, sel.$to.pos, text);
  2895. })) view.dispatch(view.state.tr.insertText(text).scrollIntoView());
  2896. event.preventDefault();
  2897. }
  2898. };
  2899. function eventCoords(event) {
  2900. return {
  2901. left: event.clientX,
  2902. top: event.clientY
  2903. };
  2904. }
  2905. function isNear(event, click) {
  2906. var dx = click.x - event.clientX,
  2907. dy = click.y - event.clientY;
  2908. return dx * dx + dy * dy < 100;
  2909. }
  2910. function runHandlerOnContext(view, propName, pos, inside, event) {
  2911. if (inside == -1) return false;
  2912. var $pos = view.state.doc.resolve(inside);
  2913. var _loop3 = function _loop3(i) {
  2914. if (view.someProp(propName, function (f) {
  2915. return i > $pos.depth ? f(view, pos, $pos.nodeAfter, $pos.before(i), event, true) : f(view, pos, $pos.node(i), $pos.before(i), event, false);
  2916. })) return {
  2917. v: true
  2918. };
  2919. },
  2920. _ret2;
  2921. for (var i = $pos.depth + 1; i > 0; i--) {
  2922. _ret2 = _loop3(i);
  2923. if (_ret2) return _ret2.v;
  2924. }
  2925. return false;
  2926. }
  2927. function updateSelection(view, selection, origin) {
  2928. if (!view.focused) view.focus();
  2929. var tr = view.state.tr.setSelection(selection);
  2930. if (origin == "pointer") tr.setMeta("pointer", true);
  2931. view.dispatch(tr);
  2932. }
  2933. function selectClickedLeaf(view, inside) {
  2934. if (inside == -1) return false;
  2935. var $pos = view.state.doc.resolve(inside),
  2936. node = $pos.nodeAfter;
  2937. if (node && node.isAtom && prosemirrorState.NodeSelection.isSelectable(node)) {
  2938. updateSelection(view, new prosemirrorState.NodeSelection($pos), "pointer");
  2939. return true;
  2940. }
  2941. return false;
  2942. }
  2943. function selectClickedNode(view, inside) {
  2944. if (inside == -1) return false;
  2945. var sel = view.state.selection,
  2946. selectedNode,
  2947. selectAt;
  2948. if (sel instanceof prosemirrorState.NodeSelection) selectedNode = sel.node;
  2949. var $pos = view.state.doc.resolve(inside);
  2950. for (var i = $pos.depth + 1; i > 0; i--) {
  2951. var node = i > $pos.depth ? $pos.nodeAfter : $pos.node(i);
  2952. if (prosemirrorState.NodeSelection.isSelectable(node)) {
  2953. if (selectedNode && sel.$from.depth > 0 && i >= sel.$from.depth && $pos.before(sel.$from.depth + 1) == sel.$from.pos) selectAt = $pos.before(sel.$from.depth);else selectAt = $pos.before(i);
  2954. break;
  2955. }
  2956. }
  2957. if (selectAt != null) {
  2958. updateSelection(view, prosemirrorState.NodeSelection.create(view.state.doc, selectAt), "pointer");
  2959. return true;
  2960. } else {
  2961. return false;
  2962. }
  2963. }
  2964. function handleSingleClick(view, pos, inside, event, selectNode) {
  2965. return runHandlerOnContext(view, "handleClickOn", pos, inside, event) || view.someProp("handleClick", function (f) {
  2966. return f(view, pos, event);
  2967. }) || (selectNode ? selectClickedNode(view, inside) : selectClickedLeaf(view, inside));
  2968. }
  2969. function handleDoubleClick(view, pos, inside, event) {
  2970. return runHandlerOnContext(view, "handleDoubleClickOn", pos, inside, event) || view.someProp("handleDoubleClick", function (f) {
  2971. return f(view, pos, event);
  2972. });
  2973. }
  2974. function handleTripleClick(view, pos, inside, event) {
  2975. return runHandlerOnContext(view, "handleTripleClickOn", pos, inside, event) || view.someProp("handleTripleClick", function (f) {
  2976. return f(view, pos, event);
  2977. }) || defaultTripleClick(view, inside, event);
  2978. }
  2979. function defaultTripleClick(view, inside, event) {
  2980. if (event.button != 0) return false;
  2981. var doc = view.state.doc;
  2982. if (inside == -1) {
  2983. if (doc.inlineContent) {
  2984. updateSelection(view, prosemirrorState.TextSelection.create(doc, 0, doc.content.size), "pointer");
  2985. return true;
  2986. }
  2987. return false;
  2988. }
  2989. var $pos = doc.resolve(inside);
  2990. for (var i = $pos.depth + 1; i > 0; i--) {
  2991. var node = i > $pos.depth ? $pos.nodeAfter : $pos.node(i);
  2992. var nodePos = $pos.before(i);
  2993. if (node.inlineContent) updateSelection(view, prosemirrorState.TextSelection.create(doc, nodePos + 1, nodePos + 1 + node.content.size), "pointer");else if (prosemirrorState.NodeSelection.isSelectable(node)) updateSelection(view, prosemirrorState.NodeSelection.create(doc, nodePos), "pointer");else continue;
  2994. return true;
  2995. }
  2996. }
  2997. function forceDOMFlush(view) {
  2998. return endComposition(view);
  2999. }
  3000. var selectNodeModifier = mac ? "metaKey" : "ctrlKey";
  3001. handlers.mousedown = function (view, _event) {
  3002. var event = _event;
  3003. view.input.shiftKey = event.shiftKey;
  3004. var flushed = forceDOMFlush(view);
  3005. var now = Date.now(),
  3006. type = "singleClick";
  3007. if (now - view.input.lastClick.time < 500 && isNear(event, view.input.lastClick) && !event[selectNodeModifier]) {
  3008. if (view.input.lastClick.type == "singleClick") type = "doubleClick";else if (view.input.lastClick.type == "doubleClick") type = "tripleClick";
  3009. }
  3010. view.input.lastClick = {
  3011. time: now,
  3012. x: event.clientX,
  3013. y: event.clientY,
  3014. type: type
  3015. };
  3016. var pos = view.posAtCoords(eventCoords(event));
  3017. if (!pos) return;
  3018. if (type == "singleClick") {
  3019. if (view.input.mouseDown) view.input.mouseDown.done();
  3020. view.input.mouseDown = new MouseDown(view, pos, event, !!flushed);
  3021. } else if ((type == "doubleClick" ? handleDoubleClick : handleTripleClick)(view, pos.pos, pos.inside, event)) {
  3022. event.preventDefault();
  3023. } else {
  3024. setSelectionOrigin(view, "pointer");
  3025. }
  3026. };
  3027. var MouseDown = function () {
  3028. function MouseDown(view, pos, event, flushed) {
  3029. var _this8 = this;
  3030. _classCallCheck(this, MouseDown);
  3031. this.view = view;
  3032. this.pos = pos;
  3033. this.event = event;
  3034. this.flushed = flushed;
  3035. this.delayedSelectionSync = false;
  3036. this.mightDrag = null;
  3037. this.startDoc = view.state.doc;
  3038. this.selectNode = !!event[selectNodeModifier];
  3039. this.allowDefault = event.shiftKey;
  3040. var targetNode, targetPos;
  3041. if (pos.inside > -1) {
  3042. targetNode = view.state.doc.nodeAt(pos.inside);
  3043. targetPos = pos.inside;
  3044. } else {
  3045. var $pos = view.state.doc.resolve(pos.pos);
  3046. targetNode = $pos.parent;
  3047. targetPos = $pos.depth ? $pos.before() : 0;
  3048. }
  3049. var target = flushed ? null : event.target;
  3050. var targetDesc = target ? view.docView.nearestDesc(target, true) : null;
  3051. this.target = targetDesc ? targetDesc.dom : null;
  3052. var selection = view.state.selection;
  3053. if (event.button == 0 && targetNode.type.spec.draggable && targetNode.type.spec.selectable !== false || selection instanceof prosemirrorState.NodeSelection && selection.from <= targetPos && selection.to > targetPos) this.mightDrag = {
  3054. node: targetNode,
  3055. pos: targetPos,
  3056. addAttr: !!(this.target && !this.target.draggable),
  3057. setUneditable: !!(this.target && gecko && !this.target.hasAttribute("contentEditable"))
  3058. };
  3059. if (this.target && this.mightDrag && (this.mightDrag.addAttr || this.mightDrag.setUneditable)) {
  3060. this.view.domObserver.stop();
  3061. if (this.mightDrag.addAttr) this.target.draggable = true;
  3062. if (this.mightDrag.setUneditable) setTimeout(function () {
  3063. if (_this8.view.input.mouseDown == _this8) _this8.target.setAttribute("contentEditable", "false");
  3064. }, 20);
  3065. this.view.domObserver.start();
  3066. }
  3067. view.root.addEventListener("mouseup", this.up = this.up.bind(this));
  3068. view.root.addEventListener("mousemove", this.move = this.move.bind(this));
  3069. setSelectionOrigin(view, "pointer");
  3070. }
  3071. _createClass(MouseDown, [{
  3072. key: "done",
  3073. value: function done() {
  3074. var _this9 = this;
  3075. this.view.root.removeEventListener("mouseup", this.up);
  3076. this.view.root.removeEventListener("mousemove", this.move);
  3077. if (this.mightDrag && this.target) {
  3078. this.view.domObserver.stop();
  3079. if (this.mightDrag.addAttr) this.target.removeAttribute("draggable");
  3080. if (this.mightDrag.setUneditable) this.target.removeAttribute("contentEditable");
  3081. this.view.domObserver.start();
  3082. }
  3083. if (this.delayedSelectionSync) setTimeout(function () {
  3084. return selectionToDOM(_this9.view);
  3085. });
  3086. this.view.input.mouseDown = null;
  3087. }
  3088. }, {
  3089. key: "up",
  3090. value: function up(event) {
  3091. this.done();
  3092. if (!this.view.dom.contains(event.target)) return;
  3093. var pos = this.pos;
  3094. if (this.view.state.doc != this.startDoc) pos = this.view.posAtCoords(eventCoords(event));
  3095. this.updateAllowDefault(event);
  3096. if (this.allowDefault || !pos) {
  3097. setSelectionOrigin(this.view, "pointer");
  3098. } else if (handleSingleClick(this.view, pos.pos, pos.inside, event, this.selectNode)) {
  3099. event.preventDefault();
  3100. } else if (event.button == 0 && (this.flushed || safari && this.mightDrag && !this.mightDrag.node.isAtom || chrome && !this.view.state.selection.visible && Math.min(Math.abs(pos.pos - this.view.state.selection.from), Math.abs(pos.pos - this.view.state.selection.to)) <= 2)) {
  3101. updateSelection(this.view, prosemirrorState.Selection.near(this.view.state.doc.resolve(pos.pos)), "pointer");
  3102. event.preventDefault();
  3103. } else {
  3104. setSelectionOrigin(this.view, "pointer");
  3105. }
  3106. }
  3107. }, {
  3108. key: "move",
  3109. value: function move(event) {
  3110. this.updateAllowDefault(event);
  3111. setSelectionOrigin(this.view, "pointer");
  3112. if (event.buttons == 0) this.done();
  3113. }
  3114. }, {
  3115. key: "updateAllowDefault",
  3116. value: function updateAllowDefault(event) {
  3117. if (!this.allowDefault && (Math.abs(this.event.x - event.clientX) > 4 || Math.abs(this.event.y - event.clientY) > 4)) this.allowDefault = true;
  3118. }
  3119. }]);
  3120. return MouseDown;
  3121. }();
  3122. handlers.touchstart = function (view) {
  3123. view.input.lastTouch = Date.now();
  3124. forceDOMFlush(view);
  3125. setSelectionOrigin(view, "pointer");
  3126. };
  3127. handlers.touchmove = function (view) {
  3128. view.input.lastTouch = Date.now();
  3129. setSelectionOrigin(view, "pointer");
  3130. };
  3131. handlers.contextmenu = function (view) {
  3132. return forceDOMFlush(view);
  3133. };
  3134. function inOrNearComposition(view, event) {
  3135. if (view.composing) return true;
  3136. if (safari && Math.abs(event.timeStamp - view.input.compositionEndedAt) < 500) {
  3137. view.input.compositionEndedAt = -2e8;
  3138. return true;
  3139. }
  3140. return false;
  3141. }
  3142. var timeoutComposition = android ? 5000 : -1;
  3143. editHandlers.compositionstart = editHandlers.compositionupdate = function (view) {
  3144. if (!view.composing) {
  3145. view.domObserver.flush();
  3146. var state = view.state,
  3147. $pos = state.selection.$from;
  3148. if (state.selection.empty && (state.storedMarks || !$pos.textOffset && $pos.parentOffset && $pos.nodeBefore.marks.some(function (m) {
  3149. return m.type.spec.inclusive === false;
  3150. }))) {
  3151. view.markCursor = view.state.storedMarks || $pos.marks();
  3152. endComposition(view, true);
  3153. view.markCursor = null;
  3154. } else {
  3155. endComposition(view);
  3156. if (gecko && state.selection.empty && $pos.parentOffset && !$pos.textOffset && $pos.nodeBefore.marks.length) {
  3157. var sel = view.domSelectionRange();
  3158. for (var node = sel.focusNode, offset = sel.focusOffset; node && node.nodeType == 1 && offset != 0;) {
  3159. var before = offset < 0 ? node.lastChild : node.childNodes[offset - 1];
  3160. if (!before) break;
  3161. if (before.nodeType == 3) {
  3162. view.domSelection().collapse(before, before.nodeValue.length);
  3163. break;
  3164. } else {
  3165. node = before;
  3166. offset = -1;
  3167. }
  3168. }
  3169. }
  3170. }
  3171. view.input.composing = true;
  3172. }
  3173. scheduleComposeEnd(view, timeoutComposition);
  3174. };
  3175. editHandlers.compositionend = function (view, event) {
  3176. if (view.composing) {
  3177. view.input.composing = false;
  3178. view.input.compositionEndedAt = event.timeStamp;
  3179. view.input.compositionPendingChanges = view.domObserver.pendingRecords().length ? view.input.compositionID : 0;
  3180. if (view.input.compositionPendingChanges) Promise.resolve().then(function () {
  3181. return view.domObserver.flush();
  3182. });
  3183. view.input.compositionID++;
  3184. scheduleComposeEnd(view, 20);
  3185. }
  3186. };
  3187. function scheduleComposeEnd(view, delay) {
  3188. clearTimeout(view.input.composingTimeout);
  3189. if (delay > -1) view.input.composingTimeout = setTimeout(function () {
  3190. return endComposition(view);
  3191. }, delay);
  3192. }
  3193. function clearComposition(view) {
  3194. if (view.composing) {
  3195. view.input.composing = false;
  3196. view.input.compositionEndedAt = timestampFromCustomEvent();
  3197. }
  3198. while (view.input.compositionNodes.length > 0) view.input.compositionNodes.pop().markParentsDirty();
  3199. }
  3200. function timestampFromCustomEvent() {
  3201. var event = document.createEvent("Event");
  3202. event.initEvent("event", true, true);
  3203. return event.timeStamp;
  3204. }
  3205. function endComposition(view) {
  3206. var forceUpdate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  3207. if (android && view.domObserver.flushingSoon >= 0) return;
  3208. view.domObserver.forceFlush();
  3209. clearComposition(view);
  3210. if (forceUpdate || view.docView && view.docView.dirty) {
  3211. var sel = selectionFromDOM(view);
  3212. if (sel && !sel.eq(view.state.selection)) view.dispatch(view.state.tr.setSelection(sel));else view.updateState(view.state);
  3213. return true;
  3214. }
  3215. return false;
  3216. }
  3217. function captureCopy(view, dom) {
  3218. if (!view.dom.parentNode) return;
  3219. var wrap = view.dom.parentNode.appendChild(document.createElement("div"));
  3220. wrap.appendChild(dom);
  3221. wrap.style.cssText = "position: fixed; left: -10000px; top: 10px";
  3222. var sel = getSelection(),
  3223. range = document.createRange();
  3224. range.selectNodeContents(dom);
  3225. view.dom.blur();
  3226. sel.removeAllRanges();
  3227. sel.addRange(range);
  3228. setTimeout(function () {
  3229. if (wrap.parentNode) wrap.parentNode.removeChild(wrap);
  3230. view.focus();
  3231. }, 50);
  3232. }
  3233. var brokenClipboardAPI = ie && ie_version < 15 || ios && webkit_version < 604;
  3234. handlers.copy = editHandlers.cut = function (view, _event) {
  3235. var event = _event;
  3236. var sel = view.state.selection,
  3237. cut = event.type == "cut";
  3238. if (sel.empty) return;
  3239. var data = brokenClipboardAPI ? null : event.clipboardData;
  3240. var slice = sel.content(),
  3241. _serializeForClipboar = serializeForClipboard(view, slice),
  3242. dom = _serializeForClipboar.dom,
  3243. text = _serializeForClipboar.text;
  3244. if (data) {
  3245. event.preventDefault();
  3246. data.clearData();
  3247. data.setData("text/html", dom.innerHTML);
  3248. data.setData("text/plain", text);
  3249. } else {
  3250. captureCopy(view, dom);
  3251. }
  3252. if (cut) view.dispatch(view.state.tr.deleteSelection().scrollIntoView().setMeta("uiEvent", "cut"));
  3253. };
  3254. function sliceSingleNode(slice) {
  3255. return slice.openStart == 0 && slice.openEnd == 0 && slice.content.childCount == 1 ? slice.content.firstChild : null;
  3256. }
  3257. function capturePaste(view, event) {
  3258. if (!view.dom.parentNode) return;
  3259. var plainText = view.input.shiftKey || view.state.selection.$from.parent.type.spec.code;
  3260. var target = view.dom.parentNode.appendChild(document.createElement(plainText ? "textarea" : "div"));
  3261. if (!plainText) target.contentEditable = "true";
  3262. target.style.cssText = "position: fixed; left: -10000px; top: 10px";
  3263. target.focus();
  3264. var plain = view.input.shiftKey && view.input.lastKeyCode != 45;
  3265. setTimeout(function () {
  3266. view.focus();
  3267. if (target.parentNode) target.parentNode.removeChild(target);
  3268. if (plainText) doPaste(view, target.value, null, plain, event);else doPaste(view, target.textContent, target.innerHTML, plain, event);
  3269. }, 50);
  3270. }
  3271. function doPaste(view, text, html, preferPlain, event) {
  3272. var slice = parseFromClipboard(view, text, html, preferPlain, view.state.selection.$from);
  3273. if (view.someProp("handlePaste", function (f) {
  3274. return f(view, event, slice || prosemirrorModel.Slice.empty);
  3275. })) return true;
  3276. if (!slice) return false;
  3277. var singleNode = sliceSingleNode(slice);
  3278. var tr = singleNode ? view.state.tr.replaceSelectionWith(singleNode, preferPlain) : view.state.tr.replaceSelection(slice);
  3279. view.dispatch(tr.scrollIntoView().setMeta("paste", true).setMeta("uiEvent", "paste"));
  3280. return true;
  3281. }
  3282. function getText(clipboardData) {
  3283. var text = clipboardData.getData("text/plain") || clipboardData.getData("Text");
  3284. if (text) return text;
  3285. var uris = clipboardData.getData("text/uri-list");
  3286. return uris ? uris.replace(/\r?\n/g, " ") : "";
  3287. }
  3288. editHandlers.paste = function (view, _event) {
  3289. var event = _event;
  3290. if (view.composing && !android) return;
  3291. var data = brokenClipboardAPI ? null : event.clipboardData;
  3292. var plain = view.input.shiftKey && view.input.lastKeyCode != 45;
  3293. if (data && doPaste(view, getText(data), data.getData("text/html"), plain, event)) event.preventDefault();else capturePaste(view, event);
  3294. };
  3295. var Dragging = _createClass(function Dragging(slice, move, node) {
  3296. _classCallCheck(this, Dragging);
  3297. this.slice = slice;
  3298. this.move = move;
  3299. this.node = node;
  3300. });
  3301. var dragCopyModifier = mac ? "altKey" : "ctrlKey";
  3302. handlers.dragstart = function (view, _event) {
  3303. var event = _event;
  3304. var mouseDown = view.input.mouseDown;
  3305. if (mouseDown) mouseDown.done();
  3306. if (!event.dataTransfer) return;
  3307. var sel = view.state.selection;
  3308. var pos = sel.empty ? null : view.posAtCoords(eventCoords(event));
  3309. var node;
  3310. if (pos && pos.pos >= sel.from && pos.pos <= (sel instanceof prosemirrorState.NodeSelection ? sel.to - 1 : sel.to)) ;else if (mouseDown && mouseDown.mightDrag) {
  3311. node = prosemirrorState.NodeSelection.create(view.state.doc, mouseDown.mightDrag.pos);
  3312. } else if (event.target && event.target.nodeType == 1) {
  3313. var desc = view.docView.nearestDesc(event.target, true);
  3314. if (desc && desc.node.type.spec.draggable && desc != view.docView) node = prosemirrorState.NodeSelection.create(view.state.doc, desc.posBefore);
  3315. }
  3316. var slice = (node || view.state.selection).content(),
  3317. _serializeForClipboar2 = serializeForClipboard(view, slice),
  3318. dom = _serializeForClipboar2.dom,
  3319. text = _serializeForClipboar2.text;
  3320. event.dataTransfer.clearData();
  3321. event.dataTransfer.setData(brokenClipboardAPI ? "Text" : "text/html", dom.innerHTML);
  3322. event.dataTransfer.effectAllowed = "copyMove";
  3323. if (!brokenClipboardAPI) event.dataTransfer.setData("text/plain", text);
  3324. view.dragging = new Dragging(slice, !event[dragCopyModifier], node);
  3325. };
  3326. handlers.dragend = function (view) {
  3327. var dragging = view.dragging;
  3328. window.setTimeout(function () {
  3329. if (view.dragging == dragging) view.dragging = null;
  3330. }, 50);
  3331. };
  3332. editHandlers.dragover = editHandlers.dragenter = function (_, e) {
  3333. return e.preventDefault();
  3334. };
  3335. editHandlers.drop = function (view, _event) {
  3336. var event = _event;
  3337. var dragging = view.dragging;
  3338. view.dragging = null;
  3339. if (!event.dataTransfer) return;
  3340. var eventPos = view.posAtCoords(eventCoords(event));
  3341. if (!eventPos) return;
  3342. var $mouse = view.state.doc.resolve(eventPos.pos);
  3343. var slice = dragging && dragging.slice;
  3344. if (slice) {
  3345. view.someProp("transformPasted", function (f) {
  3346. slice = f(slice, view);
  3347. });
  3348. } else {
  3349. slice = parseFromClipboard(view, getText(event.dataTransfer), brokenClipboardAPI ? null : event.dataTransfer.getData("text/html"), false, $mouse);
  3350. }
  3351. var move = !!(dragging && !event[dragCopyModifier]);
  3352. if (view.someProp("handleDrop", function (f) {
  3353. return f(view, event, slice || prosemirrorModel.Slice.empty, move);
  3354. })) {
  3355. event.preventDefault();
  3356. return;
  3357. }
  3358. if (!slice) return;
  3359. event.preventDefault();
  3360. var insertPos = slice ? prosemirrorTransform.dropPoint(view.state.doc, $mouse.pos, slice) : $mouse.pos;
  3361. if (insertPos == null) insertPos = $mouse.pos;
  3362. var tr = view.state.tr;
  3363. if (move) {
  3364. var node = dragging.node;
  3365. if (node) node.replace(tr);else tr.deleteSelection();
  3366. }
  3367. var pos = tr.mapping.map(insertPos);
  3368. var isNode = slice.openStart == 0 && slice.openEnd == 0 && slice.content.childCount == 1;
  3369. var beforeInsert = tr.doc;
  3370. if (isNode) tr.replaceRangeWith(pos, pos, slice.content.firstChild);else tr.replaceRange(pos, pos, slice);
  3371. if (tr.doc.eq(beforeInsert)) return;
  3372. var $pos = tr.doc.resolve(pos);
  3373. if (isNode && prosemirrorState.NodeSelection.isSelectable(slice.content.firstChild) && $pos.nodeAfter && $pos.nodeAfter.sameMarkup(slice.content.firstChild)) {
  3374. tr.setSelection(new prosemirrorState.NodeSelection($pos));
  3375. } else {
  3376. var end = tr.mapping.map(insertPos);
  3377. tr.mapping.maps[tr.mapping.maps.length - 1].forEach(function (_from, _to, _newFrom, newTo) {
  3378. return end = newTo;
  3379. });
  3380. tr.setSelection(selectionBetween(view, $pos, tr.doc.resolve(end)));
  3381. }
  3382. view.focus();
  3383. view.dispatch(tr.setMeta("uiEvent", "drop"));
  3384. };
  3385. handlers.focus = function (view) {
  3386. view.input.lastFocus = Date.now();
  3387. if (!view.focused) {
  3388. view.domObserver.stop();
  3389. view.dom.classList.add("ProseMirror-focused");
  3390. view.domObserver.start();
  3391. view.focused = true;
  3392. setTimeout(function () {
  3393. if (view.docView && view.hasFocus() && !view.domObserver.currentSelection.eq(view.domSelectionRange())) selectionToDOM(view);
  3394. }, 20);
  3395. }
  3396. };
  3397. handlers.blur = function (view, _event) {
  3398. var event = _event;
  3399. if (view.focused) {
  3400. view.domObserver.stop();
  3401. view.dom.classList.remove("ProseMirror-focused");
  3402. view.domObserver.start();
  3403. if (event.relatedTarget && view.dom.contains(event.relatedTarget)) view.domObserver.currentSelection.clear();
  3404. view.focused = false;
  3405. }
  3406. };
  3407. handlers.beforeinput = function (view, _event) {
  3408. var event = _event;
  3409. if (chrome && android && event.inputType == "deleteContentBackward") {
  3410. view.domObserver.flushSoon();
  3411. var domChangeCount = view.input.domChangeCount;
  3412. setTimeout(function () {
  3413. if (view.input.domChangeCount != domChangeCount) return;
  3414. view.dom.blur();
  3415. view.focus();
  3416. if (view.someProp("handleKeyDown", function (f) {
  3417. return f(view, keyEvent(8, "Backspace"));
  3418. })) return;
  3419. var $cursor = view.state.selection.$cursor;
  3420. if ($cursor && $cursor.pos > 0) view.dispatch(view.state.tr["delete"]($cursor.pos - 1, $cursor.pos).scrollIntoView());
  3421. }, 50);
  3422. }
  3423. };
  3424. for (var prop in editHandlers) handlers[prop] = editHandlers[prop];
  3425. function compareObjs(a, b) {
  3426. if (a == b) return true;
  3427. for (var p in a) if (a[p] !== b[p]) return false;
  3428. for (var _p in b) if (!(_p in a)) return false;
  3429. return true;
  3430. }
  3431. var WidgetType = function () {
  3432. function WidgetType(toDOM, spec) {
  3433. _classCallCheck(this, WidgetType);
  3434. this.toDOM = toDOM;
  3435. this.spec = spec || noSpec;
  3436. this.side = this.spec.side || 0;
  3437. }
  3438. _createClass(WidgetType, [{
  3439. key: "map",
  3440. value: function map(mapping, span, offset, oldOffset) {
  3441. var _mapping$mapResult = mapping.mapResult(span.from + oldOffset, this.side < 0 ? -1 : 1),
  3442. pos = _mapping$mapResult.pos,
  3443. deleted = _mapping$mapResult.deleted;
  3444. return deleted ? null : new Decoration(pos - offset, pos - offset, this);
  3445. }
  3446. }, {
  3447. key: "valid",
  3448. value: function valid() {
  3449. return true;
  3450. }
  3451. }, {
  3452. key: "eq",
  3453. value: function eq(other) {
  3454. return this == other || other instanceof WidgetType && (this.spec.key && this.spec.key == other.spec.key || this.toDOM == other.toDOM && compareObjs(this.spec, other.spec));
  3455. }
  3456. }, {
  3457. key: "destroy",
  3458. value: function destroy(node) {
  3459. if (this.spec.destroy) this.spec.destroy(node);
  3460. }
  3461. }]);
  3462. return WidgetType;
  3463. }();
  3464. var InlineType = function () {
  3465. function InlineType(attrs, spec) {
  3466. _classCallCheck(this, InlineType);
  3467. this.attrs = attrs;
  3468. this.spec = spec || noSpec;
  3469. }
  3470. _createClass(InlineType, [{
  3471. key: "map",
  3472. value: function map(mapping, span, offset, oldOffset) {
  3473. var from = mapping.map(span.from + oldOffset, this.spec.inclusiveStart ? -1 : 1) - offset;
  3474. var to = mapping.map(span.to + oldOffset, this.spec.inclusiveEnd ? 1 : -1) - offset;
  3475. return from >= to ? null : new Decoration(from, to, this);
  3476. }
  3477. }, {
  3478. key: "valid",
  3479. value: function valid(_, span) {
  3480. return span.from < span.to;
  3481. }
  3482. }, {
  3483. key: "eq",
  3484. value: function eq(other) {
  3485. return this == other || other instanceof InlineType && compareObjs(this.attrs, other.attrs) && compareObjs(this.spec, other.spec);
  3486. }
  3487. }, {
  3488. key: "destroy",
  3489. value: function destroy() {}
  3490. }], [{
  3491. key: "is",
  3492. value: function is(span) {
  3493. return span.type instanceof InlineType;
  3494. }
  3495. }]);
  3496. return InlineType;
  3497. }();
  3498. var NodeType = function () {
  3499. function NodeType(attrs, spec) {
  3500. _classCallCheck(this, NodeType);
  3501. this.attrs = attrs;
  3502. this.spec = spec || noSpec;
  3503. }
  3504. _createClass(NodeType, [{
  3505. key: "map",
  3506. value: function map(mapping, span, offset, oldOffset) {
  3507. var from = mapping.mapResult(span.from + oldOffset, 1);
  3508. if (from.deleted) return null;
  3509. var to = mapping.mapResult(span.to + oldOffset, -1);
  3510. if (to.deleted || to.pos <= from.pos) return null;
  3511. return new Decoration(from.pos - offset, to.pos - offset, this);
  3512. }
  3513. }, {
  3514. key: "valid",
  3515. value: function valid(node, span) {
  3516. var _node$content$findInd = node.content.findIndex(span.from),
  3517. index = _node$content$findInd.index,
  3518. offset = _node$content$findInd.offset,
  3519. child;
  3520. return offset == span.from && !(child = node.child(index)).isText && offset + child.nodeSize == span.to;
  3521. }
  3522. }, {
  3523. key: "eq",
  3524. value: function eq(other) {
  3525. return this == other || other instanceof NodeType && compareObjs(this.attrs, other.attrs) && compareObjs(this.spec, other.spec);
  3526. }
  3527. }, {
  3528. key: "destroy",
  3529. value: function destroy() {}
  3530. }]);
  3531. return NodeType;
  3532. }();
  3533. var Decoration = function () {
  3534. function Decoration(from, to, type) {
  3535. _classCallCheck(this, Decoration);
  3536. this.from = from;
  3537. this.to = to;
  3538. this.type = type;
  3539. }
  3540. _createClass(Decoration, [{
  3541. key: "copy",
  3542. value: function copy(from, to) {
  3543. return new Decoration(from, to, this.type);
  3544. }
  3545. }, {
  3546. key: "eq",
  3547. value: function eq(other) {
  3548. var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  3549. return this.type.eq(other.type) && this.from + offset == other.from && this.to + offset == other.to;
  3550. }
  3551. }, {
  3552. key: "map",
  3553. value: function map(mapping, offset, oldOffset) {
  3554. return this.type.map(mapping, this, offset, oldOffset);
  3555. }
  3556. }, {
  3557. key: "spec",
  3558. get: function get() {
  3559. return this.type.spec;
  3560. }
  3561. }, {
  3562. key: "inline",
  3563. get: function get() {
  3564. return this.type instanceof InlineType;
  3565. }
  3566. }, {
  3567. key: "widget",
  3568. get: function get() {
  3569. return this.type instanceof WidgetType;
  3570. }
  3571. }], [{
  3572. key: "widget",
  3573. value: function widget(pos, toDOM, spec) {
  3574. return new Decoration(pos, pos, new WidgetType(toDOM, spec));
  3575. }
  3576. }, {
  3577. key: "inline",
  3578. value: function inline(from, to, attrs, spec) {
  3579. return new Decoration(from, to, new InlineType(attrs, spec));
  3580. }
  3581. }, {
  3582. key: "node",
  3583. value: function node(from, to, attrs, spec) {
  3584. return new Decoration(from, to, new NodeType(attrs, spec));
  3585. }
  3586. }]);
  3587. return Decoration;
  3588. }();
  3589. var none = [],
  3590. noSpec = {};
  3591. var DecorationSet = function () {
  3592. function DecorationSet(local, children) {
  3593. _classCallCheck(this, DecorationSet);
  3594. this.local = local.length ? local : none;
  3595. this.children = children.length ? children : none;
  3596. }
  3597. _createClass(DecorationSet, [{
  3598. key: "find",
  3599. value: function find(start, end, predicate) {
  3600. var result = [];
  3601. this.findInner(start == null ? 0 : start, end == null ? 1e9 : end, result, 0, predicate);
  3602. return result;
  3603. }
  3604. }, {
  3605. key: "findInner",
  3606. value: function findInner(start, end, result, offset, predicate) {
  3607. for (var i = 0; i < this.local.length; i++) {
  3608. var span = this.local[i];
  3609. if (span.from <= end && span.to >= start && (!predicate || predicate(span.spec))) result.push(span.copy(span.from + offset, span.to + offset));
  3610. }
  3611. for (var _i5 = 0; _i5 < this.children.length; _i5 += 3) {
  3612. if (this.children[_i5] < end && this.children[_i5 + 1] > start) {
  3613. var childOff = this.children[_i5] + 1;
  3614. this.children[_i5 + 2].findInner(start - childOff, end - childOff, result, offset + childOff, predicate);
  3615. }
  3616. }
  3617. }
  3618. }, {
  3619. key: "map",
  3620. value: function map(mapping, doc, options) {
  3621. if (this == empty || mapping.maps.length == 0) return this;
  3622. return this.mapInner(mapping, doc, 0, 0, options || noSpec);
  3623. }
  3624. }, {
  3625. key: "mapInner",
  3626. value: function mapInner(mapping, node, offset, oldOffset, options) {
  3627. var newLocal;
  3628. for (var i = 0; i < this.local.length; i++) {
  3629. var mapped = this.local[i].map(mapping, offset, oldOffset);
  3630. if (mapped && mapped.type.valid(node, mapped)) (newLocal || (newLocal = [])).push(mapped);else if (options.onRemove) options.onRemove(this.local[i].spec);
  3631. }
  3632. if (this.children.length) return mapChildren(this.children, newLocal || [], mapping, node, offset, oldOffset, options);else return newLocal ? new DecorationSet(newLocal.sort(byPos), none) : empty;
  3633. }
  3634. }, {
  3635. key: "add",
  3636. value: function add(doc, decorations) {
  3637. if (!decorations.length) return this;
  3638. if (this == empty) return DecorationSet.create(doc, decorations);
  3639. return this.addInner(doc, decorations, 0);
  3640. }
  3641. }, {
  3642. key: "addInner",
  3643. value: function addInner(doc, decorations, offset) {
  3644. var _this10 = this;
  3645. var children,
  3646. childIndex = 0;
  3647. doc.forEach(function (childNode, childOffset) {
  3648. var baseOffset = childOffset + offset,
  3649. found;
  3650. if (!(found = takeSpansForNode(decorations, childNode, baseOffset))) return;
  3651. if (!children) children = _this10.children.slice();
  3652. while (childIndex < children.length && children[childIndex] < childOffset) childIndex += 3;
  3653. if (children[childIndex] == childOffset) children[childIndex + 2] = children[childIndex + 2].addInner(childNode, found, baseOffset + 1);else children.splice(childIndex, 0, childOffset, childOffset + childNode.nodeSize, buildTree(found, childNode, baseOffset + 1, noSpec));
  3654. childIndex += 3;
  3655. });
  3656. var local = moveSpans(childIndex ? withoutNulls(decorations) : decorations, -offset);
  3657. for (var i = 0; i < local.length; i++) if (!local[i].type.valid(doc, local[i])) local.splice(i--, 1);
  3658. return new DecorationSet(local.length ? this.local.concat(local).sort(byPos) : this.local, children || this.children);
  3659. }
  3660. }, {
  3661. key: "remove",
  3662. value: function remove(decorations) {
  3663. if (decorations.length == 0 || this == empty) return this;
  3664. return this.removeInner(decorations, 0);
  3665. }
  3666. }, {
  3667. key: "removeInner",
  3668. value: function removeInner(decorations, offset) {
  3669. var children = this.children,
  3670. local = this.local;
  3671. for (var i = 0; i < children.length; i += 3) {
  3672. var found = void 0;
  3673. var from = children[i] + offset,
  3674. to = children[i + 1] + offset;
  3675. for (var j = 0, span; j < decorations.length; j++) if (span = decorations[j]) {
  3676. if (span.from > from && span.to < to) {
  3677. decorations[j] = null;
  3678. (found || (found = [])).push(span);
  3679. }
  3680. }
  3681. if (!found) continue;
  3682. if (children == this.children) children = this.children.slice();
  3683. var removed = children[i + 2].removeInner(found, from + 1);
  3684. if (removed != empty) {
  3685. children[i + 2] = removed;
  3686. } else {
  3687. children.splice(i, 3);
  3688. i -= 3;
  3689. }
  3690. }
  3691. if (local.length) for (var _i6 = 0, _span; _i6 < decorations.length; _i6++) if (_span = decorations[_i6]) {
  3692. for (var _j2 = 0; _j2 < local.length; _j2++) if (local[_j2].eq(_span, offset)) {
  3693. if (local == this.local) local = this.local.slice();
  3694. local.splice(_j2--, 1);
  3695. }
  3696. }
  3697. if (children == this.children && local == this.local) return this;
  3698. return local.length || children.length ? new DecorationSet(local, children) : empty;
  3699. }
  3700. }, {
  3701. key: "forChild",
  3702. value: function forChild(offset, node) {
  3703. if (this == empty) return this;
  3704. if (node.isLeaf) return DecorationSet.empty;
  3705. var child, local;
  3706. for (var i = 0; i < this.children.length; i += 3) if (this.children[i] >= offset) {
  3707. if (this.children[i] == offset) child = this.children[i + 2];
  3708. break;
  3709. }
  3710. var start = offset + 1,
  3711. end = start + node.content.size;
  3712. for (var _i7 = 0; _i7 < this.local.length; _i7++) {
  3713. var dec = this.local[_i7];
  3714. if (dec.from < end && dec.to > start && dec.type instanceof InlineType) {
  3715. var from = Math.max(start, dec.from) - start,
  3716. to = Math.min(end, dec.to) - start;
  3717. if (from < to) (local || (local = [])).push(dec.copy(from, to));
  3718. }
  3719. }
  3720. if (local) {
  3721. var localSet = new DecorationSet(local.sort(byPos), none);
  3722. return child ? new DecorationGroup([localSet, child]) : localSet;
  3723. }
  3724. return child || empty;
  3725. }
  3726. }, {
  3727. key: "eq",
  3728. value: function eq(other) {
  3729. if (this == other) return true;
  3730. if (!(other instanceof DecorationSet) || this.local.length != other.local.length || this.children.length != other.children.length) return false;
  3731. for (var i = 0; i < this.local.length; i++) if (!this.local[i].eq(other.local[i])) return false;
  3732. for (var _i8 = 0; _i8 < this.children.length; _i8 += 3) if (this.children[_i8] != other.children[_i8] || this.children[_i8 + 1] != other.children[_i8 + 1] || !this.children[_i8 + 2].eq(other.children[_i8 + 2])) return false;
  3733. return true;
  3734. }
  3735. }, {
  3736. key: "locals",
  3737. value: function locals(node) {
  3738. return removeOverlap(this.localsInner(node));
  3739. }
  3740. }, {
  3741. key: "localsInner",
  3742. value: function localsInner(node) {
  3743. if (this == empty) return none;
  3744. if (node.inlineContent || !this.local.some(InlineType.is)) return this.local;
  3745. var result = [];
  3746. for (var i = 0; i < this.local.length; i++) {
  3747. if (!(this.local[i].type instanceof InlineType)) result.push(this.local[i]);
  3748. }
  3749. return result;
  3750. }
  3751. }], [{
  3752. key: "create",
  3753. value: function create(doc, decorations) {
  3754. return decorations.length ? buildTree(decorations, doc, 0, noSpec) : empty;
  3755. }
  3756. }]);
  3757. return DecorationSet;
  3758. }();
  3759. DecorationSet.empty = new DecorationSet([], []);
  3760. DecorationSet.removeOverlap = removeOverlap;
  3761. var empty = DecorationSet.empty;
  3762. var DecorationGroup = function () {
  3763. function DecorationGroup(members) {
  3764. _classCallCheck(this, DecorationGroup);
  3765. this.members = members;
  3766. }
  3767. _createClass(DecorationGroup, [{
  3768. key: "map",
  3769. value: function map(mapping, doc) {
  3770. var mappedDecos = this.members.map(function (member) {
  3771. return member.map(mapping, doc, noSpec);
  3772. });
  3773. return DecorationGroup.from(mappedDecos);
  3774. }
  3775. }, {
  3776. key: "forChild",
  3777. value: function forChild(offset, child) {
  3778. if (child.isLeaf) return DecorationSet.empty;
  3779. var found = [];
  3780. for (var i = 0; i < this.members.length; i++) {
  3781. var result = this.members[i].forChild(offset, child);
  3782. if (result == empty) continue;
  3783. if (result instanceof DecorationGroup) found = found.concat(result.members);else found.push(result);
  3784. }
  3785. return DecorationGroup.from(found);
  3786. }
  3787. }, {
  3788. key: "eq",
  3789. value: function eq(other) {
  3790. if (!(other instanceof DecorationGroup) || other.members.length != this.members.length) return false;
  3791. for (var i = 0; i < this.members.length; i++) if (!this.members[i].eq(other.members[i])) return false;
  3792. return true;
  3793. }
  3794. }, {
  3795. key: "locals",
  3796. value: function locals(node) {
  3797. var result,
  3798. sorted = true;
  3799. for (var i = 0; i < this.members.length; i++) {
  3800. var locals = this.members[i].localsInner(node);
  3801. if (!locals.length) continue;
  3802. if (!result) {
  3803. result = locals;
  3804. } else {
  3805. if (sorted) {
  3806. result = result.slice();
  3807. sorted = false;
  3808. }
  3809. for (var j = 0; j < locals.length; j++) result.push(locals[j]);
  3810. }
  3811. }
  3812. return result ? removeOverlap(sorted ? result : result.sort(byPos)) : none;
  3813. }
  3814. }], [{
  3815. key: "from",
  3816. value: function from(members) {
  3817. switch (members.length) {
  3818. case 0:
  3819. return empty;
  3820. case 1:
  3821. return members[0];
  3822. default:
  3823. return new DecorationGroup(members.every(function (m) {
  3824. return m instanceof DecorationSet;
  3825. }) ? members : members.reduce(function (r, m) {
  3826. return r.concat(m instanceof DecorationSet ? m : m.members);
  3827. }, []));
  3828. }
  3829. }
  3830. }]);
  3831. return DecorationGroup;
  3832. }();
  3833. function mapChildren(oldChildren, newLocal, mapping, node, offset, oldOffset, options) {
  3834. var children = oldChildren.slice();
  3835. var _loop4 = function _loop4(_baseOffset) {
  3836. var moved = 0;
  3837. mapping.maps[i].forEach(function (oldStart, oldEnd, newStart, newEnd) {
  3838. var dSize = newEnd - newStart - (oldEnd - oldStart);
  3839. for (var _i12 = 0; _i12 < children.length; _i12 += 3) {
  3840. var end = children[_i12 + 1];
  3841. if (end < 0 || oldStart > end + _baseOffset - moved) continue;
  3842. var start = children[_i12] + _baseOffset - moved;
  3843. if (oldEnd >= start) {
  3844. children[_i12 + 1] = oldStart <= start ? -2 : -1;
  3845. } else if (oldStart >= _baseOffset && dSize) {
  3846. children[_i12] += dSize;
  3847. children[_i12 + 1] += dSize;
  3848. }
  3849. }
  3850. moved += dSize;
  3851. });
  3852. _baseOffset = mapping.maps[i].map(_baseOffset, -1);
  3853. baseOffset = _baseOffset;
  3854. };
  3855. for (var i = 0, baseOffset = oldOffset; i < mapping.maps.length; i++) {
  3856. _loop4(baseOffset);
  3857. }
  3858. var mustRebuild = false;
  3859. for (var _i9 = 0; _i9 < children.length; _i9 += 3) if (children[_i9 + 1] < 0) {
  3860. if (children[_i9 + 1] == -2) {
  3861. mustRebuild = true;
  3862. children[_i9 + 1] = -1;
  3863. continue;
  3864. }
  3865. var from = mapping.map(oldChildren[_i9] + oldOffset),
  3866. fromLocal = from - offset;
  3867. if (fromLocal < 0 || fromLocal >= node.content.size) {
  3868. mustRebuild = true;
  3869. continue;
  3870. }
  3871. var to = mapping.map(oldChildren[_i9 + 1] + oldOffset, -1),
  3872. toLocal = to - offset;
  3873. var _node$content$findInd2 = node.content.findIndex(fromLocal),
  3874. index = _node$content$findInd2.index,
  3875. childOffset = _node$content$findInd2.offset;
  3876. var childNode = node.maybeChild(index);
  3877. if (childNode && childOffset == fromLocal && childOffset + childNode.nodeSize == toLocal) {
  3878. var mapped = children[_i9 + 2].mapInner(mapping, childNode, from + 1, oldChildren[_i9] + oldOffset + 1, options);
  3879. if (mapped != empty) {
  3880. children[_i9] = fromLocal;
  3881. children[_i9 + 1] = toLocal;
  3882. children[_i9 + 2] = mapped;
  3883. } else {
  3884. children[_i9 + 1] = -2;
  3885. mustRebuild = true;
  3886. }
  3887. } else {
  3888. mustRebuild = true;
  3889. }
  3890. }
  3891. if (mustRebuild) {
  3892. var decorations = mapAndGatherRemainingDecorations(children, oldChildren, newLocal, mapping, offset, oldOffset, options);
  3893. var built = buildTree(decorations, node, 0, options);
  3894. newLocal = built.local;
  3895. for (var _i10 = 0; _i10 < children.length; _i10 += 3) if (children[_i10 + 1] < 0) {
  3896. children.splice(_i10, 3);
  3897. _i10 -= 3;
  3898. }
  3899. for (var _i11 = 0, j = 0; _i11 < built.children.length; _i11 += 3) {
  3900. var _from2 = built.children[_i11];
  3901. while (j < children.length && children[j] < _from2) j += 3;
  3902. children.splice(j, 0, built.children[_i11], built.children[_i11 + 1], built.children[_i11 + 2]);
  3903. }
  3904. }
  3905. return new DecorationSet(newLocal.sort(byPos), children);
  3906. }
  3907. function moveSpans(spans, offset) {
  3908. if (!offset || !spans.length) return spans;
  3909. var result = [];
  3910. for (var i = 0; i < spans.length; i++) {
  3911. var span = spans[i];
  3912. result.push(new Decoration(span.from + offset, span.to + offset, span.type));
  3913. }
  3914. return result;
  3915. }
  3916. function mapAndGatherRemainingDecorations(children, oldChildren, decorations, mapping, offset, oldOffset, options) {
  3917. function gather(set, oldOffset) {
  3918. for (var i = 0; i < set.local.length; i++) {
  3919. var mapped = set.local[i].map(mapping, offset, oldOffset);
  3920. if (mapped) decorations.push(mapped);else if (options.onRemove) options.onRemove(set.local[i].spec);
  3921. }
  3922. for (var _i13 = 0; _i13 < set.children.length; _i13 += 3) gather(set.children[_i13 + 2], set.children[_i13] + oldOffset + 1);
  3923. }
  3924. for (var i = 0; i < children.length; i += 3) if (children[i + 1] == -1) gather(children[i + 2], oldChildren[i] + oldOffset + 1);
  3925. return decorations;
  3926. }
  3927. function takeSpansForNode(spans, node, offset) {
  3928. if (node.isLeaf) return null;
  3929. var end = offset + node.nodeSize,
  3930. found = null;
  3931. for (var i = 0, span; i < spans.length; i++) {
  3932. if ((span = spans[i]) && span.from > offset && span.to < end) {
  3933. (found || (found = [])).push(span);
  3934. spans[i] = null;
  3935. }
  3936. }
  3937. return found;
  3938. }
  3939. function withoutNulls(array) {
  3940. var result = [];
  3941. for (var i = 0; i < array.length; i++) if (array[i] != null) result.push(array[i]);
  3942. return result;
  3943. }
  3944. function buildTree(spans, node, offset, options) {
  3945. var children = [],
  3946. hasNulls = false;
  3947. node.forEach(function (childNode, localStart) {
  3948. var found = takeSpansForNode(spans, childNode, localStart + offset);
  3949. if (found) {
  3950. hasNulls = true;
  3951. var subtree = buildTree(found, childNode, offset + localStart + 1, options);
  3952. if (subtree != empty) children.push(localStart, localStart + childNode.nodeSize, subtree);
  3953. }
  3954. });
  3955. var locals = moveSpans(hasNulls ? withoutNulls(spans) : spans, -offset).sort(byPos);
  3956. for (var i = 0; i < locals.length; i++) if (!locals[i].type.valid(node, locals[i])) {
  3957. if (options.onRemove) options.onRemove(locals[i].spec);
  3958. locals.splice(i--, 1);
  3959. }
  3960. return locals.length || children.length ? new DecorationSet(locals, children) : empty;
  3961. }
  3962. function byPos(a, b) {
  3963. return a.from - b.from || a.to - b.to;
  3964. }
  3965. function removeOverlap(spans) {
  3966. var working = spans;
  3967. for (var i = 0; i < working.length - 1; i++) {
  3968. var span = working[i];
  3969. if (span.from != span.to) for (var j = i + 1; j < working.length; j++) {
  3970. var next = working[j];
  3971. if (next.from == span.from) {
  3972. if (next.to != span.to) {
  3973. if (working == spans) working = spans.slice();
  3974. working[j] = next.copy(next.from, span.to);
  3975. insertAhead(working, j + 1, next.copy(span.to, next.to));
  3976. }
  3977. continue;
  3978. } else {
  3979. if (next.from < span.to) {
  3980. if (working == spans) working = spans.slice();
  3981. working[i] = span.copy(span.from, next.from);
  3982. insertAhead(working, j, span.copy(next.from, span.to));
  3983. }
  3984. break;
  3985. }
  3986. }
  3987. }
  3988. return working;
  3989. }
  3990. function insertAhead(array, i, deco) {
  3991. while (i < array.length && byPos(deco, array[i]) > 0) i++;
  3992. array.splice(i, 0, deco);
  3993. }
  3994. function viewDecorations(view) {
  3995. var found = [];
  3996. view.someProp("decorations", function (f) {
  3997. var result = f(view.state);
  3998. if (result && result != empty) found.push(result);
  3999. });
  4000. if (view.cursorWrapper) found.push(DecorationSet.create(view.state.doc, [view.cursorWrapper.deco]));
  4001. return DecorationGroup.from(found);
  4002. }
  4003. var observeOptions = {
  4004. childList: true,
  4005. characterData: true,
  4006. characterDataOldValue: true,
  4007. attributes: true,
  4008. attributeOldValue: true,
  4009. subtree: true
  4010. };
  4011. var useCharData = ie && ie_version <= 11;
  4012. var SelectionState = function () {
  4013. function SelectionState() {
  4014. _classCallCheck(this, SelectionState);
  4015. this.anchorNode = null;
  4016. this.anchorOffset = 0;
  4017. this.focusNode = null;
  4018. this.focusOffset = 0;
  4019. }
  4020. _createClass(SelectionState, [{
  4021. key: "set",
  4022. value: function set(sel) {
  4023. this.anchorNode = sel.anchorNode;
  4024. this.anchorOffset = sel.anchorOffset;
  4025. this.focusNode = sel.focusNode;
  4026. this.focusOffset = sel.focusOffset;
  4027. }
  4028. }, {
  4029. key: "clear",
  4030. value: function clear() {
  4031. this.anchorNode = this.focusNode = null;
  4032. }
  4033. }, {
  4034. key: "eq",
  4035. value: function eq(sel) {
  4036. return sel.anchorNode == this.anchorNode && sel.anchorOffset == this.anchorOffset && sel.focusNode == this.focusNode && sel.focusOffset == this.focusOffset;
  4037. }
  4038. }]);
  4039. return SelectionState;
  4040. }();
  4041. var DOMObserver = function () {
  4042. function DOMObserver(view, handleDOMChange) {
  4043. var _this11 = this;
  4044. _classCallCheck(this, DOMObserver);
  4045. this.view = view;
  4046. this.handleDOMChange = handleDOMChange;
  4047. this.queue = [];
  4048. this.flushingSoon = -1;
  4049. this.observer = null;
  4050. this.currentSelection = new SelectionState();
  4051. this.onCharData = null;
  4052. this.suppressingSelectionUpdates = false;
  4053. this.observer = window.MutationObserver && new window.MutationObserver(function (mutations) {
  4054. for (var i = 0; i < mutations.length; i++) _this11.queue.push(mutations[i]);
  4055. if (ie && ie_version <= 11 && mutations.some(function (m) {
  4056. return m.type == "childList" && m.removedNodes.length || m.type == "characterData" && m.oldValue.length > m.target.nodeValue.length;
  4057. })) _this11.flushSoon();else _this11.flush();
  4058. });
  4059. if (useCharData) {
  4060. this.onCharData = function (e) {
  4061. _this11.queue.push({
  4062. target: e.target,
  4063. type: "characterData",
  4064. oldValue: e.prevValue
  4065. });
  4066. _this11.flushSoon();
  4067. };
  4068. }
  4069. this.onSelectionChange = this.onSelectionChange.bind(this);
  4070. }
  4071. _createClass(DOMObserver, [{
  4072. key: "flushSoon",
  4073. value: function flushSoon() {
  4074. var _this12 = this;
  4075. if (this.flushingSoon < 0) this.flushingSoon = window.setTimeout(function () {
  4076. _this12.flushingSoon = -1;
  4077. _this12.flush();
  4078. }, 20);
  4079. }
  4080. }, {
  4081. key: "forceFlush",
  4082. value: function forceFlush() {
  4083. if (this.flushingSoon > -1) {
  4084. window.clearTimeout(this.flushingSoon);
  4085. this.flushingSoon = -1;
  4086. this.flush();
  4087. }
  4088. }
  4089. }, {
  4090. key: "start",
  4091. value: function start() {
  4092. if (this.observer) {
  4093. this.observer.takeRecords();
  4094. this.observer.observe(this.view.dom, observeOptions);
  4095. }
  4096. if (this.onCharData) this.view.dom.addEventListener("DOMCharacterDataModified", this.onCharData);
  4097. this.connectSelection();
  4098. }
  4099. }, {
  4100. key: "stop",
  4101. value: function stop() {
  4102. var _this13 = this;
  4103. if (this.observer) {
  4104. var take = this.observer.takeRecords();
  4105. if (take.length) {
  4106. for (var i = 0; i < take.length; i++) this.queue.push(take[i]);
  4107. window.setTimeout(function () {
  4108. return _this13.flush();
  4109. }, 20);
  4110. }
  4111. this.observer.disconnect();
  4112. }
  4113. if (this.onCharData) this.view.dom.removeEventListener("DOMCharacterDataModified", this.onCharData);
  4114. this.disconnectSelection();
  4115. }
  4116. }, {
  4117. key: "connectSelection",
  4118. value: function connectSelection() {
  4119. this.view.dom.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
  4120. }
  4121. }, {
  4122. key: "disconnectSelection",
  4123. value: function disconnectSelection() {
  4124. this.view.dom.ownerDocument.removeEventListener("selectionchange", this.onSelectionChange);
  4125. }
  4126. }, {
  4127. key: "suppressSelectionUpdates",
  4128. value: function suppressSelectionUpdates() {
  4129. var _this14 = this;
  4130. this.suppressingSelectionUpdates = true;
  4131. setTimeout(function () {
  4132. return _this14.suppressingSelectionUpdates = false;
  4133. }, 50);
  4134. }
  4135. }, {
  4136. key: "onSelectionChange",
  4137. value: function onSelectionChange() {
  4138. if (!hasFocusAndSelection(this.view)) return;
  4139. if (this.suppressingSelectionUpdates) return selectionToDOM(this.view);
  4140. if (ie && ie_version <= 11 && !this.view.state.selection.empty) {
  4141. var sel = this.view.domSelectionRange();
  4142. if (sel.focusNode && isEquivalentPosition(sel.focusNode, sel.focusOffset, sel.anchorNode, sel.anchorOffset)) return this.flushSoon();
  4143. }
  4144. this.flush();
  4145. }
  4146. }, {
  4147. key: "setCurSelection",
  4148. value: function setCurSelection() {
  4149. this.currentSelection.set(this.view.domSelectionRange());
  4150. }
  4151. }, {
  4152. key: "ignoreSelectionChange",
  4153. value: function ignoreSelectionChange(sel) {
  4154. if (!sel.focusNode) return true;
  4155. var ancestors = new Set(),
  4156. container;
  4157. for (var scan = sel.focusNode; scan; scan = parentNode(scan)) ancestors.add(scan);
  4158. for (var _scan = sel.anchorNode; _scan; _scan = parentNode(_scan)) if (ancestors.has(_scan)) {
  4159. container = _scan;
  4160. break;
  4161. }
  4162. var desc = container && this.view.docView.nearestDesc(container);
  4163. if (desc && desc.ignoreMutation({
  4164. type: "selection",
  4165. target: container.nodeType == 3 ? container.parentNode : container
  4166. })) {
  4167. this.setCurSelection();
  4168. return true;
  4169. }
  4170. }
  4171. }, {
  4172. key: "pendingRecords",
  4173. value: function pendingRecords() {
  4174. if (this.observer) {
  4175. var _iterator2 = _createForOfIteratorHelper(this.observer.takeRecords()),
  4176. _step2;
  4177. try {
  4178. for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
  4179. var mut = _step2.value;
  4180. this.queue.push(mut);
  4181. }
  4182. } catch (err) {
  4183. _iterator2.e(err);
  4184. } finally {
  4185. _iterator2.f();
  4186. }
  4187. }
  4188. return this.queue;
  4189. }
  4190. }, {
  4191. key: "flush",
  4192. value: function flush() {
  4193. var view = this.view;
  4194. if (!view.docView || this.flushingSoon > -1) return;
  4195. var mutations = this.pendingRecords();
  4196. if (mutations.length) this.queue = [];
  4197. var sel = view.domSelectionRange();
  4198. var newSel = !this.suppressingSelectionUpdates && !this.currentSelection.eq(sel) && hasFocusAndSelection(view) && !this.ignoreSelectionChange(sel);
  4199. var from = -1,
  4200. to = -1,
  4201. typeOver = false,
  4202. added = [];
  4203. if (view.editable) {
  4204. for (var i = 0; i < mutations.length; i++) {
  4205. var result = this.registerMutation(mutations[i], added);
  4206. if (result) {
  4207. from = from < 0 ? result.from : Math.min(result.from, from);
  4208. to = to < 0 ? result.to : Math.max(result.to, to);
  4209. if (result.typeOver) typeOver = true;
  4210. }
  4211. }
  4212. }
  4213. if (gecko && added.length > 1) {
  4214. var brs = added.filter(function (n) {
  4215. return n.nodeName == "BR";
  4216. });
  4217. if (brs.length == 2) {
  4218. var a = brs[0],
  4219. b = brs[1];
  4220. if (a.parentNode && a.parentNode.parentNode == b.parentNode) b.remove();else a.remove();
  4221. }
  4222. }
  4223. var readSel = null;
  4224. if (from < 0 && newSel && view.input.lastFocus > Date.now() - 200 && Math.max(view.input.lastTouch, view.input.lastClick.time) < Date.now() - 300 && selectionCollapsed(sel) && (readSel = selectionFromDOM(view)) && readSel.eq(prosemirrorState.Selection.near(view.state.doc.resolve(0), 1))) {
  4225. view.input.lastFocus = 0;
  4226. selectionToDOM(view);
  4227. this.currentSelection.set(sel);
  4228. view.scrollToSelection();
  4229. } else if (from > -1 || newSel) {
  4230. if (from > -1) {
  4231. view.docView.markDirty(from, to);
  4232. checkCSS(view);
  4233. }
  4234. this.handleDOMChange(from, to, typeOver, added);
  4235. if (view.docView && view.docView.dirty) view.updateState(view.state);else if (!this.currentSelection.eq(sel)) selectionToDOM(view);
  4236. this.currentSelection.set(sel);
  4237. }
  4238. }
  4239. }, {
  4240. key: "registerMutation",
  4241. value: function registerMutation(mut, added) {
  4242. if (added.indexOf(mut.target) > -1) return null;
  4243. var desc = this.view.docView.nearestDesc(mut.target);
  4244. if (mut.type == "attributes" && (desc == this.view.docView || mut.attributeName == "contenteditable" || mut.attributeName == "style" && !mut.oldValue && !mut.target.getAttribute("style"))) return null;
  4245. if (!desc || desc.ignoreMutation(mut)) return null;
  4246. if (mut.type == "childList") {
  4247. for (var i = 0; i < mut.addedNodes.length; i++) added.push(mut.addedNodes[i]);
  4248. if (desc.contentDOM && desc.contentDOM != desc.dom && !desc.contentDOM.contains(mut.target)) return {
  4249. from: desc.posBefore,
  4250. to: desc.posAfter
  4251. };
  4252. var prev = mut.previousSibling,
  4253. next = mut.nextSibling;
  4254. if (ie && ie_version <= 11 && mut.addedNodes.length) {
  4255. for (var _i14 = 0; _i14 < mut.addedNodes.length; _i14++) {
  4256. var _mut$addedNodes$_i = mut.addedNodes[_i14],
  4257. previousSibling = _mut$addedNodes$_i.previousSibling,
  4258. nextSibling = _mut$addedNodes$_i.nextSibling;
  4259. if (!previousSibling || Array.prototype.indexOf.call(mut.addedNodes, previousSibling) < 0) prev = previousSibling;
  4260. if (!nextSibling || Array.prototype.indexOf.call(mut.addedNodes, nextSibling) < 0) next = nextSibling;
  4261. }
  4262. }
  4263. var fromOffset = prev && prev.parentNode == mut.target ? domIndex(prev) + 1 : 0;
  4264. var from = desc.localPosFromDOM(mut.target, fromOffset, -1);
  4265. var toOffset = next && next.parentNode == mut.target ? domIndex(next) : mut.target.childNodes.length;
  4266. var to = desc.localPosFromDOM(mut.target, toOffset, 1);
  4267. return {
  4268. from: from,
  4269. to: to
  4270. };
  4271. } else if (mut.type == "attributes") {
  4272. return {
  4273. from: desc.posAtStart - desc.border,
  4274. to: desc.posAtEnd + desc.border
  4275. };
  4276. } else {
  4277. return {
  4278. from: desc.posAtStart,
  4279. to: desc.posAtEnd,
  4280. typeOver: mut.target.nodeValue == mut.oldValue
  4281. };
  4282. }
  4283. }
  4284. }]);
  4285. return DOMObserver;
  4286. }();
  4287. var cssChecked = new WeakMap();
  4288. var cssCheckWarned = false;
  4289. function checkCSS(view) {
  4290. if (cssChecked.has(view)) return;
  4291. cssChecked.set(view, null);
  4292. if (['normal', 'nowrap', 'pre-line'].indexOf(getComputedStyle(view.dom).whiteSpace) !== -1) {
  4293. view.requiresGeckoHackNode = gecko;
  4294. if (cssCheckWarned) return;
  4295. console["warn"]("ProseMirror expects the CSS white-space property to be set, preferably to 'pre-wrap'. It is recommended to load style/prosemirror.css from the prosemirror-view package.");
  4296. cssCheckWarned = true;
  4297. }
  4298. }
  4299. function safariShadowSelectionRange(view) {
  4300. var found;
  4301. function read(event) {
  4302. event.preventDefault();
  4303. event.stopImmediatePropagation();
  4304. found = event.getTargetRanges()[0];
  4305. }
  4306. view.dom.addEventListener("beforeinput", read, true);
  4307. document.execCommand("indent");
  4308. view.dom.removeEventListener("beforeinput", read, true);
  4309. var anchorNode = found.startContainer,
  4310. anchorOffset = found.startOffset;
  4311. var focusNode = found.endContainer,
  4312. focusOffset = found.endOffset;
  4313. var currentAnchor = view.domAtPos(view.state.selection.anchor);
  4314. if (isEquivalentPosition(currentAnchor.node, currentAnchor.offset, focusNode, focusOffset)) {
  4315. var _ref3 = [focusNode, focusOffset, anchorNode, anchorOffset];
  4316. anchorNode = _ref3[0];
  4317. anchorOffset = _ref3[1];
  4318. focusNode = _ref3[2];
  4319. focusOffset = _ref3[3];
  4320. }
  4321. return {
  4322. anchorNode: anchorNode,
  4323. anchorOffset: anchorOffset,
  4324. focusNode: focusNode,
  4325. focusOffset: focusOffset
  4326. };
  4327. }
  4328. function parseBetween(view, from_, to_) {
  4329. var _view$docView$parseRa = view.docView.parseRange(from_, to_),
  4330. parent = _view$docView$parseRa.node,
  4331. fromOffset = _view$docView$parseRa.fromOffset,
  4332. toOffset = _view$docView$parseRa.toOffset,
  4333. from = _view$docView$parseRa.from,
  4334. to = _view$docView$parseRa.to;
  4335. var domSel = view.domSelectionRange();
  4336. var find;
  4337. var anchor = domSel.anchorNode;
  4338. if (anchor && view.dom.contains(anchor.nodeType == 1 ? anchor : anchor.parentNode)) {
  4339. find = [{
  4340. node: anchor,
  4341. offset: domSel.anchorOffset
  4342. }];
  4343. if (!selectionCollapsed(domSel)) find.push({
  4344. node: domSel.focusNode,
  4345. offset: domSel.focusOffset
  4346. });
  4347. }
  4348. if (chrome && view.input.lastKeyCode === 8) {
  4349. for (var off = toOffset; off > fromOffset; off--) {
  4350. var node = parent.childNodes[off - 1],
  4351. desc = node.pmViewDesc;
  4352. if (node.nodeName == "BR" && !desc) {
  4353. toOffset = off;
  4354. break;
  4355. }
  4356. if (!desc || desc.size) break;
  4357. }
  4358. }
  4359. var startDoc = view.state.doc;
  4360. var parser = view.someProp("domParser") || prosemirrorModel.DOMParser.fromSchema(view.state.schema);
  4361. var $from = startDoc.resolve(from);
  4362. var sel = null,
  4363. doc = parser.parse(parent, {
  4364. topNode: $from.parent,
  4365. topMatch: $from.parent.contentMatchAt($from.index()),
  4366. topOpen: true,
  4367. from: fromOffset,
  4368. to: toOffset,
  4369. preserveWhitespace: $from.parent.type.whitespace == "pre" ? "full" : true,
  4370. findPositions: find,
  4371. ruleFromNode: ruleFromNode,
  4372. context: $from
  4373. });
  4374. if (find && find[0].pos != null) {
  4375. var _anchor = find[0].pos,
  4376. head = find[1] && find[1].pos;
  4377. if (head == null) head = _anchor;
  4378. sel = {
  4379. anchor: _anchor + from,
  4380. head: head + from
  4381. };
  4382. }
  4383. return {
  4384. doc: doc,
  4385. sel: sel,
  4386. from: from,
  4387. to: to
  4388. };
  4389. }
  4390. function ruleFromNode(dom) {
  4391. var desc = dom.pmViewDesc;
  4392. if (desc) {
  4393. return desc.parseRule();
  4394. } else if (dom.nodeName == "BR" && dom.parentNode) {
  4395. if (safari && /^(ul|ol)$/i.test(dom.parentNode.nodeName)) {
  4396. var skip = document.createElement("div");
  4397. skip.appendChild(document.createElement("li"));
  4398. return {
  4399. skip: skip
  4400. };
  4401. } else if (dom.parentNode.lastChild == dom || safari && /^(tr|table)$/i.test(dom.parentNode.nodeName)) {
  4402. return {
  4403. ignore: true
  4404. };
  4405. }
  4406. } else if (dom.nodeName == "IMG" && dom.getAttribute("mark-placeholder")) {
  4407. return {
  4408. ignore: true
  4409. };
  4410. }
  4411. return null;
  4412. }
  4413. var isInline = /^(a|abbr|acronym|b|bd[io]|big|br|button|cite|code|data(list)?|del|dfn|em|i|ins|kbd|label|map|mark|meter|output|q|ruby|s|samp|small|span|strong|su[bp]|time|u|tt|var)$/i;
  4414. function readDOMChange(view, from, to, typeOver, addedNodes) {
  4415. var compositionID = view.input.compositionPendingChanges || (view.composing ? view.input.compositionID : 0);
  4416. view.input.compositionPendingChanges = 0;
  4417. if (from < 0) {
  4418. var origin = view.input.lastSelectionTime > Date.now() - 50 ? view.input.lastSelectionOrigin : null;
  4419. var newSel = selectionFromDOM(view, origin);
  4420. if (newSel && !view.state.selection.eq(newSel)) {
  4421. if (chrome && android && view.input.lastKeyCode === 13 && Date.now() - 100 < view.input.lastKeyCodeTime && view.someProp("handleKeyDown", function (f) {
  4422. return f(view, keyEvent(13, "Enter"));
  4423. })) return;
  4424. var _tr = view.state.tr.setSelection(newSel);
  4425. if (origin == "pointer") _tr.setMeta("pointer", true);else if (origin == "key") _tr.scrollIntoView();
  4426. if (compositionID) _tr.setMeta("composition", compositionID);
  4427. view.dispatch(_tr);
  4428. }
  4429. return;
  4430. }
  4431. var $before = view.state.doc.resolve(from);
  4432. var shared = $before.sharedDepth(to);
  4433. from = $before.before(shared + 1);
  4434. to = view.state.doc.resolve(to).after(shared + 1);
  4435. var sel = view.state.selection;
  4436. var parse = parseBetween(view, from, to);
  4437. var doc = view.state.doc,
  4438. compare = doc.slice(parse.from, parse.to);
  4439. var preferredPos, preferredSide;
  4440. if (view.input.lastKeyCode === 8 && Date.now() - 100 < view.input.lastKeyCodeTime) {
  4441. preferredPos = view.state.selection.to;
  4442. preferredSide = "end";
  4443. } else {
  4444. preferredPos = view.state.selection.from;
  4445. preferredSide = "start";
  4446. }
  4447. view.input.lastKeyCode = null;
  4448. var change = findDiff(compare.content, parse.doc.content, parse.from, preferredPos, preferredSide);
  4449. if ((ios && view.input.lastIOSEnter > Date.now() - 225 || android) && addedNodes.some(function (n) {
  4450. return n.nodeType == 1 && !isInline.test(n.nodeName);
  4451. }) && (!change || change.endA >= change.endB) && view.someProp("handleKeyDown", function (f) {
  4452. return f(view, keyEvent(13, "Enter"));
  4453. })) {
  4454. view.input.lastIOSEnter = 0;
  4455. return;
  4456. }
  4457. if (!change) {
  4458. if (typeOver && sel instanceof prosemirrorState.TextSelection && !sel.empty && sel.$head.sameParent(sel.$anchor) && !view.composing && !(parse.sel && parse.sel.anchor != parse.sel.head)) {
  4459. change = {
  4460. start: sel.from,
  4461. endA: sel.to,
  4462. endB: sel.to
  4463. };
  4464. } else {
  4465. if (parse.sel) {
  4466. var _sel = resolveSelection(view, view.state.doc, parse.sel);
  4467. if (_sel && !_sel.eq(view.state.selection)) {
  4468. var _tr2 = view.state.tr.setSelection(_sel);
  4469. if (compositionID) _tr2.setMeta("composition", compositionID);
  4470. view.dispatch(_tr2);
  4471. }
  4472. }
  4473. return;
  4474. }
  4475. }
  4476. if (chrome && view.cursorWrapper && parse.sel && parse.sel.anchor == view.cursorWrapper.deco.from && parse.sel.head == parse.sel.anchor) {
  4477. var size = change.endB - change.start;
  4478. parse.sel = {
  4479. anchor: parse.sel.anchor + size,
  4480. head: parse.sel.anchor + size
  4481. };
  4482. }
  4483. view.input.domChangeCount++;
  4484. if (view.state.selection.from < view.state.selection.to && change.start == change.endB && view.state.selection instanceof prosemirrorState.TextSelection) {
  4485. if (change.start > view.state.selection.from && change.start <= view.state.selection.from + 2 && view.state.selection.from >= parse.from) {
  4486. change.start = view.state.selection.from;
  4487. } else if (change.endA < view.state.selection.to && change.endA >= view.state.selection.to - 2 && view.state.selection.to <= parse.to) {
  4488. change.endB += view.state.selection.to - change.endA;
  4489. change.endA = view.state.selection.to;
  4490. }
  4491. }
  4492. if (ie && ie_version <= 11 && change.endB == change.start + 1 && change.endA == change.start && change.start > parse.from && parse.doc.textBetween(change.start - parse.from - 1, change.start - parse.from + 1) == " \xA0") {
  4493. change.start--;
  4494. change.endA--;
  4495. change.endB--;
  4496. }
  4497. var $from = parse.doc.resolveNoCache(change.start - parse.from);
  4498. var $to = parse.doc.resolveNoCache(change.endB - parse.from);
  4499. var $fromA = doc.resolve(change.start);
  4500. var inlineChange = $from.sameParent($to) && $from.parent.inlineContent && $fromA.end() >= change.endA;
  4501. var nextSel;
  4502. if ((ios && view.input.lastIOSEnter > Date.now() - 225 && (!inlineChange || addedNodes.some(function (n) {
  4503. return n.nodeName == "DIV" || n.nodeName == "P";
  4504. })) || !inlineChange && $from.pos < parse.doc.content.size && !$from.sameParent($to) && (nextSel = prosemirrorState.Selection.findFrom(parse.doc.resolve($from.pos + 1), 1, true)) && nextSel.head == $to.pos) && view.someProp("handleKeyDown", function (f) {
  4505. return f(view, keyEvent(13, "Enter"));
  4506. })) {
  4507. view.input.lastIOSEnter = 0;
  4508. return;
  4509. }
  4510. if (view.state.selection.anchor > change.start && looksLikeJoin(doc, change.start, change.endA, $from, $to) && view.someProp("handleKeyDown", function (f) {
  4511. return f(view, keyEvent(8, "Backspace"));
  4512. })) {
  4513. if (android && chrome) view.domObserver.suppressSelectionUpdates();
  4514. return;
  4515. }
  4516. if (chrome && android && change.endB == change.start) view.input.lastAndroidDelete = Date.now();
  4517. if (android && !inlineChange && $from.start() != $to.start() && $to.parentOffset == 0 && $from.depth == $to.depth && parse.sel && parse.sel.anchor == parse.sel.head && parse.sel.head == change.endA) {
  4518. change.endB -= 2;
  4519. $to = parse.doc.resolveNoCache(change.endB - parse.from);
  4520. setTimeout(function () {
  4521. view.someProp("handleKeyDown", function (f) {
  4522. return f(view, keyEvent(13, "Enter"));
  4523. });
  4524. }, 20);
  4525. }
  4526. var chFrom = change.start,
  4527. chTo = change.endA;
  4528. var tr, storedMarks, markChange;
  4529. if (inlineChange) {
  4530. if ($from.pos == $to.pos) {
  4531. if (ie && ie_version <= 11 && $from.parentOffset == 0) {
  4532. view.domObserver.suppressSelectionUpdates();
  4533. setTimeout(function () {
  4534. return selectionToDOM(view);
  4535. }, 20);
  4536. }
  4537. tr = view.state.tr["delete"](chFrom, chTo);
  4538. storedMarks = doc.resolve(change.start).marksAcross(doc.resolve(change.endA));
  4539. } else if (change.endA == change.endB && (markChange = isMarkChange($from.parent.content.cut($from.parentOffset, $to.parentOffset), $fromA.parent.content.cut($fromA.parentOffset, change.endA - $fromA.start())))) {
  4540. tr = view.state.tr;
  4541. if (markChange.type == "add") tr.addMark(chFrom, chTo, markChange.mark);else tr.removeMark(chFrom, chTo, markChange.mark);
  4542. } else if ($from.parent.child($from.index()).isText && $from.index() == $to.index() - ($to.textOffset ? 0 : 1)) {
  4543. var text = $from.parent.textBetween($from.parentOffset, $to.parentOffset);
  4544. if (view.someProp("handleTextInput", function (f) {
  4545. return f(view, chFrom, chTo, text);
  4546. })) return;
  4547. tr = view.state.tr.insertText(text, chFrom, chTo);
  4548. }
  4549. }
  4550. if (!tr) tr = view.state.tr.replace(chFrom, chTo, parse.doc.slice(change.start - parse.from, change.endB - parse.from));
  4551. if (parse.sel) {
  4552. var _sel2 = resolveSelection(view, tr.doc, parse.sel);
  4553. if (_sel2 && !(chrome && android && view.composing && _sel2.empty && (change.start != change.endB || view.input.lastAndroidDelete < Date.now() - 100) && (_sel2.head == chFrom || _sel2.head == tr.mapping.map(chTo) - 1) || ie && _sel2.empty && _sel2.head == chFrom)) tr.setSelection(_sel2);
  4554. }
  4555. if (storedMarks) tr.ensureMarks(storedMarks);
  4556. if (compositionID) tr.setMeta("composition", compositionID);
  4557. view.dispatch(tr.scrollIntoView());
  4558. }
  4559. function resolveSelection(view, doc, parsedSel) {
  4560. if (Math.max(parsedSel.anchor, parsedSel.head) > doc.content.size) return null;
  4561. return selectionBetween(view, doc.resolve(parsedSel.anchor), doc.resolve(parsedSel.head));
  4562. }
  4563. function isMarkChange(cur, prev) {
  4564. var curMarks = cur.firstChild.marks,
  4565. prevMarks = prev.firstChild.marks;
  4566. var added = curMarks,
  4567. removed = prevMarks,
  4568. type,
  4569. mark,
  4570. update;
  4571. for (var i = 0; i < prevMarks.length; i++) added = prevMarks[i].removeFromSet(added);
  4572. for (var _i15 = 0; _i15 < curMarks.length; _i15++) removed = curMarks[_i15].removeFromSet(removed);
  4573. if (added.length == 1 && removed.length == 0) {
  4574. mark = added[0];
  4575. type = "add";
  4576. update = function update(node) {
  4577. return node.mark(mark.addToSet(node.marks));
  4578. };
  4579. } else if (added.length == 0 && removed.length == 1) {
  4580. mark = removed[0];
  4581. type = "remove";
  4582. update = function update(node) {
  4583. return node.mark(mark.removeFromSet(node.marks));
  4584. };
  4585. } else {
  4586. return null;
  4587. }
  4588. var updated = [];
  4589. for (var _i16 = 0; _i16 < prev.childCount; _i16++) updated.push(update(prev.child(_i16)));
  4590. if (prosemirrorModel.Fragment.from(updated).eq(cur)) return {
  4591. mark: mark,
  4592. type: type
  4593. };
  4594. }
  4595. function looksLikeJoin(old, start, end, $newStart, $newEnd) {
  4596. if (!$newStart.parent.isTextblock || end - start <= $newEnd.pos - $newStart.pos || skipClosingAndOpening($newStart, true, false) < $newEnd.pos) return false;
  4597. var $start = old.resolve(start);
  4598. if ($start.parentOffset < $start.parent.content.size || !$start.parent.isTextblock) return false;
  4599. var $next = old.resolve(skipClosingAndOpening($start, true, true));
  4600. if (!$next.parent.isTextblock || $next.pos > end || skipClosingAndOpening($next, true, false) < end) return false;
  4601. return $newStart.parent.content.cut($newStart.parentOffset).eq($next.parent.content);
  4602. }
  4603. function skipClosingAndOpening($pos, fromEnd, mayOpen) {
  4604. var depth = $pos.depth,
  4605. end = fromEnd ? $pos.end() : $pos.pos;
  4606. while (depth > 0 && (fromEnd || $pos.indexAfter(depth) == $pos.node(depth).childCount)) {
  4607. depth--;
  4608. end++;
  4609. fromEnd = false;
  4610. }
  4611. if (mayOpen) {
  4612. var next = $pos.node(depth).maybeChild($pos.indexAfter(depth));
  4613. while (next && !next.isLeaf) {
  4614. next = next.firstChild;
  4615. end++;
  4616. }
  4617. }
  4618. return end;
  4619. }
  4620. function findDiff(a, b, pos, preferredPos, preferredSide) {
  4621. var start = a.findDiffStart(b, pos);
  4622. if (start == null) return null;
  4623. var _a$findDiffEnd = a.findDiffEnd(b, pos + a.size, pos + b.size),
  4624. endA = _a$findDiffEnd.a,
  4625. endB = _a$findDiffEnd.b;
  4626. if (preferredSide == "end") {
  4627. var adjust = Math.max(0, start - Math.min(endA, endB));
  4628. preferredPos -= endA + adjust - start;
  4629. }
  4630. if (endA < start && a.size < b.size) {
  4631. var move = preferredPos <= start && preferredPos >= endA ? start - preferredPos : 0;
  4632. start -= move;
  4633. if (start && start < b.size && isSurrogatePair(b.textBetween(start - 1, start + 1))) start += move ? 1 : -1;
  4634. endB = start + (endB - endA);
  4635. endA = start;
  4636. } else if (endB < start) {
  4637. var _move = preferredPos <= start && preferredPos >= endB ? start - preferredPos : 0;
  4638. start -= _move;
  4639. if (start && start < a.size && isSurrogatePair(a.textBetween(start - 1, start + 1))) start += _move ? 1 : -1;
  4640. endA = start + (endA - endB);
  4641. endB = start;
  4642. }
  4643. return {
  4644. start: start,
  4645. endA: endA,
  4646. endB: endB
  4647. };
  4648. }
  4649. function isSurrogatePair(str) {
  4650. if (str.length != 2) return false;
  4651. var a = str.charCodeAt(0),
  4652. b = str.charCodeAt(1);
  4653. return a >= 0xDC00 && a <= 0xDFFF && b >= 0xD800 && b <= 0xDBFF;
  4654. }
  4655. var __serializeForClipboard = serializeForClipboard;
  4656. var __parseFromClipboard = parseFromClipboard;
  4657. var __endComposition = endComposition;
  4658. var EditorView = function () {
  4659. function EditorView(place, props) {
  4660. var _this15 = this;
  4661. _classCallCheck(this, EditorView);
  4662. this._root = null;
  4663. this.focused = false;
  4664. this.trackWrites = null;
  4665. this.mounted = false;
  4666. this.markCursor = null;
  4667. this.cursorWrapper = null;
  4668. this.lastSelectedViewDesc = undefined;
  4669. this.input = new InputState();
  4670. this.prevDirectPlugins = [];
  4671. this.pluginViews = [];
  4672. this.requiresGeckoHackNode = false;
  4673. this.dragging = null;
  4674. this._props = props;
  4675. this.state = props.state;
  4676. this.directPlugins = props.plugins || [];
  4677. this.directPlugins.forEach(checkStateComponent);
  4678. this.dispatch = this.dispatch.bind(this);
  4679. this.dom = place && place.mount || document.createElement("div");
  4680. if (place) {
  4681. if (place.appendChild) place.appendChild(this.dom);else if (typeof place == "function") place(this.dom);else if (place.mount) this.mounted = true;
  4682. }
  4683. this.editable = getEditable(this);
  4684. updateCursorWrapper(this);
  4685. this.nodeViews = buildNodeViews(this);
  4686. this.docView = docViewDesc(this.state.doc, computeDocDeco(this), viewDecorations(this), this.dom, this);
  4687. this.domObserver = new DOMObserver(this, function (from, to, typeOver, added) {
  4688. return readDOMChange(_this15, from, to, typeOver, added);
  4689. });
  4690. this.domObserver.start();
  4691. initInput(this);
  4692. this.updatePluginViews();
  4693. }
  4694. _createClass(EditorView, [{
  4695. key: "composing",
  4696. get: function get() {
  4697. return this.input.composing;
  4698. }
  4699. }, {
  4700. key: "props",
  4701. get: function get() {
  4702. if (this._props.state != this.state) {
  4703. var prev = this._props;
  4704. this._props = {};
  4705. for (var name in prev) this._props[name] = prev[name];
  4706. this._props.state = this.state;
  4707. }
  4708. return this._props;
  4709. }
  4710. }, {
  4711. key: "update",
  4712. value: function update(props) {
  4713. if (props.handleDOMEvents != this._props.handleDOMEvents) ensureListeners(this);
  4714. var prevProps = this._props;
  4715. this._props = props;
  4716. if (props.plugins) {
  4717. props.plugins.forEach(checkStateComponent);
  4718. this.directPlugins = props.plugins;
  4719. }
  4720. this.updateStateInner(props.state, prevProps);
  4721. }
  4722. }, {
  4723. key: "setProps",
  4724. value: function setProps(props) {
  4725. var updated = {};
  4726. for (var name in this._props) updated[name] = this._props[name];
  4727. updated.state = this.state;
  4728. for (var _name2 in props) updated[_name2] = props[_name2];
  4729. this.update(updated);
  4730. }
  4731. }, {
  4732. key: "updateState",
  4733. value: function updateState(state) {
  4734. this.updateStateInner(state, this._props);
  4735. }
  4736. }, {
  4737. key: "updateStateInner",
  4738. value: function updateStateInner(state, prevProps) {
  4739. var _a;
  4740. var prev = this.state,
  4741. redraw = false,
  4742. updateSel = false;
  4743. if (state.storedMarks && this.composing) {
  4744. clearComposition(this);
  4745. updateSel = true;
  4746. }
  4747. this.state = state;
  4748. var pluginsChanged = prev.plugins != state.plugins || this._props.plugins != prevProps.plugins;
  4749. if (pluginsChanged || this._props.plugins != prevProps.plugins || this._props.nodeViews != prevProps.nodeViews) {
  4750. var nodeViews = buildNodeViews(this);
  4751. if (changedNodeViews(nodeViews, this.nodeViews)) {
  4752. this.nodeViews = nodeViews;
  4753. redraw = true;
  4754. }
  4755. }
  4756. if (pluginsChanged || prevProps.handleDOMEvents != this._props.handleDOMEvents) {
  4757. ensureListeners(this);
  4758. }
  4759. this.editable = getEditable(this);
  4760. updateCursorWrapper(this);
  4761. var innerDeco = viewDecorations(this),
  4762. outerDeco = computeDocDeco(this);
  4763. var scroll = prev.plugins != state.plugins && !prev.doc.eq(state.doc) ? "reset" : state.scrollToSelection > prev.scrollToSelection ? "to selection" : "preserve";
  4764. var updateDoc = redraw || !this.docView.matchesNode(state.doc, outerDeco, innerDeco);
  4765. if (updateDoc || !state.selection.eq(prev.selection)) updateSel = true;
  4766. var oldScrollPos = scroll == "preserve" && updateSel && this.dom.style.overflowAnchor == null && storeScrollPos(this);
  4767. if (updateSel) {
  4768. this.domObserver.stop();
  4769. var forceSelUpdate = updateDoc && (ie || chrome) && !this.composing && !prev.selection.empty && !state.selection.empty && selectionContextChanged(prev.selection, state.selection);
  4770. if (updateDoc) {
  4771. var chromeKludge = chrome ? this.trackWrites = this.domSelectionRange().focusNode : null;
  4772. if (redraw || !this.docView.update(state.doc, outerDeco, innerDeco, this)) {
  4773. this.docView.updateOuterDeco([]);
  4774. this.docView.destroy();
  4775. this.docView = docViewDesc(state.doc, outerDeco, innerDeco, this.dom, this);
  4776. }
  4777. if (chromeKludge && !this.trackWrites) forceSelUpdate = true;
  4778. }
  4779. if (forceSelUpdate || !(this.input.mouseDown && this.domObserver.currentSelection.eq(this.domSelectionRange()) && anchorInRightPlace(this))) {
  4780. selectionToDOM(this, forceSelUpdate);
  4781. } else {
  4782. syncNodeSelection(this, state.selection);
  4783. this.domObserver.setCurSelection();
  4784. }
  4785. this.domObserver.start();
  4786. }
  4787. this.updatePluginViews(prev);
  4788. if (((_a = this.dragging) === null || _a === void 0 ? void 0 : _a.node) && !prev.doc.eq(state.doc)) this.updateDraggedNode(this.dragging, prev);
  4789. if (scroll == "reset") {
  4790. this.dom.scrollTop = 0;
  4791. } else if (scroll == "to selection") {
  4792. this.scrollToSelection();
  4793. } else if (oldScrollPos) {
  4794. resetScrollPos(oldScrollPos);
  4795. }
  4796. }
  4797. }, {
  4798. key: "scrollToSelection",
  4799. value: function scrollToSelection() {
  4800. var _this16 = this;
  4801. var startDOM = this.domSelectionRange().focusNode;
  4802. if (this.someProp("handleScrollToSelection", function (f) {
  4803. return f(_this16);
  4804. })) ;else if (this.state.selection instanceof prosemirrorState.NodeSelection) {
  4805. var target = this.docView.domAfterPos(this.state.selection.from);
  4806. if (target.nodeType == 1) scrollRectIntoView(this, target.getBoundingClientRect(), startDOM);
  4807. } else {
  4808. scrollRectIntoView(this, this.coordsAtPos(this.state.selection.head, 1), startDOM);
  4809. }
  4810. }
  4811. }, {
  4812. key: "destroyPluginViews",
  4813. value: function destroyPluginViews() {
  4814. var view;
  4815. while (view = this.pluginViews.pop()) if (view.destroy) view.destroy();
  4816. }
  4817. }, {
  4818. key: "updatePluginViews",
  4819. value: function updatePluginViews(prevState) {
  4820. if (!prevState || prevState.plugins != this.state.plugins || this.directPlugins != this.prevDirectPlugins) {
  4821. this.prevDirectPlugins = this.directPlugins;
  4822. this.destroyPluginViews();
  4823. for (var i = 0; i < this.directPlugins.length; i++) {
  4824. var plugin = this.directPlugins[i];
  4825. if (plugin.spec.view) this.pluginViews.push(plugin.spec.view(this));
  4826. }
  4827. for (var _i17 = 0; _i17 < this.state.plugins.length; _i17++) {
  4828. var _plugin = this.state.plugins[_i17];
  4829. if (_plugin.spec.view) this.pluginViews.push(_plugin.spec.view(this));
  4830. }
  4831. } else {
  4832. for (var _i18 = 0; _i18 < this.pluginViews.length; _i18++) {
  4833. var pluginView = this.pluginViews[_i18];
  4834. if (pluginView.update) pluginView.update(this, prevState);
  4835. }
  4836. }
  4837. }
  4838. }, {
  4839. key: "updateDraggedNode",
  4840. value: function updateDraggedNode(dragging, prev) {
  4841. var sel = dragging.node,
  4842. found = -1;
  4843. if (this.state.doc.nodeAt(sel.from) == sel.node) {
  4844. found = sel.from;
  4845. } else {
  4846. var movedPos = sel.from + (this.state.doc.content.size - prev.doc.content.size);
  4847. var moved = movedPos > 0 && this.state.doc.nodeAt(movedPos);
  4848. if (moved == sel.node) found = movedPos;
  4849. }
  4850. this.dragging = new Dragging(dragging.slice, dragging.move, found < 0 ? undefined : prosemirrorState.NodeSelection.create(this.state.doc, found));
  4851. }
  4852. }, {
  4853. key: "someProp",
  4854. value: function someProp(propName, f) {
  4855. var prop = this._props && this._props[propName],
  4856. value;
  4857. if (prop != null && (value = f ? f(prop) : prop)) return value;
  4858. for (var i = 0; i < this.directPlugins.length; i++) {
  4859. var _prop = this.directPlugins[i].props[propName];
  4860. if (_prop != null && (value = f ? f(_prop) : _prop)) return value;
  4861. }
  4862. var plugins = this.state.plugins;
  4863. if (plugins) for (var _i19 = 0; _i19 < plugins.length; _i19++) {
  4864. var _prop2 = plugins[_i19].props[propName];
  4865. if (_prop2 != null && (value = f ? f(_prop2) : _prop2)) return value;
  4866. }
  4867. }
  4868. }, {
  4869. key: "hasFocus",
  4870. value: function hasFocus() {
  4871. if (ie) {
  4872. var node = this.root.activeElement;
  4873. if (node == this.dom) return true;
  4874. if (!node || !this.dom.contains(node)) return false;
  4875. while (node && this.dom != node && this.dom.contains(node)) {
  4876. if (node.contentEditable == 'false') return false;
  4877. node = node.parentElement;
  4878. }
  4879. return true;
  4880. }
  4881. return this.root.activeElement == this.dom;
  4882. }
  4883. }, {
  4884. key: "focus",
  4885. value: function focus() {
  4886. this.domObserver.stop();
  4887. if (this.editable) focusPreventScroll(this.dom);
  4888. selectionToDOM(this);
  4889. this.domObserver.start();
  4890. }
  4891. }, {
  4892. key: "root",
  4893. get: function get() {
  4894. var _this17 = this;
  4895. var cached = this._root;
  4896. if (cached == null) {
  4897. var _loop5 = function _loop5(search) {
  4898. if (search.nodeType == 9 || search.nodeType == 11 && search.host) {
  4899. if (!search.getSelection) Object.getPrototypeOf(search).getSelection = function () {
  4900. return search.ownerDocument.getSelection();
  4901. };
  4902. return {
  4903. v: _this17._root = search
  4904. };
  4905. }
  4906. },
  4907. _ret3;
  4908. for (var search = this.dom.parentNode; search; search = search.parentNode) {
  4909. _ret3 = _loop5(search);
  4910. if (_ret3) return _ret3.v;
  4911. }
  4912. }
  4913. return cached || document;
  4914. }
  4915. }, {
  4916. key: "updateRoot",
  4917. value: function updateRoot() {
  4918. this._root = null;
  4919. }
  4920. }, {
  4921. key: "posAtCoords",
  4922. value: function posAtCoords(coords) {
  4923. return _posAtCoords(this, coords);
  4924. }
  4925. }, {
  4926. key: "coordsAtPos",
  4927. value: function coordsAtPos(pos) {
  4928. var side = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  4929. return _coordsAtPos(this, pos, side);
  4930. }
  4931. }, {
  4932. key: "domAtPos",
  4933. value: function domAtPos(pos) {
  4934. var side = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  4935. return this.docView.domFromPos(pos, side);
  4936. }
  4937. }, {
  4938. key: "nodeDOM",
  4939. value: function nodeDOM(pos) {
  4940. var desc = this.docView.descAt(pos);
  4941. return desc ? desc.nodeDOM : null;
  4942. }
  4943. }, {
  4944. key: "posAtDOM",
  4945. value: function posAtDOM(node, offset) {
  4946. var bias = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1;
  4947. var pos = this.docView.posFromDOM(node, offset, bias);
  4948. if (pos == null) throw new RangeError("DOM position not inside the editor");
  4949. return pos;
  4950. }
  4951. }, {
  4952. key: "endOfTextblock",
  4953. value: function endOfTextblock(dir, state) {
  4954. return _endOfTextblock(this, state || this.state, dir);
  4955. }
  4956. }, {
  4957. key: "pasteHTML",
  4958. value: function pasteHTML(html, event) {
  4959. return doPaste(this, "", html, false, event || new ClipboardEvent("paste"));
  4960. }
  4961. }, {
  4962. key: "pasteText",
  4963. value: function pasteText(text, event) {
  4964. return doPaste(this, text, null, true, event || new ClipboardEvent("paste"));
  4965. }
  4966. }, {
  4967. key: "destroy",
  4968. value: function destroy() {
  4969. if (!this.docView) return;
  4970. destroyInput(this);
  4971. this.destroyPluginViews();
  4972. if (this.mounted) {
  4973. this.docView.update(this.state.doc, [], viewDecorations(this), this);
  4974. this.dom.textContent = "";
  4975. } else if (this.dom.parentNode) {
  4976. this.dom.parentNode.removeChild(this.dom);
  4977. }
  4978. this.docView.destroy();
  4979. this.docView = null;
  4980. }
  4981. }, {
  4982. key: "isDestroyed",
  4983. get: function get() {
  4984. return this.docView == null;
  4985. }
  4986. }, {
  4987. key: "dispatchEvent",
  4988. value: function dispatchEvent(event) {
  4989. return _dispatchEvent(this, event);
  4990. }
  4991. }, {
  4992. key: "dispatch",
  4993. value: function dispatch(tr) {
  4994. var dispatchTransaction = this._props.dispatchTransaction;
  4995. if (dispatchTransaction) dispatchTransaction.call(this, tr);else this.updateState(this.state.apply(tr));
  4996. }
  4997. }, {
  4998. key: "domSelectionRange",
  4999. value: function domSelectionRange() {
  5000. return safari && this.root.nodeType === 11 && deepActiveElement(this.dom.ownerDocument) == this.dom ? safariShadowSelectionRange(this) : this.domSelection();
  5001. }
  5002. }, {
  5003. key: "domSelection",
  5004. value: function domSelection() {
  5005. return this.root.getSelection();
  5006. }
  5007. }]);
  5008. return EditorView;
  5009. }();
  5010. function computeDocDeco(view) {
  5011. var attrs = Object.create(null);
  5012. attrs["class"] = "ProseMirror";
  5013. attrs.contenteditable = String(view.editable);
  5014. view.someProp("attributes", function (value) {
  5015. if (typeof value == "function") value = value(view.state);
  5016. if (value) for (var attr in value) {
  5017. if (attr == "class") attrs["class"] += " " + value[attr];else if (attr == "style") attrs.style = (attrs.style ? attrs.style + ";" : "") + value[attr];else if (!attrs[attr] && attr != "contenteditable" && attr != "nodeName") attrs[attr] = String(value[attr]);
  5018. }
  5019. });
  5020. if (!attrs.translate) attrs.translate = "no";
  5021. return [Decoration.node(0, view.state.doc.content.size, attrs)];
  5022. }
  5023. function updateCursorWrapper(view) {
  5024. if (view.markCursor) {
  5025. var dom = document.createElement("img");
  5026. dom.className = "ProseMirror-separator";
  5027. dom.setAttribute("mark-placeholder", "true");
  5028. dom.setAttribute("alt", "");
  5029. view.cursorWrapper = {
  5030. dom: dom,
  5031. deco: Decoration.widget(view.state.selection.head, dom, {
  5032. raw: true,
  5033. marks: view.markCursor
  5034. })
  5035. };
  5036. } else {
  5037. view.cursorWrapper = null;
  5038. }
  5039. }
  5040. function getEditable(view) {
  5041. return !view.someProp("editable", function (value) {
  5042. return value(view.state) === false;
  5043. });
  5044. }
  5045. function selectionContextChanged(sel1, sel2) {
  5046. var depth = Math.min(sel1.$anchor.sharedDepth(sel1.head), sel2.$anchor.sharedDepth(sel2.head));
  5047. return sel1.$anchor.start(depth) != sel2.$anchor.start(depth);
  5048. }
  5049. function buildNodeViews(view) {
  5050. var result = Object.create(null);
  5051. function add(obj) {
  5052. for (var _prop3 in obj) if (!Object.prototype.hasOwnProperty.call(result, _prop3)) result[_prop3] = obj[_prop3];
  5053. }
  5054. view.someProp("nodeViews", add);
  5055. view.someProp("markViews", add);
  5056. return result;
  5057. }
  5058. function changedNodeViews(a, b) {
  5059. var nA = 0,
  5060. nB = 0;
  5061. for (var _prop4 in a) {
  5062. if (a[_prop4] != b[_prop4]) return true;
  5063. nA++;
  5064. }
  5065. for (var _ in b) nB++;
  5066. return nA != nB;
  5067. }
  5068. function checkStateComponent(plugin) {
  5069. if (plugin.spec.state || plugin.spec.filterTransaction || plugin.spec.appendTransaction) throw new RangeError("Plugins passed directly to the view must not have a state component");
  5070. }
  5071. exports.Decoration = Decoration;
  5072. exports.DecorationSet = DecorationSet;
  5073. exports.EditorView = EditorView;
  5074. exports.__endComposition = __endComposition;
  5075. exports.__parseFromClipboard = __parseFromClipboard;
  5076. exports.__serializeForClipboard = __serializeForClipboard;